Table of Contents
1. resultMap
association联合" >association联合
collection聚集" >collection聚集
Home Database Mysql Tutorial Detailed explanation of mybatis attributes

Detailed explanation of mybatis attributes

Jun 11, 2018 pm 11:17 PM
mybatis

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 

  1. Get the ResultSet object through JDBC query

  2. 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

  3. According to the type attribute of the ResultMap tag Instantiate the domain model through reflection

  4. 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(){}
}
Copy after login
<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>
Copy after login

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:

##propertyNeeds to be mapped to JavaBean Property name. columnThe column name or label alias of the data table. javaTypeA 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. jdbcType 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. typeHandlerUse this attribute to override the type handler. This value can be a complete class name or a type alias.

示例2:通过构造函数构造领域模型

<select id="getStudent" resultMap="getStudentRM">
  SELECT ID, Name, Age
    FROM TStudent</select><resultMap id="getStudentRM" type="EStudnet">  <constructor>    <idArg column="ID" javaType="_long"/>    <arg column="Name" javaType="String"/>    <arg column="Age" javaType="_int"/>  </constructor></resultMap>
Copy after login

子元素说明:

  • constructor元素 ,指定使用指定参数列表的构造函数来实例化领域模型。注意:其子元素顺序必须与参数列表顺序对应

  • idArg子元素 ,标记该入参为主键

  • arg子元素 ,标记该入参为普通字段(主键使用该子元素设置也是可以的)

3、一对一关系、一对多关系查询

 注意:在采用嵌套结果的方式查询一对一、一对多关系时,必须要通过resultMap下的id或result标签来显式设置属性/字段映射关系,否则在查询多条记录时会仅仅返回最后一条记录的情况。
Copy after login

association联合

联合元素用来处理“一对一”的关系。需要指定映射的Java实体类的属性,属性的javaType(通常MyBatis 自己会识别)。对应的数据库表的列名称。如果想覆写的话返回结果的值,需要指定typeHandler。 
不同情况需要告诉MyBatis 如何加载一个联合。MyBatis 可以用两种方式加载:

  • select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;

  • resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

例如,一个班级对应一个班主任。 
首先定义好班级中的班主任 private TeacherEntity teacherEntity;

使用select实现联合 
例:班级实体类中有班主任的属性,通过联合在得到一个班级实体时,同时映射出班主任实体。 
这样可以直接复用在TeacherMapper.xml文件中定义好的查询teacher根据其ID的select语句。而且不需要修改写好的SQL语句,只需要直接修改resultMap即可。

ClassMapper.xml文件部分内容:

<resultMap type="ClassEntity" id="classResultMap">  
    <id property="classID" column="CLASS_ID" />  
    <result property="className" column="CLASS_NAME" />  
    <result property="classYear" column="CLASS_YEAR" />  
    <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>  
</resultMap>  
<select id="getClassByID" parameterType="String" resultMap="classResultMap">  
    SELECT * FROM CLASS_TBL CT  
    WHERE CT.CLASS_ID = #{classID};  
</select>
Copy after login

TeacherMapper.xml文件部分内容:

<resultMap type="TeacherEntity" id="teacherResultMap">  
    <id property="teacherID" column="TEACHER_ID" />  
    <result property="teacherName" column="TEACHER_NAME" />  
    <result property="teacherSex" column="TEACHER_SEX" />  
    <result property="teacherBirthday" column="TEACHER_BIRTHDAY"/>  
    <result property="workDate" column="WORK_DATE"/>  
    <result property="professional" column="PROFESSIONAL"/>  
</resultMap>  
<select id="getTeacher" parameterType="String"  resultMap="teacherResultMap">  
    SELECT *  
      FROM TEACHER_TBL TT  
     WHERE TT.TEACHER_ID = #{teacherID}  
</select>
Copy after login

使用resultMap实现联合 
与上面同样的功能,查询班级,同时查询器班主任。需在association中添加resultMap(在teacher的xml文件中定义好的),新写sql(查询班级表left join教师表),不需要teacher的select。

修改ClassMapper.xml文件部分内容:

<resultMap type="ClassEntity" id="classResultMap">  
    <id property="classID" column="CLASS_ID" />  
    <result property="className" column="CLASS_NAME" />  
    <result property="classYear" column="CLASS_YEAR" />  
    <association property="teacherEntity" column="TEACHER_ID"  resultMap="teacherResultMap"/>  
</resultMap>  
<select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap">  
    SELECT *  
      FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID  
     WHERE CT.CLASS_ID = #{classID};  
</select>
Copy after login

其中的teacherResultMap请见上面TeacherMapper.xml文件部分内容中。

collection聚集

聚集元素用来处理“一对多”的关系。需要指定映射的Java实体类的属性,属性的javaType(一般为ArrayList);列表中对象的类型ofType(Java实体类);对应的数据库表的列名称; 
不同情况需要告诉MyBatis 如何加载一个聚集。MyBatis 可以用两种方式加载: 
1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活; 
2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

例如,一个班级有多个学生。 
首先定义班级中的学生列表属性:private List<StudentEntity> studentList;

使用select实现聚集 
用法和联合很类似,区别在于,这是一对多,所以一般映射过来的都是列表。所以这里需要定义javaType为ArrayList,还需要定义列表中对象的类型ofType,以及必须设置的select的语句名称(需要注意的是,这里的查询student的select语句条件必须是外键classID)。

ClassMapper.xml文件部分内容:

