MyBatis 是一个优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
MyBatis 简介
MyBatis 允许你使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs 映射成数据库中的记录。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程,让你能够更加专注于 SQL 本身。
MyBatis 的核心组件
- SqlSessionFactory:SqlSessionFactory 是 MyBatis 的核心接口,它负责创建 SqlSession。
- SqlSession:SqlSession 是 MyBatis 的核心对象,它包含了执行 SQL 命令所需的所有方法。
- Executor:Executor 是 MyBatis 的核心执行器,它负责执行 SQL 命令。
- MappedStatement:MappedStatement 是 MyBatis 的核心映射对象,它包含了 SQL 语句、参数映射和结果映射等信息。
MyBatis 入门
环境搭建
- 添加依赖:在项目的 pom.xml 文件中添加 MyBatis 的依赖。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
- 配置文件:创建 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/mybatis_db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/example/EmployeeMapper.xml"/>
</mappers>
</configuration>
- Mapper 文件:创建 EmployeeMapper.xml 文件,定义 SQL 语句。
<mapper namespace="com.mybatis.example.Employee">
<select id="selectEmployee" resultType="com.mybatis.example.Employee">
SELECT * FROM employee WHERE id = #{id}
</select>
</mapper>
- 接口文件:创建 EmployeeMapper 接口。
package com.mybatis.example;
public interface Employee {
Employee selectEmployee(int id);
}
- 测试:编写测试代码,调用 EmployeeMapper 接口。
package com.mybatis.example;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisTest {
public static void main(String[] args) {
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build("mybatis-config.xml");
SqlSession sqlSession = sqlSessionFactory.openSession();
Employee employee = sqlSession.selectOne("com.mybatis.example.Employee.selectEmployee", 1);
System.out.println(employee);
sqlSession.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
MyBatis 实战
动态 SQL
MyBatis 提供了动态 SQL 功能,可以让你根据条件动态构建 SQL 语句。
<mapper namespace="com.mybatis.example.Employee">
<select id="selectEmployeeByCondition" resultType="com.mybatis.example.Employee">
SELECT * FROM employee
<where>
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
</mapper>
关联查询
MyBatis 支持关联查询,可以让你方便地查询关联数据。
<mapper namespace="com.mybatis.example.Employee">
<resultMap id="employeeResultMap" type="com.mybatis.example.Employee">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="department" column="department_id" javaType="com.mybatis.example.Department">
<id property="id" column="id"/>
<result property="name" column="name"/>
</association>
</resultMap>
<select id="selectEmployeeWithDepartment" resultMap="employeeResultMap">
SELECT e.*, d.* FROM employee e
INNER JOIN department d ON e.department_id = d.id
WHERE e.id = #{id}
</select>
</mapper>
缓存
MyBatis 提供了缓存机制,可以让你缓存查询结果,提高查询效率。
<mapper namespace="com.mybatis.example.Employee">
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
</mapper>
总结
MyBatis 是一个优秀的持久层框架,它可以帮助你简化数据库操作,提高开发效率。通过本文的介绍,相信你已经对 MyBatis 有了一定的了解。希望你在实际项目中能够灵活运用 MyBatis,提高你的开发效率。
