Detailed explanation of Mybatis mapping files
This article brings you a detailed explanation of the Mybatis mapping file. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The real core of MyBatis is the mapping file. Because it is extremely powerful, if you compare it with JDBC code with the same function, you will find that it saves nearly 95% of the code.
MyBatis entity class mapping file
MyBatis has several top-level elements
select: mapping query statement
<select id="findAll" resultType="com.simple.mybatis.entitys.Employee"> select * from tal_employee </select>
insert: mapping insertion statement
Use #{parameter attribute name} to assign value
<insert id="saveEmp" parameterType="com.simple.mybatis.entitys.Employee"> insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender}) </insert>
: Mapping modification statement
<update id="updateEmp" parameterType="com.simple.mybatis.entitys.Employee"> update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id} </update>
: Mapping deletion statement
<delete id="deleteEmp" parameterType="Integer"> delete from tal_employee where id=#{id} </delete>
: SQL statement that can be reused by other SQL statements .
: It is the most complex and powerful element, used to map database tables and entity classes.
<resultMap type="com.simple.mybatis.entitys.Employee" id="Employee"> <id property="id" column="id"/> <result property="lastName" column="last_name"/> <result property="email" column="email"/> <result property="gender" column="gender"/></resultMap><!-- 返回外部resultMap格式的类型。--> <select id="findAll" resultMap="Employee"> select id,last_name AS lastName,email,gender from tal_employee </select>
: Cache configuration for the given namespace.
: References to other namespace cache configurations.
We combine Chapter 1 and use the previous method of interacting with the database:
sqlSession.selectList("命名空间.select标签ID"); sqlSession.selectOne("命名空间.select标签ID"); sqlSession.update("命名空间.update标签ID", "Object类型参数"); sqlSession.delete("命名空间.delete标签ID", "Object类型参数"); sqlSession.insert("命名空间.insert标签ID", "Object类型参数");
Additions, deletions and modifications must submit transactions: sqlSession.commit();
Use When executing a method using the XML mapping file method, since the method is called through a string, the type constraints are not mandatory and the readability is poor. This was the calling method of early ibatis. In addition, Mybatis also provides two other implementation methods
MyBatis annotation method
1. @Select: used to modify the method of using query statements
2.@Insert : Used to modify the method of adding statements,
3, @Update: Used to modify the method of modifying.
4. @Delete: used to modify the method of using delete statement.
public interface EmployeeAnnotation {
@Select("select id,last_name AS lastName,email,gender from tal_employee")
List
@Insert("insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender})") int saveEmp(Employee employee); @Update("update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}") boolean updateEmp(Employee employee); @Delete("elete from tal_employee where id=#{id}") long deleteEmp(Integer id); }
Then we need to configure the mybatis main configuration file Mapping this annotation interface
<!-- 映射注解类 --> <mapper class="com.simple.mybatis.dao.EmployeeAnnotation"/> @Test public void testAnnotation(){ SqlSession session = sqlSessionFactory.openSession(); EmployeeAnnotation employeeAnnotation = session.getMapper(EmployeeAnnotation.class); Employee employee = new Employee("测试注解增加", "email", "男"); employeeAnnotation.saveEmp(employee); //提交事务 session.commit(); session.close(); }
Although it is more convenient to use annotations, it is more complicated to configure complex sql statements. Therefore, a combination of the two methods is often used.
Mybatis interface programming
We first add an interface
public interface EmployeeMapper { List<Employee> findAll(); int saveEmp(Employee employee); boolean updateEmp(Employee employee); long deleteEmp(Integer id); }
The namespace (namespace) of the entity class mapping file must correspond to the full name of the interface, and the methods inside need to correspond to the additions in it Delete and modify the tag ID to complete the binding
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 该配置命名空间 --> <mapper namespace="com.simple.mybatis.dao.EmployeeMapper"> <resultMap type="com.simple.mybatis.entitys.Employee" id="Employee"> <id property="id" column="id"/> <result property="lastName" column="last_name"/> <result property="email" column="email"/> <result property="gender" column="gender"/> </resultMap> <!-- 返回外部resultMap格式的类型。--> <select id="findAll" resultMap="Employee"> select id,last_name AS lastName,email,gender from tal_employee </select> <insert id="saveEmp" parameterType="com.simple.mybatis.entitys.Employee"> insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender}) </insert> <update id="updateEmp" parameterType="com.simple.mybatis.entitys.Employee"> update tal_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id} </update> <delete id="deleteEmp" parameterType="Integer"> delete from tal_employee where id=#{id} </delete> </mapper>
And this mapping file must be loaded in the MyBatis main configuration file
<mappers> <mapper resource="com/simple/mybatis/entitys/EmployeeMapper.xml" /> </mappers>
Usage:
@Test public void test4(){ SqlSession session = sqlSessionFactory.openSession(); //获取接口。这个接口mybatis会帮我创建代理实现类完成接口与XML映射的绑定 EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class); employeeMapper.deleteEmp(1); session.commit(); session.close(); }
Get the automatically growing primary key after insertion
The useGeneratedKeys parameter only takes effect for the insert statement, and the default is false. When set to true, it means that if the inserted table has an auto-increment column as the primary key, JDBC is allowed to support automatic generation of the primary key, and the automatically generated primary key can be returned. keyProperty is to be encapsulated to the primary key ID attribute name in parameterType="com.simple.mybatis.entitys.Employee
<insert id="saveEmpGetKey" parameterType="com.simple.mybatis.entitys.Employee" useGeneratedKeys="true" keyProperty="id" > insert into tal_employee(last_name,email,gender) values(#{lastName},#{email},#{gender}) </insert> @Test public void saveEmpGetKey(){ SqlSession session = sqlSessionFactory.openSession(); EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class); Employee employee = new Employee("自动封装主键", "email", "男");//增加之前主键没赋值 employeeMapper.saveEmpGetKey(employee); session.commit();//提交事务 System.out.println("增加后主键有值: " + employee.getId()); session.close();//关闭资源 }
Mybatis parameter rules
When a parameter is used:
directly Just use #{xxx} to access the parameters. The parameter name is arbitrary. For example, #{a}, #{b} can access the parameter value. If it is a collection (Collection, List, Set) type, use the collection name in lowercase as the key value .
Required interface method:
long deleteEmp(Integer emp);
Mapping file:
<delete id="deleteEmp" parameterType="Integer"> delete from tal_employee where id=#{id} </delete>
You can see here that we have not assigned a value to this, #{id}My attribute name is also It is not called id but emp. So a parameter can have any name if it is the incoming collection type
The value when passing in the Map parameter:
//接口有方法:long deleteEmp(Map<String, Object> map); <delete id="deleteEmp" parameterType="Map"> delete from tal_employee where id=#{empID} </delete>
The value here is empID, which Just think about it and you know that you can create a map.put("empID", 1) as a parameter and pass it into the method call.
When there are multiple parameters:
Default: Just You cannot use #{xxx} to access parameters. When there are multiple parameters, mybatis will encapsulate the parameters into a map collection, and the value can only be obtained through #{subscript} or #{paramN}.
Named parameters: Manually specify the key of the map when explicitly specifying the encapsulation parameters. Specify the parameter name by adding @Param("key") to the interface method.
-
Entity class: If multiple parameters are encapsulated business classes, then just pass in the business object directly. You can get the attributes through #{property name}. Map: If multiple parameters are not encapsulated business classes , then just pass in the map collection. Get the corresponding value through #{key}.
{} and ${}
In MyBatis, in addition to using #{} to obtain the value of the parameter, you can also use ${} to obtain the value of the parameter.
Difference: #{}: The parameters will be generated into the sql statement in a pre-compiled manner. $ {}: will directly generate the value into the sql statement.
In most cases, we use #{} to obtain parameters, but in some places where placeholders are not supported, you can use ${ } to get parameters, such as table names.
resultType return type
For methods of addition, deletion and modification, Mybatis will automatically encapsulate the results and return int, Boolean, long All are OK.
For returning entity objects and List collections, resultType can be set to the entity type.
If a single object is returned, it can also be encapsulated For Map
, the resultType is set to map. If a Map collection object is returned, such as Map
, the resultType is set to the Student type, and the key value placed in the Map is modified through the @MapKey("id") annotation .
ResultMap标签介绍
ResultMap的常用子标签
id:映射主键。result:映射普通列。
association:复杂结果映射。
collection:复杂类型的集合映射。
constructor:构造函数注入。
前面我们的案例我们数据库的字段与实体类的属性基本上一致,或者不一致使用取别名方案解决,可是每条语句都去取别名是非常麻烦的,ResultMap这个标签就很好的解决数据库与实体类字段不对应的问题
我们新创建一个部门表:
CREATE TABLE tal_dept( d_id INT PRIMARY KEY AUTO_INCREMENT, d_name VARCHAR(50) );
对应实体类:
public class Dept { private Integer id; private Integer name; }
如果我们查询语句直接使用resultType="com.simple.mybatis.entitys.Dept那么毫无疑问我们的实体类中名字不对应是赋值为null的,我们可以ResultMap映射实体类与数据库的对应关系
<!--配置返回结果,配置数据库字段与类属性的映射--> <resultMap type="com.simple.mybatis.entitys.Dept" id="DeptResult"> <id column="d_id" property="id"/> <result column="d_name" property="name"/> </resultMap> <!--使用resultMap使用指向上面返回结果的ID--> <select id="getDeptOne" parameterType="Integer" resultMap="DeptResult"> select * from tal_dept where d_id = #{id} </select>
关系映射
在数据库中,许多数据是分布在多个表中的,有时候需要将多个表的数据关联起来进行查询。那么在ORM框架中,我们需要处理数据表的映射关系。
常见的映射关系:
关联属性映射association
映射collection映射
result方式映射属性
这种方式并不推荐,没有什么重用性
<resultMap type="com.simple.mybatis.entitys.Employee" id="EmployeeOrDept"> <id property="id" column="id"/> <result property="lastName" column="last_name"/> <result property="email" column="email"/> <result property="gender" column="gender"/> <!-- 指定Employee中Dept对象属性 --> <result property="dept.id" column="d_id"/> <result property="dept.name" column="d_name"/> </resultMap> <select id="getEmployeeOrDeptAll" resultMap="EmployeeOrDept"> select * from tal_employee e inner join tal_dept d on e.d_id=d.d_id </select>
association映射(分步查询)
多对一,查询员工获取员工中的部门:
<resultMap type="com.simple.mybatis.entitys.Employee" id="EmployeeOrDept"> <id property="id" column="id"/> <result property="lastName" column="last_name"/> <result property="email" column="email"/> <result property="gender" column="gender"/> <!-- property指定实体类中对象dept 指定发过去的参数column="d_id" select指定com.simple.mybatis.dao.DeptMapper映射文件中getDeptOne查询方法 --> <association property="dept" column="d_id" select="com.simple.mybatis.dao.DeptMapper.getDeptOne"></association> </resultMap> <!--resultMap指定使用上面定义的返回结果--> <select id="getEmployeeOrDeptAll" resultMap="EmployeeOrDept"> select * from tal_employee e inner join tal_dept d on e.d_id=d.d_id </select>
Collection查询
一对多,获取部门同时部门中员工也获取:
<!--部门映射--> <resultMap type="com.simple.mybatis.entitys.Dept" id="DeptResult"> <id column="d_id" property="id"/> <result column="d_name" property="name"/> <!--property指定Dept中集合属性名 ofType指定集合中属性类型 --> <collection property="Employees" ofType="com.simple.mybatis.entitys.Employee" > <id property="id" column="id"/> <result property="lastName" column="last_name"/> <result property="email" column="email"/> <result property="gender" column="gender"/> </collection> </resultMap> <select id="getDeptAll" resultMap="DeptResult"> select * from tal_dept d left join tal_Employee e on d.d_id = e.d_id </select>
Collection嵌套查询
一对多分布查询
<resultMap type="com.simple.mybatis.entitys.Dept" id="DeptOrEmployee"> <id column="d_id" property="id"/> <result column="d_name" property="name"/> <!--select指定com.simple.mybatis.dao.EmployeeMapper映射文件中的getEmployeeByDeptId--> <collection property="Employees" column="d_id" ofType="com.simple.mybatis.entitys.Employee" select="com.simple.mybatis.dao.EmployeeMapper.getEmployeeByDeptId"> </collection> </resultMap> <!--com.simple.mybatis.dao.EmployeeMapperXML中方法--> <select id="getEmployeeByDeptId" parameterType="Integer" resultType="com.simple.mybatis.entitys.Employee"> select * from tal_employee where d_id = #{id} </select>
关联查询和分步查询的区别:
关联查询一次将多个表的数据查询出来,分步查询通过多次查询获取查询结果。
配置文件不同,关联查询需要定义额外的映射,分步查询需要定义外键列,和查询的select方法。
关联查询不支持延迟加载,分步查询支持延迟加载。fetchType="lazy"
延迟加载
刚才分布查询是一次将结果查询出来,为了提高效率,mybatis还支持延迟加载技术,等需要用到对象时才进行查询。
在mybatis主配置文件中配置:
<!– 通过全局配置文件设置延迟加载--> <settings> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="false"/> </settings>
设置级联延迟加载
<!– 设置延迟加载属性--> <association fetchType="lazy"/>
The above is the detailed content of Detailed explanation of Mybatis mapping files. 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
