


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
<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>
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">
select id, classname, deptname from tb_class where id = #{id,jdbcType=INTEGER} </select>
<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
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="类的全限定名">select id, classname, deptname from tb_class where id = #{id,jdbcType=INTEGER}</select>
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
<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>
<!-- 订单信息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>
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类对象属性
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

JPA and MyBatis: Function and Performance Comparative Analysis Introduction: In Java development, the persistence framework plays a very important role. Common persistence frameworks include JPA (JavaPersistenceAPI) and MyBatis. This article will conduct a comparative analysis of the functions and performance of the two frameworks and provide specific code examples. 1. Function comparison: JPA: JPA is part of JavaEE and provides an object-oriented data persistence solution. It is passed annotation or X

Detailed explanation of how to use MyBatis batch delete statements requires specific code examples. Introduction: MyBatis is an excellent persistence layer framework that provides rich SQL operation functions. In actual project development, we often encounter situations where data needs to be deleted in batches. This article will introduce in detail how to use MyBatis batch delete statements, and attach specific code examples. Usage scenario: When deleting a large amount of data in the database, it is inefficient to execute the delete statements one by one. At this point, you can use the batch deletion function of MyBatis

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? During the development process, efficient data access has always been one of the focuses of programmers. For persistence layer frameworks like MyBatis, caching is one of the key methods to improve data access efficiency. MyBatis provides two caching mechanisms: first-level cache and second-level cache. The first-level cache is enabled by default. This article will introduce the mechanism of MyBatis first-level cache in detail and provide specific code examples to help readers better understand

Analysis of MyBatis' caching mechanism: The difference and application of first-level cache and second-level cache In the MyBatis framework, caching is a very important feature that can effectively improve the performance of database operations. Among them, first-level cache and second-level cache are two commonly used caching mechanisms in MyBatis. This article will analyze the differences and applications of first-level cache and second-level cache in detail, and provide specific code examples to illustrate. 1. Level 1 Cache Level 1 cache is also called local cache. It is enabled by default and cannot be turned off. The first level cache is SqlSes

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When
