Home > Java > javaTutorial > Detailed analysis of the differences and connections between resultType and resultMap in mybatis

Detailed analysis of the differences and connections between resultType and resultMap in mybatis

巴扎黑
Release: 2017-07-17 13:21:58
Original
3974 people have browsed it

When using mybatis for database connection operations, there are usually two ways to process the results returned by SQL statements. One is resultType and the other is resultMap. Let’s talk about my knowledge and understanding of the two.

For example, the single-table query we usually use often uses resultType

Come down and look at a piece of code

 1 package org.cxxy.base.cxsc.entity; 2  3 public class TbClass { 4     private Integer id; 5  6     private String classname; 7  8     private String deptname; 9 10     public Integer getId() {11         return id;12     }13 14     public void setId(Integer id) {15         this.id = id;16     }17 18     public String getClassname() {19         return classname;20     }21 22     public void setClassname(String classname) {23         this.classname = classname == null ? null : classname.trim();24     }25 26     public String getDeptname() {27         return deptname;28     }29 30     public void setDeptname(String deptname) {31         this.deptname = deptname == null ? null : deptname.trim();32     }33 }
Copy after login

Above For the PO class, I use a small Demo of mine

Come down and start pasting my XML Mapper

<resultMap id="BaseResultMap" type="org.cxxy.base.cxsc.entity.TbClass"><id column="id" jdbcType="INTEGER" property="id" /><result column="classname" jdbcType="VARCHAR" property="classname" /><result column="deptname" jdbcType="VARCHAR" property="deptname" /></resultMap>
Copy after login

This resultMap corresponds to mine The attributes of the po class

come down and post the single table query statement of my xml

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
Copy after login
select 
 id, classname, deptname
from tb_class
where id = #{id,jdbcType=INTEGER}
</select>
Copy after login
 <br>
Copy after login

parameterType represents the input parameter (for example: select * from tb_class where id = "xxxx"), resultMap represents the mapped result set. You can also see the Map from the naming, which is of course the result set.

The above code represents the single table query (one-to-one), of course , in general development, for this kind of mapping, we usually use the following writing method

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="类的全限定名">select 
 id, classname, deptname
from tb_class
where id = #{id,jdbcType=INTEGER}</select>
Copy after login

That is to say, the results obtained are the same. Generally speaking, in terms of our understanding, we try to still Choose the latter

But if the needs of the customer change, for example, an extension class of the class is written

org.cxxy.base.cxsc.entity.TbClassDatail
Copy after login

If an external class is introduced in the extension class (For other table attributes (which have no common attributes with this class)), we can use resultMap, but it is not completely

resultMap

定义po类
在Orders类中加入User属性。
在Orders类中加入List<Orderdetail> orderdetails属性
Copy after login

Order Query List

<select id="findOrdersDetailList" resultMap="userorderdetailmap">SELECT
    orders.*,
    user.username,
    user.address,
    orderdetail.id orderdetail_id,
    orderdetail.items_id,
    orderdetail.items_num
    FROM orders,user,orderdetail
    WHERE orders.user_id = user.id
    AND orders.id = orderdetail.orders_id</select> 
Copy after login

<!-- 订单信息resultmap --><resultMap type="cn.itcast.mybatis.po.Orders" id="userorderdetailmap"><id property="id"column="id"/><result property="user_id" column="user_id"/><result property="number" column="number"/><association property="user" javaType="cn.itcast.mybatis.po.User"><id property="id" column="user_id"/><result property="username" column="username"/><result property="address" column="address"/></association><collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail"><id property="id" column="orderdetail_id"/><result property="items_id" column="items_id"/><result property="items_num" column="items_num"/></collection></resultMap>
Copy after login

The above code is an order from a training institution. Query code,

The relationship between the above entity classes is: Order----->User one-to-one (one user, one order) Order------->OrderDetail one-to-many ( An order has multiple order details)

For mapping of one-to-many and many-to-many queries like this, we try to use resultMap

注:collection 标签是一对多的映射,常用于一对多中扩展类下的List<po对象>的属性
   association标签适用扩展类包含的一对一的po类对象属性
Copy after login

The difference between resultType and resultMap in MyBatis

When querying select mapping in MyBatis, the return type can be resultType or resultMap. resultType directly represents the return type (corresponding to the entities in our model object), while resultMap is external Reference to ResultMap (the implicit key-->value relationship between db and model is defined in advance), but resultType and resultMap cannot exist at the same time.
When MyBatis performs query mapping, each attribute queried is actually placed in a corresponding Map, where the key is the attribute name and the value is its corresponding value.
① When the provided return type attribute is resultType, MyBatis will take out the key-value pairs in the Map and assign them to the attributes corresponding to the object specified by resultType. So in fact, the return type of every query map of MyBatis is ResultMap, but when the provided return type attribute is resultType, MyBatis automatically assigns the corresponding value to the attribute of the object specified by resultType.
② When the provided return type is resultMap, because Map cannot represent the domain model well, you need to further convert it into the corresponding object yourself, which is often very useful in complex queries.

To summarize

resultType:

Function:

Map the query results to the pojo according to the consistency of the sql column name pojo attribute name (applicable to single table query only).

Occasion:

It is common to display some detailed records, such as user purchase details, and all related query information is displayed on the page. At this time, you can directly use resultType to map each record to a pojo , just traverse the list (pojo in the list) on the front-end page.

The above is the detailed content of Detailed analysis of the differences and connections between resultType and resultMap in mybatis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template