Detailed explanation of mybatis attributes
Preface
MyBatis is based on the idea of "the database structure is uncontrollable", that is, we hope that the database follows the third normal form or BCNF, but in fact it goes against our wishes, then the result set mapping It is MyBatis that provides us with this means of converting between ideal and reality, and resultMap is the configuration label of the result set mapping.
Before going deep into the ResultMap tag, we need to understand the process from the SQL query result set to the JavaBean or POJO entity.
From SQL query results to domain model entities
Get the ResultSet object through JDBC query
Traverse the ResultSet object and temporarily store each row of data into a HashMap instance, using the field name or field alias of the result set as the key, and the field value as the value
According to the type attribute of the ResultMap tag Instantiate the domain model through reflection
-
Fill the key-value pairs in the HashMap into the domain model instance according to the type attribute of the ResultMap tag and tag information such as id and result and return
1. resultMap
1. Attribute description
id attribute, the identification of the resultMap tag.
type attribute , the fully qualified class name of the return value, or a type alias.
autoMapping attribute, value range true (default value) | false, set whether to start the automatic mapping function. The automatic mapping function is to automatically find the attribute name with the same name as the field name in lowercase, and call the setter method. After setting it to false, you need to clearly indicate the mapping relationship in
resultMap
before calling the corresponding setter method.
2. Basic function: Establish a mapping relationship between SQL query result fields and entity attributes
Example 1: Construct a domain model through setters
public class EStudent{ private long id; private String name; private int age; // getter,setter方法 /** * 必须提供一个无参数的构造函数 */ public EStudent(){} }
<select id="getStudent" resultMap="getStudentRM"> SELECT ID, Name, Age FROM TStudent </select> <resultMap id="getStudentRM" type="EStudnet"> <id property="id" column="ID"/> <result property="studentName" column="Name"/> <result property="studentAge" column="Age"/> </resultMap>
Sub-element description:
id element, used to set the mapping relationship between primary key fields and domain model attributes
result element, used Set the mapping relationship between ordinary fields and domain model attributes
id, result statement attribute configuration details:
Property | Description |
---|---|
Needs to be mapped to JavaBean Property name. | |
The column name or label alias of the data table. | |
A complete class name, or a type alias. If you match a JavaBean, MyBatis will usually detect it on its own. Then, if you want to map to a HashMap, then you need to specify the purpose of javaType. | |
List of types supported by the data table. This attribute is only useful for columns that allow nulls during insert, update, or delete. JDBC requires this, but MyBatis does not. If you are coding directly against JDBC and have columns that allow nulls, you will want to specify this. | |
Use this attribute to override the type handler. This value can be a complete class name or a type alias. |
The above is the detailed content of Detailed explanation of mybatis attributes. 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

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

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.

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

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

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
