Use the columnPrefix alias to distinguish the field of which class it belongs

There are several problems in writing an association table query today.

You need to find out all the status of this order. It belongs to one to many relationship

//This is the query statement
 <select id="selectOrderByMap" parameterType="java.lang.String" resultMap="OrderLinkAudit">
        select
        ord.id,ord.ser_title,ord.org_name,ord.status,audit.content as audit_content,    audit.create_time as audit_create_time,audit.order_id as audit_order_id 
        from ser_order ord
        left join ser_order_audit audit on ord.id = audit.order_id
        <where>
            <if test="_parameter!= null">
                and ord.id = #{_parameter, jdbcType=VARCHAR}
            </if>
        </where>
        order by audit.create_time desc
    </select>


// Order associated audit record 
    <resultMap id="OrderLinkAudit" type="com.xx.xx.xx.xx.Order" extends="BaseResultMap">
        <collection property="auditList" columnPrefix="audit_"
                    ofType="com.xx.xx.xx.xx.OrderAudit">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="order_id" jdbcType="VARCHAR" property="orderId"/>
            <result column="status" jdbcType="INTEGER" property="status"/>
            <result column="user_id" jdbcType="VARCHAR" property="userId"/>
            <result column="user_name" jdbcType="VARCHAR" property="userName"/>
            <result column="content" jdbcType="VARCHAR" property="content"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        </collection>
    </resultMap>

I don't know resultMap, so I made a mistake when using it.

resultMap: suitable for the case where the return value is a custom entity class.

resultType: it is suitable to use the non customized data type of return value, that is, the type provided by jdk.

In order to return the same field content, create "time, the alias columnPrefix =" audit "defined in resultMap was not used at the beginning, which directly covered the same field value of the previous table.
columnPrefix: the alias defined is used to distinguish the fields of which class, map the corresponding properties, and distinguish which classes they belong to.

Tags: Java JDK

Posted on Fri, 01 May 2020 11:06:44 -0700 by smordue