MyBatis 是一个优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
MyBatis 简介
MyBatis 遵循约定优于配置( Convention Over Configuration)的原则,它允许你将数据库映射配置在 XML 文件中,这样可以让开发者更加关注业务逻辑的实现。MyBatis 通过半自动化的方式简化了数据库操作,提高了开发效率。
入门 MyBatis
1. 添加依赖
首先,你需要在项目的 pom.xml 文件中添加 MyBatis 的依赖:
<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>
2. 配置数据源
在 resources 目录下创建 mybatis-config.xml 文件,配置数据源、事务管理以及映射器:
<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?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 目录下创建 UserMapper.xml 文件,定义 SQL 映射:
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectById" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
4. 编写接口
在 com.example.mapper 包下创建 UserMapper 接口:
package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
User selectById(Long id);
}
5. 使用 MyBatis
在你的业务逻辑代码中,你可以这样使用 MyBatis:
public class UserService {
private final SqlSessionFactory sqlSessionFactory;
public UserService(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public User getUserById(Long id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
return mapper.selectById(id);
}
}
}
高效使用 MyBatis
1. 使用注解
MyBatis 支持使用注解来定义映射,这样你就不需要编写 XML 映射文件。以下是一个使用注解的例子:
package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
User selectById(Long id);
}
2. 使用动态 SQL
MyBatis 提供了动态 SQL 功能,可以让你在 XML 映射文件中编写可变的 SQL 语句。以下是一个使用动态 SQL 的例子:
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectByCondition" resultType="com.example.entity.User">
SELECT * FROM user
<where>
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
</mapper>
3. 使用缓存
MyBatis 支持两种类型的缓存:一级缓存和二级缓存。一级缓存默认开启,用于一个会话内的数据共享;二级缓存是跨会话的缓存,需要手动开启。
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
4. 性能优化
- 使用合理的 SQL 语句和索引。
- 尽量减少数据库连接和事务的开启次数。
- 使用批处理操作。
总结
MyBatis 是一个功能强大的 Java 开源框架,可以帮助你简化数据库操作。通过本文的介绍,你应该已经对 MyBatis 有了一个初步的了解。在实际开发中,你可以根据项目需求选择合适的配置和使用技巧,以提高开发效率和代码质量。
