引言
MyBatis 是一个优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects)映射成数据库中的记录。
入门指南
1. MyBatis 简介
MyBatis 允许你采用 SQL 映射语言来配置和构建存储过程,将接口和 Java 的 POJOs 映射成数据库中的记录。它支持自定义 SQL、存储过程以及高级映射。
2. 环境搭建
a. 添加依赖
在 Maven 项目中,添加以下依赖到 pom.xml 文件:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
b. 配置文件
创建 mybatis-config.xml 文件,配置数据源、事务管理以及映射器:
<?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/mybatis_db?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
3. 编写 Mapper 接口和 XML 映射文件
a. Mapper 接口
创建 UserMapper.java 接口,定义 SQL 映射语句:
package com.example.mapper;
import com.example.entity.User;
public interface UserMapper {
User getUserById(Integer id);
}
b. XML 映射文件
创建 UserMapper.xml 文件,配置 SQL 映射语句:
<?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.example.mapper.UserMapper">
<select id="getUserById" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
实战技巧
1. 动态 SQL
MyBatis 提供了动态 SQL 功能,可以根据条件动态构建 SQL 语句。
<select id="getUserByCondition" resultType="com.example.entity.User">
SELECT * FROM user
<where>
<if test="id != null">
AND id = #{id}
</if>
<if test="username != null">
AND username = #{username}
</if>
</where>
</select>
2. 分页查询
MyBatis 支持分页查询,可以通过 <foreach> 和 <choose> 标签实现。
<select id="getUserByPage" resultType="com.example.entity.User">
SELECT * FROM user
<where>
<choose>
<when test="id != null">
AND id = #{id}
</when>
<otherwise>
AND username = #{username}
</otherwise>
</choose>
</where>
LIMIT #{offset}, #{limit}
</select>
3. 关联查询
MyBatis 支持关联查询,可以通过 <resultMap> 标签实现多表关联。
<resultMap id="userResultMap" type="com.example.entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<association property="role" column="role_id" select="getRoleById"/>
</resultMap>
<select id="getUserById" resultMap="userResultMap">
SELECT * FROM user WHERE id = #{id}
</select>
<select id="getRoleById" resultType="com.example.entity.Role">
SELECT * FROM role WHERE id = #{id}
</select>
项目应用详解
1. MyBatis 与 Spring 集成
MyBatis 可以与 Spring 框架集成,通过 Spring 配置文件或注解的方式实现。
a. 配置文件方式
在 applicationContext.xml 文件中配置 MyBatis 和 Spring 集成:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
b. 注解方式
使用 @Mapper 注解标注 Mapper 接口,使用 @Transactional 注解控制事务。
@Mapper
public interface UserMapper {
// ...
}
@Transactional
public void updateUser(User user) {
// ...
}
2. MyBatis 与其他框架集成
MyBatis 还可以与其他框架集成,如 Hibernate、MyBatis-Plus 等。
a. MyBatis 与 Hibernate 集成
通过配置 hibernate-mybatis 插件,可以实现 MyBatis 与 Hibernate 的集成。
<plugin interceptor="org.mybatis.caches.hibernate.HibernateCachePlugin"/>
b. MyBatis 与 MyBatis-Plus 集成
MyBatis-Plus 是 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.3</version>
</dependency>
总结
MyBatis 是一个优秀的持久层框架,它可以帮助开发者简化数据库操作,提高开发效率。通过本文的介绍,相信你已经对 MyBatis 有了一定的了解。在实际开发中,你可以根据项目需求选择合适的集成方式,充分发挥 MyBatis 的优势。
