[Mybatis] configure the resultMap element and resultType element of the mapping file

resultMap overview

resultMapis Mybatisthe most important and powerful element in the mapping file. It describes how to load objects from the result set, and its main role is to define mapping rules, cascading updates, and custom type converters. It frees you from 90% of the JDBC ResultSets data extraction code, and in some cases allows you to do things that JDBC doesn't support. In fact, it is likely to replace thousands of lines of equivalently functional code when jointly mapping complex statements. The design idea of ​​resultMap is that simple statements do not require explicit result mapping, while more complex statements only need to describe their relationship.

resultMapThe child elements are as follows:

<resultMap>
        <constructor>
            <idArg/>
            <arg/>
        </constructor>
        <id/>
        <result/>
        <association property=""/>
        <collection property=""/>
        <discriminator javaType="">
            <case value=""></case>
        </discriminator>
</resultMap>
element effect
constructor Used to configure the constructor method to inject the result into the constructor method when the class is instantiated
id primary key of the column
result Configure POJO to database column name mapping relationship
association cascade usage - represents a one-to-one relationship
collection Cascading uses - to represent a one-to-many relationship
discriminator Cascading use - the discriminator selects the instance according to the actual, the result set can be determined by specific conditions

resultType元素

Use resultType for output mapping. Only when the queried column name is consistent with the attribute name in pojo (entity bean), the column can be successfully mapped. To put it simply, your database field and the field name in JavaBean must be the same to map successfully.
Example database field:
insert image description here

使用pojo类存储结果

Entity class:

public class User {
    
    
    private int id;
    private String name;
    private int age;
	//省略get set方法
}
public interface UserMapper {
    
    
   User slelectAll();
}

return a result set

    <select id="slelectAll" resultType="com.lucas.mybatis.model.User">
        select  * from user where id = 2
    </select>

使用集合存储结果

resultType can also return multiple result sets

public interface UserMapper {
    
    
    List<User> slelectAll();
}

mapper file

 <select id="slelectAll" resultType="com.lucas.mybatis.model.User">
     select  * from user
 </select>

使用Map存储结果集

 <select id="slelectAll" resultType="java.util.Map">
     select  * from user where id = 2
 </select>
public interface UserMapper {
    
    
   Map<String ,Object> slelectAll();
}
Map<String ,Object> map1 = userMapper.slelectAll();
 System.out.println(map1);

在这里插入图片描述

resultMap映射结果集

So when the field names in our JavaBean are different from the database field names, or when multi-table queries are made, we generally use resultMap
to mapper.xmlmodify the file as follows

    <resultMap id="userResult" type="com.lucas.mybatis.model.User">
        <id property="id1" column="id"/>
        <result property="name1" column="name"/>
        <result property="age1" column="age1"/>
    </resultMap>
    <select id="slelectAll" resultMap="userResult">
        select  * from user where id = 2
    </select>

Results of the:
在这里插入图片描述

Guess you like

Origin blog.csdn.net/huweiliyi/article/details/107907310