ResultMap custom query results by map

mybatis in the use of automatic mapping resultType do, pay attention to pojo field and attribute names must be the same , if not, you will need to alias the field, to ensure consistency with the alias name attribute.

Use resultMap do custom result mapping, field names can be different , and you can specify which columns to display , more flexible, but also a wide range of applications.

<select id="getUserList" resultMap="userList" parameterType="User">
  select u.*, r.roleName from smbms_user u, smbms_role r 
  where u.userName like connect ('%', #{userName}, '%')
  and u.userRole=#{userRole} and u.userRole=r.id
</select>

<resultMap id="userList" type="User">
  <result property="id" column="id"/>
  <result property="userCode" column="userCode"/>
  <result property="userName" column="userName"/>
  <result property="phone" column="phone"/>
  <result property="birthday" column="birthday"/>
  <result property="gender" column="gender"/>
  <result property="userRole" column="userRole"/>
  <result property="userRoleName" column="roleName"/>
</resultMap>

 In the above code, a contingency table queries, to obtain the corresponding user name in Chinese character

resultMap element is used to describe how the results set mapping to a java object , the object is used here to show the necessary fields resultMap desired mapping freely, especially in the case where pojo field names and attribute names in the database consistent.

result for identifying the child node simple properties, which represents a column attribute query field name from the database, check out the Property which property value assigned to the field corresponding to said physical object.

When mybatis query mapping for each field value out of the query are placed inside a corresponding Map , wherein the key is a field name, the value of its corresponding values. When the select element return type attribute is provided when resultType, mybatis Map will be taken inside the key-value pair is assigned to the specified target resultType corresponding property (i.e., property setter method is called the object corresponding to the filling). Because of this, when using resultType directly able to receive its corresponding attribute value of the object in the background .

This shows that, in fact, mybatis each query types are mapped return restultMap, but when we return type attribute is provided resultType time, mybatis will automatically assign a value to the corresponding resultType specified properties of an object, and when we offer a return type attribute is resultMap time, since Map not a good representation of the domain model , you need to define it further into a corresponding physical object.

In the select element mybatis, the essence is the same resultType and resultMap, are Map data structure. The two can not coexist .

resultMap automatic mapping level

The above code, check out all the attributes smbms_user table, although resultMap mapping associated with some attributes, but do not attribute (the same field name attribute) associated with the mapping in resultMap also normal output in the background. This mapping with automatic level resultMap related default mapping level of the PARTIAL . If you want the mapping not done in resultMap associated attribute is not output, it is necessary to set mybatis resultMap automatic map level (autoMappingBehavior) is NONE, which prohibits the automatic matching. as follows

<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE configuration PUBLIC
                "-//mybatis.org//DTD Config 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
        <setting name="autoMappingBehavior" value="NONE"/>
    </settings>
</configuration>

 

Guess you like

Origin www.cnblogs.com/yanguobin/p/11698675.html