Home >Java >javaTutorial >Detailed analysis of the differences and connections between resultType and resultMap in mybatis
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 }
Above For the PO class, I use a small Demo of mine
Come down and start pasting my XML Mapper
2fcf37cc8639c0835f2a112fe5f93a849eef2c25b88ca2d81d394f11ec96c407e7278c69b1e0021e9427bf255a0086f7bb49b36718ffba6c40aa7d011fafcc356ebaf93ccf0a7eb73abf17f5d66f496d
This resultMap corresponds to mine The attributes of the po class
come down and post the single table query statement of my xml
669abfcbe0c33fe8796fe4e80dcc38da
select id, classname, deptname from tb_class where id = #{id,jdbcType=INTEGER} 18bb6ffaf0152bbe49cd8a3620346341
<br>
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
a2aa3edd282ebb7d674f208ca16e6482select id, classname, deptname from tb_class where id = #{id,jdbcType=INTEGER}18bb6ffaf0152bbe49cd8a3620346341
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
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属性
Order Query List
67b7af552f22717e0a27bb8ca8afddafSELECT 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_id18bb6ffaf0152bbe49cd8a3620346341
5357cb75350360434e7a14c60a460937
1bc9007c1b08a5a41bb5ee9be64a55c77cb486de3528c1e544b606da1e2164ae6cf1cda1774140b1bf7e54614c8f58e9e08450006b72f6e37b32564ebf7e3714e38a9cf50eb8e25da5892c8813ca0011a1a7d4d2b193733ef0c15d86ce05f5537d6d4df2e47d8c9389fee9b1f90ca509285d3778291c3d7aa6eafe5c70300921113aecbcc56d2525429c3d61b48a1d346cc0a7625caa65b9709a1e50bbd52c945e983f6196ee2a11899ac257ac4a761a62a82c88b9c7d4583fd468de5781adfc29510f87cb44cd62c70fc7046aedde68501f605e20e4cedea353a3c24c992da491fc0312a46e774fade754e0d6bf5f586ebaf93ccf0a7eb73abf17f5d66f496d
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 标签是一对多的映射,常用于一对多中扩展类下的List26f8748aa4657b998cc4ad34c80d1a5b的属性 association标签适用扩展类包含的一对一的po类对象属性
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!