MyBatis 是一款优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
MyBatis 入门
1. 环境搭建
要开始使用 MyBatis,首先需要在项目中引入 MyBatis 的依赖。以下是一个基本的 Maven 依赖配置:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
2. 配置文件
MyBatis 的配置文件 mybatis-config.xml 包含了整个 MyBatis 的运行环境信息以及运行时的一些参数。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
3. Mapper 文件
Mapper 文件是一个 XML 文件,其中包含 SQL 语句以及 MyBatis 的映射规则。
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
SELECT * FROM BLOG WHERE id = #{id}
</select>
</mapper>
4. 接口
MyBatis 使用接口和 XML 文件一起工作,接口中定义了方法,而 XML 文件中定义了 SQL 语句。
public interface BlogMapper {
Blog selectBlog(int id);
}
MyBatis 实战技巧
1. 动态 SQL
MyBatis 提供了动态 SQL 的能力,允许你在 XML 文件中使用 <if>, <choose>, <when>, <otherwise> 等标签来构建动态的 SQL 语句。
<select id="selectBlog" resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="title != null">
AND title = #{title}
</if>
<if test="author != null">
AND author = #{author}
</if>
</where>
</select>
2. 关联映射
MyBatis 支持关联映射,可以轻松地在 SQL 查询中处理多表关系。
<resultMap id="blogResultMap" type="Blog">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="author" column="author"/>
<association property="author" column="author_id" select="selectAuthor"/>
</resultMap>
3. 缓存机制
MyBatis 提供了两种缓存机制:一级缓存和二级缓存。
- 一级缓存:会话级别的缓存,当同一个会话中查询到数据后,会被存储在一级缓存中,后续的查询可以直接从缓存中获取数据。
- 二级缓存:全局级别的缓存,可以在不同的会话间共享数据。
4. 分页插件
MyBatis 支持使用分页插件来实现分页查询,常用的分页插件有 PageHelper 和 MyBatis-Page。
PageHelper.startPage(1, 10);
List<Blog> blogs = blogMapper.selectBlog();
5. 代码生成器
MyBatis 提供了代码生成器 MyBatis Generator,可以自动生成实体类、映射文件和接口。
<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.EqualsAndHashCodePlugin"/>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mydb"
userId="root"
password=""/>
<javaModelGenerator targetPackage="org.mybatis.example" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="org.mybatis.example" targetProject="src/main/resources"/>
<javaClientGenerator targetPackage="org.mybatis.example" targetProject="src/main/java" type="XMLMAPPER"/>
<table schema="mydb" tableName="BLOG"/>
</context>
</generatorConfiguration>
通过以上步骤,你可以快速入门 MyBatis 并将其应用到实际项目中。在实际使用过程中,还需要不断学习和积累实战经验,才能更好地发挥 MyBatis 的威力。
