使用columnPrefix别名区分所属哪个类的字段

今天在写一个关联表查询的时候出现的几个问题。

需要查出这个订单的所有状态,属于一对多的关系

//这个为查询的语句
 <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>


// 订单关联审核记录 
    <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>

不了解resultMap,所以在使用的时候出了差错。

resultMap:适合使用返回值是自定义实体类的情况。

resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型。

在想返回有相同字段content,create_time ,一开始没有用resultMap中定义的别名columnPrefix=”audit_”,导致直接覆盖了,前一个表的相同字段值。
columnPrefix:定义的别名用来区分是哪个类的字段,映射对应的属性,区分他们分别属于哪些类。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章