<resultMap type="ClassEntity" id="classResultMap">  
    <id property="classID" column="CLASS_ID" />  
    <result property="className" column="CLASS_NAME" />  
    <result property="classYear" column="CLASS_YEAR" />  
    <association property="teacherEntity" column="TEACHER_ID"  select="getTeacher"/>  
    <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/>  
</resultMap>  
<select id="getClassByID" parameterType="String" resultMap="classResultMap">  
    SELECT * FROM CLASS_TBL CT  
    WHERE CT.CLASS_ID = #{classID};  
</select>
Copy after login

StudentMapper.xml文件部分内容:

<!-- java属性,数据库表字段之间的映射定义 -->  
<resultMap type="StudentEntity" id="studentResultMap">  
    <id property="studentID" column="STUDENT_ID" />  
    <result property="studentName" column="STUDENT_NAME" />  
    <result property="studentSex" column="STUDENT_SEX" />  
    <result property="studentBirthday" column="STUDENT_BIRTHDAY" />  
</resultMap>  
<!-- 查询学生list,根据班级id -->  
<select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap">  
    <include refid="selectStudentAll" />  
    WHERE ST.CLASS_ID = #{classID}  
</select>
Copy after login

使用resultMap实现聚集 
使用resultMap,就需要重写一个sql,left join学生表。

<resultMap type="ClassEntity" id="classResultMap">  
    <id property="classID" column="CLASS_ID" />  
    <result property="className" column="CLASS_NAME" />  
    <result property="classYear" column="CLASS_YEAR" />  
    <association property="teacherEntity" column="TEACHER_ID"  resultMap="teacherResultMap"/>  
    <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/>  
</resultMap>  
<select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap">  
    SELECT *  
      FROM CLASS_TBL CT  
           LEFT JOIN STUDENT_TBL ST  
              ON CT.CLASS_ID = ST.CLASS_ID  
           LEFT JOIN TEACHER_TBL TT  
              ON CT.TEACHER_ID = TT.TEACHER_ID  
      WHERE CT.CLASS_ID = #{classID};  
</select>
Copy after login

其中的teacherResultMap请见上面TeacherMapper.xml文件部分内容中。studentResultMap请见上面StudentMapper.xml文件部分内容中。

4. 动态映射关系 
通过 discriminator子元素 (鉴别器)可以实现动态映射关系信息的设置。具体示例如下:

public class EStudent{  private long id;  private String name;  private String juniorHighSchool;  private String seniorHighSchool;  private int during; // 在本校就读时间  // getter,setter方法  /**
   * 必须提供一个无参数的构造函数
   */  public EStudent(){}
}
Copy after login

情景:查询学生信息的seniorHighSchool信息,若就读时间during字段值为4、5、6时,则以juniorHighSchool字段作所为seniorHighSchool信息。

<select id="getStundent" resultMap="rm">
  SELECT ID, Name, JuniorHighSchool, SeniorHighSchool, during
    FROM TStudent</select><resultMap id="rm" type="EStudent">
  // 若不加这句,则当将juniorHighSchool赋予给seniorHighSchool属性时,juniorHighSchool属性将为null  <result column="juniorHighSchool" property="juniorHighSchool"/>  <discriminator column="during" javaType="_int">
    // 形式1:通过resultType设置动态映射信息    <case value="4" resultType="EStudent">      <result column="juniorHighSchool" property="seniorHighSchool"/>    </case>
   // 形式2: 通过resultMap设置动态映射信息   <case value="5" resultMap="dynamicRM"/>   <case value="6" resultMap="dynamicRM"/>  </discriminator></resultMap><resultMap id="dynamicRM" type="EStudent">  <result column="juniorHighSchool" property="seniorHighSchool"/></resultMap>
Copy after login

注意:上面关于 discriminator子元素 的 case元素 的 resultType属性 和 resultMap元素 的 type属性 ,均不是直指返回的领域模型类型,而是指定根据判断条件后得到映射关系,可通过 id子元素 和 result子元素 重写映射关系。

5. id元素,result元素,idArg元素,arg元素,discriminator元素的共同属性

  • javaType属性 :Java类的全限定名,或别名

  • jdbcType属性 :JDBC类型, JDBC类型为CUD操作时列可能为空时进行处理

  • typeHandler属性 :指定类型处理器的全限定类名或类型别名

  • column属性 :指定SQL查询结果的字段名或字段别名。将用于JDBC的 resultSet.getString(columnName)

  • This article explains the detailed explanation of mybatis attributes. For more related content, please pay attention to the PHP Chinese website.

  • Related recommendations:

  • MySQL database multi-table operation

  • MySQL database single table query

  • Oracle database output input

Property Description

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iBatis vs. MyBatis: Which one is better for you? iBatis vs. MyBatis: Which one is better for you? Feb 19, 2024 pm 04:38 PM

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

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

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

Comparative analysis of the functions and performance of JPA and MyBatis Comparative analysis of the functions and performance of JPA and MyBatis Feb 19, 2024 pm 05:43 PM

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

Various ways to implement batch deletion operations in MyBatis Various ways to implement batch deletion operations in MyBatis Feb 19, 2024 pm 07:31 PM

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 Detailed explanation of how to use MyBatis batch delete statements Feb 20, 2024 am 08:31 AM

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? Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? Feb 23, 2024 pm 08:13 PM

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 cache mechanism: understand the cache storage principle in one article Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Feb 23, 2024 pm 04:09 PM

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

Analyze the caching mechanism of MyBatis: compare the characteristics and usage of first-level cache and second-level cache Analyze the caching mechanism of MyBatis: compare the characteristics and usage of first-level cache and second-level cache Feb 25, 2024 pm 12:30 PM

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

See all articles