Using resultmap

Time:2020-10-17

Resultmap tag is a common element in mybatis framework. It is also a very important mapping element. It is used to implement the high-level mapping of mybatis

Its application scenarios are as follows:

1) When the field in the table is inconsistent with the attribute name in the POJO class, (such as stu_ id/stuId).

< resultmap type = return type fully qualified name "id = named" >
    <id property="id" column="id" />
    <result property="name" column="name"/>
    <result property="note" column="note"/>    
</resultMap>

The method is as follows: type is the fully qualified name of the return type, and ID is the name defined for the resultmap. Write the ID on the resultmap. The < ID > in the resultmap must be the primary key defined in the table, < result > is other properties in the table. The property attribute is the attribute name defined by POJO, and column is the field name in the table

2) SQL statement nested query: when we query a 1-to-N relationship, we can select the collection element, which is also the property name defined for POJO, column is the field name in the table, and the select attribute points to another Dao layer method of fully qualified name

< resultmap type = return type fully qualified name "id = named" >
    <id property="id" column="id" />
    <result property="name" column="name"/>
    <result property="note" column="note"/>    
    <! -- collection is generally applied to One2many query -- >
    <collection property="menuIds" column="id">
        < select = "fully qualified class name" / >
    </collection>
</resultMap>

3) Multi table associated query: through left outer or right outer join, the required tables are associated together for query, the information of benchmark table is written in < ID > < result >, the associated tables are written in < Collection > one by one, and the oftype property is the type of single data in the property array

<select id="findObjectById" resultMap="sysRoleMenuVo">
    select r.id,r.name,r.note,rm.menu_id
    from sys_roles r left join sys_role_menus rm
    on r.id=rm.role_id
    where r.id=#{id}
</select>

<resultMap type="com.cy.pj.sys.pojo.SysRoleMenuVo"
    id="sysRoleMenuVo">
    <id property="id" column="id" />
    <result property="name" column="name"/>
    <result property="note" column="note"/>
    <! -- collection is generally applied to One2many query -- >
    <collection property="menuIds" ofType="integer">
        <result column="menu_id"/>
    </collection>
</resultMap>

Association and collection

In addition to the collection mentioned above in resultmap, association can also be used for multi table query in the data layer

We need to distinguish between the two:
Association is used when querying 1 – > 1 or N – > 1;
Collection is used when querying 1 – > n;

You may not be able to distinguish between 1 to 1 / 1 and N / N to 1?

for instance:
For example, if there is a class table / a student table, we all know that one class must correspond to multiple students;
Now we need to find out the class in which the students are in when querying. At this time, the relationship between N and 1 is n-to-1, and association is used
Now, if we need to query all the students in the class when querying the class, then it is the 1-to-N relationship — > we need to use collection

<collection property="students" ofType="com.glj.pojo.Student"
column="id" javaType="ArrayList"
select="com.glj.mapper.StudentMapper.selectStudentByClazzId"/>
    <id property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="age" column="age"/>
</collection>
<association property="clazz" javaType="com.glj.pojo.Clazz"/>
    <id property="id" column="id"/>
    <result property="code" column="code"/>
    <result property="name" column="name"/>
</association>