The use of the configuration file resultMap tag in MyBatis

       The function of the resultMap tag is to map the set of results from the select query. Its main function is to associate and map the fields in the entity class with the fields in the database table.

E.g:

1. Entity class code

public class Test
{
    private int id;
    private String parentId;
    private String name;
    private String enName;
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    public String getParentId()
    {
        return parentId;
    }
    public void setParentId(String parentId)
    {
        this.parentId = parentId;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getEnName()
    {
        return enName;
    }
    public void setEnName(String enName)
    {
        this.enName = enName;
    }
   
}

When the query data table field and entity class field name are the same:

create table test1(
    id int(5) primary key,
    parentId int(5),
    name varchar(10),
    enName varchar (10)
)


Use in config file:

<resultMap type="com.test" id="testResultMap">
</resultMap>

<select id="selectList"  resultMap="testResultMap">
        select * from test1
</select>




When the query data table field and entity class field names are inconsistent:

create table test2(
    id int(5) primary key,
    parent_id int(5),
    name varchar(10),
    en_name varchar(10)
)

The configuration in the configuration file is:

<!-- type points to your javabean class, id can be customized -->
<resultMap type="Category" id="category">
    <!-- property corresponds to the attribute name of the entity class, and column is the name of the column of the database result set -->
   <id property="id" column="id" />
   <result property="parentId" column="parent_id" jdbcType="INTEGER"/>
   <result property="name" column="name" jdbcType="VARCHAR"/>
   <result property="enName" column="en_name" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectList"  resultMap="testResultMap">
        select * from test2
    </select>

      Note: When the fields in the entity class are the same as the fields in the database table, the association relationship in the resultMap tag can be ignored.

              When the fields in the entity class are different from the fields in the database table, it is necessary to associate and map the entity class fields with the database fields one by one in the resultMap tag.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848991&siteId=291194637