MyBatis 是一款优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
MyBatis 入门
什么是MyBatis?
MyBatis 是一个半自动的持久层框架。它允许你只编写 SQL 语句,而不需要自己处理连接、事务和结果集的映射。MyBatis 遵循约定优于配置的原则,通过配置文件来管理数据库连接和映射,使得代码更加简洁。
MyBatis 的核心组件
- SqlSessionFactory:生产 SqlSession 的工厂类,SqlSession 是 MyBatis 的核心接口,负责管理事务和执行 SQL 命令。
- SqlSession:它包含了面向数据库执行 SQL 命令所需的所有方法。每个线程都应该有一个 SqlSession 实例。
- Executor:执行器,负责执行 SQL 命令,并处理结果集。
- MappedStatement:映射 SQL 命令、参数和结果集的映射关系。
- SqlSource:负责读取 SQL 语句,并生成 MappedStatement。
MyBatis 的基本使用
- 添加依赖:在你的项目中添加 MyBatis 的依赖。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
- 配置 MyBatis:创建
mybatis-config.xml文件,配置数据源、事务管理等。
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
</configuration>
- 创建 Mapper 接口:定义 Mapper 接口,声明 SQL 命令。
public interface UserMapper {
List<User> findAll();
}
- 创建 Mapper XML:定义 SQL 命令和结果集映射。
<mapper namespace="com.example.mapper.UserMapper">
<select id="findAll" resultType="com.example.entity.User">
SELECT * FROM user
</select>
</mapper>
- 执行 SQL 命令:使用 SqlSession 执行 SQL 命令。
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryBuilder.build(new FileInputStream("mybatis-config.xml"));
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.findAll();
// 处理结果集
}
MyBatis 进阶
动态 SQL
MyBatis 支持动态 SQL,可以根据条件动态地拼接 SQL 语句。
<select id="findUsersByCondition" resultType="com.example.entity.User">
SELECT * FROM user
<where>
<if test="name != null">
name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
缓存
MyBatis 支持一级缓存和二级缓存,可以减少数据库的访问次数,提高性能。
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
扩展
MyBatis 还支持扩展,例如插件、注解等。
实战案例
以下是一个使用 MyBatis 查询用户信息的简单示例。
- 实体类:定义 User 实体类。
public class User {
private Integer id;
private String name;
private Integer age;
// 省略getter和setter方法
}
- Mapper 接口:定义 UserMapper 接口。
public interface UserMapper {
List<User> findUsers();
}
- Mapper XML:定义查询语句。
<mapper namespace="com.example.mapper.UserMapper">
<select id="findUsers" resultType="com.example.entity.User">
SELECT * FROM user
</select>
</mapper>
- 执行查询:使用 SqlSession 执行查询。
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryBuilder.build(new FileInputStream("mybatis-config.xml"));
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.findUsers();
// 处理结果集
}
通过以上步骤,你可以轻松地使用 MyBatis 实现持久层操作。MyBatis 提供了丰富的功能和特性,可以帮助你快速开发高效、可靠的 Java 应用程序。
