Detailed Use of Mybatis ResultMap

Keywords: MySQL Attribute Mybatis Java Mybaits

Definition of Mybatis

MyBatis is an excellent persistence framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manual parameter setting and retrieving result sets. MyBatis can
To configure and map native information using simple XML or annotations, the interface and Java POJOs(Plain Old Java Objects, common Java objects) are mapped to records in the database.

How to use Mybaits? Here's an article about how to use Mybaits. Mybatis Introduction, no used children's shoes can be referred to, today's main talk is ResultMap.

ReultMap

ResultMap is the most powerful element of Mybatis, which maps complex data queried (such as data queried into several tables) into a result set.

ResultMap contains elements:

<! - Column can be a field of any table without restriction, and property must be a pojo attribute defined for type - >.
<resultMap id= "Unique ID" type= "Mapped pojo object">
<id column= "primary key field of table, or alias field in query statement" jdbcType= "field type" property= "mapping primary key property of pojo object"/>
<result column= "a field of a table (which can be a field of any table)" jdbcType= "field type" property= "an attribute mapped to a pojo object (a genus in a pojo object that needs to be defined for type"                        
Sex "/"
<association property= "an object property of pojo" javaType="pojo objects associated with pojo">
<id column= "primary key field of associated pojo object corresponding table" jdbcType= "field type" property= "chairman property of associated pojo object"/>
<result column= "fields of any table" jdbcType= "field type" property= "properties of associated pojo objects"/>
</association>
<! - Property in a collection must be the property of a pojo object defined for software type - >
<collection property= "collection property of pojo" ofType= "pojo objects in a collection">
<id column= "Primary key fields of tables corresponding to pojo objects in a collection" jdbcType= "Field type" property= "Primary key properties of pojo objects in a collection"/>
<result column= "fields that can be any table" jdbcType= "field type" property= "properties of pojo objects in a collection" />  
</collection>
</resultMap>

< strong > If the collection tag uses nested queries, the format is as follows: </strong>.

<collection column= "field parameters passed to nested query statements" property= "set attributes in POJO objects" ofType= "pojo objects in collection attributes" select= "nested query statements"> 
</collection>

Be careful:
Column in the collection tag: the parameters to be passed to the select query statement are column="{parameter name 1 = table field 1, parameter name 2 = table field 2} if multiple parameters are passed.

Example scenarios introduce the use of ResultMap:


Simple requirements: mapping the results of a commodity;


1. Create commodity pojo objects:

public class TShopSku  {
    /**
     * Primary key ID
     */
    private Long id;

    /**
     * Trade name
     */
private String skuName;

    /**
     * Classification ID
     */
private Long categoryId;


    /**
     * Primary key ID
     * @return ID 
     */
public Long getId() {
    return id;
}

    /**
     * The primary key is ID.
     * @param id 
     */
public void setId(Long id) {
    this.id = id;
}

    /**
     * Trade name
     * @return SKU_NAME Trade name
     */
public String getSkuName() {
    return skuName;
}

    /**
     * Trade name
     * @param skuName Trade name
     */
public void setSkuName(String skuName) {
    this.skuName = skuName == null ? null : skuName.trim();
}

    /**
     * Classification ID
     * @return CATEGORY_ID Classification ID
     */
public Long getCategoryId() {
    return categoryId;
}

    /**
     * Classification ID
     * @param categoryId Classification ID
     */
public void setCategoryId(Long categoryId) {
    this.categoryId = categoryId;
}

The corresponding resultMap:

<resultMap id="BaseResultMap" type="com.meikai.shop.entity.TShopSku">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
    <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
</resultMap> 

2. Merchandise pojo class adds attribute set:

  A commodity will have some attributes. Now we need to add the queried commodity attributes to the commodity object. First, we need to add a set of attributes on the basis of the original commodity pojo class:
    /**
     * Attribute Set
     */
private List<TShopAttribute> attributes;

    /**
     * Get the set of attributes
     */
public List<TShopAttribute> getAttributes() {
    return attributes;
}

    /**
     * Setting attribute sets
     * @param attributes
     */
 public void setAttributes(List<TShopAttribute> attributes) {
    this.attributes = attributes;
 }

There are two ways to add Collection tags to resultMap

1. Nested results:
The corresponding resultMap:

<resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
    <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
    <collection property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" > 
        <id column="AttributeID" jdbcType="BIGINT" property="id" />
        <result column="attribute_NAME" jdbcType="VARCHAR" property="attributeName" />
    </collection>
</resultMap>

Query statement:

<select id="getById"  resultMap="basePlusResultMap">
    select s.ID,s.SKU_NAME,s.CATEGORY_ID,a.ID,a.ATTRIBUTE_NAME
    from t_shop_sku s,t_shop_attribute a 
    where s.ID =a.SKU_ID and s.ID = #{id,jdbcType =BIGINT};
</select>

2. Relevant nested queries (add select attribute to collection):
Commodity result set mapping resultMap:

<resultMap id="BasePlusResultMap" type="com.meikai.shop.entity.TShopSku">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
    <result column="CATEGORY_ID" jdbcType="BIGINT" property="categoryId" />
    <collection column="{skuId=ID}" property="attributes" ofType="com.meikai.shop.entity.TShopAttribute" select="getAttribute" > 
    </collection>
</resultMap>

collection's select executes the following query attribute statement:

<select id="getAttribute"  resultMap="AttributeResultMap">
    select a.ID,s.ATTRIBUTE_NAME
    from t_shop_attribute a
    where  a.ID = #{skuId,jdbcType =BIGINT};
</select>

Property result set mapping:

<resultMap id="AttributeResultMap" type="com.meikai.shop.entity.TShopAttribute">
    <id column="ID" jdbcType="BIGINT" property="id" />
    <result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName" />
</resultMap>

BasePlus ResultMap contains Collection s for attribute query statements.
So you can get the commodity and the set of attributes it contains by querying the commodity statement below:

<select id="getById"  resultMap="BasePlusResultMap">
    select s.ID,s.SKU_NAME,s.CATEGORY_ID
    from t_shop_sku s
    where  s.ID = #{id,jdbcType =BIGINT};
</select>

Posted by budder on Fri, 20 Sep 2019 08:46:18 -0700