resultMap

Mybatis SQL statement returns two kinds of results

  • ResultType The queried field must have the same field or basic data type in the corresponding POJO, which is suitable for simple query
  • ResultMap requires custom fields, multi-table queries, one-to-many relationships, etc. It is more powerful than resultType and suitable for complex queries
<resultMap id="VideoResultMap" type="Video">
 <! Property poJO class name -->
 <id column="id" property="id" jdbcType="INTEGER" />
 <result column="video_tile" property="title" jdbcType="VARCHAR" />
 <result column="summary" property="summary" jdbcType="VARCHAR" />
 <result column="cover_img" property="coverImg" jdbcType="VARCHAR" />
</resultMap>
<select id="selectBaseFieldByIdWithResultMap" resultMap="VideoResultMap">
select id , title as video_tile, summary, cover_img from video where id = #
{video_id}
</select>
Copy the code

Mybatis Complex object mapping Configures association of ResultMap

  • Association: maps to some complex type property of a POJO, such as an order object that contains a User object
<?xml version="1.0" encoding="UTF-8" ? >

      
<! Namespace: a namespace that can map SQL statements to the corresponding method name and parameter, return type mybatis, use interface dynamic proxy -->
<mapper namespace="cn.junko.dao.VideoOrderMapper">
    <resultMap id="VideoOrderResultMap" type="VideoOrder">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="out_trade_no" property="outTradeNo"/>
        <result column="create_time" property="createTime"/>
        <result column="state" property="state"/>
        <result column="total_fee" property="totalFee"/>
        <result column="video_id" property="videoId"/>
        <result column="video_title" property="videoTitle"/>
        <! JavaType: the type of this property -->
        <association property="user" javaType="User">
            <id property="id" column="user_id"/>
            <result property="name" column="name"/>
            <result property="headImg" column="head_img"/>
            <result property="createTime" column="create_time"/>
            <result property="phone" column="phone"/>
        </association>
    </resultMap>
    <select id="queryVideoOrder" resultMap="VideoOrderResultMap">
        select o.id id,
        o.user_id,
        o.out_trade_no,
        o.create_time,
        o.state,
        o.total_fee,
        o.video_id,
        o.video_id,
        o.video_title,
        u.name,
        u.phone
        from video_order o left join user u on o.user_id = u.id
    </select>
</mapper>
Copy the code

Then the call executes and finds that it successfully echoes the data and encapsulates the User object

ResultMap A collection of one-to-many query result mappings of complex objects

  • Collection: A one-to-many query result query mapping, such as user having multiple orders
<resultMap id="UserOrder" type="User">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="pwd" column="pwd"/>
        <result property="headImg" column="head_img"/>
        <result property="createTime" column="create_time"/>
        <result property="phone" column="phone"/>
        <! OfType: PoJO object in the collection -->
        <collection property="videoOrderList" ofType="VideoOrder">
            <id column="order_id" property="id"/>
            <result column="user_id" property="userId"/>
            <result column="out_trade_no" property="outTradeNo"/>
            <result column="create_time" property="createTime"/>
            <result column="state" property="state"/>
            <result column="total_fee" property="totalFee"/>
            <result column="video_id" property="videoId"/>
            <result column="video_title" property="videoTitle"/>
        </collection>
    </resultMap>
    <select id="queryUserOrder" resultMap="UserOrder">
        select
        u.name,
        u.id,
        u.phone,
        o.id order_id,
        o.video_title,
        o.video_id
        from user u left join video_order o on u.id = o.user_id
    </select>
Copy the code

Summarize complex object queries for ResultMap

  • Association maps to a POJO class that handles one-to-one associations.
  • Collection a collection list of maps that handle a one-to-many association.
  • The template
<! Column can be a column of any table, and property must be a POJO attribute defined by type.
<resultMap id="Unique identifier" type="Mapped POJO objects">
 <id column="Primary key field of the table, or alias field in the query statement" jdbcType="Field Type" property="Map primary key properties of POJO objects" />
 <result column="A field in a table" jdbcType="Field Type" property="A property mapped to a POJO object"/>
 <association property="An object property of the POJO" javaType="Pojo associated POJO objects">
<id column="Associate primary key field of table corresponding to POJO object" jdbcType="Field Type" property="Associate properties of POJO objects"/>
 <result column="Fields of a table" jdbcType="Field Type" property="Associate properties of POJO objects"/>
 </association>
 <! Property of the poJO object defined for ofType
 <collection property="Collection property name of POJO" ofType="Individual POJO object types in the collection">
 <id column="Pojo objects in the collection correspond to primary key fields in the table" jdbcType="Field Type" property="Primary key property of poJO object in collection" />
 <result column="Field of any table" jdbcType="Field Type" property="Properties of POJO objects in collections" />
 </collection>
</resultMap>
Copy the code