MyBatis简介
MyBatis是一款优秀的持久层框架,它消除了几乎所有的JDBC代码和手动设置参数以及获取结果集的工作。MyBatis可以通过简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
入门指南
1. 环境搭建
1.1 准备开发环境
- Java开发工具包(JDK)
- 安装IDE(如IntelliJ IDEA、Eclipse等)
- 安装数据库(如MySQL)
1.2 创建Maven项目
<dependencies>
<!-- MyBatis核心依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- 数据库连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!-- 数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
2. 配置MyBatis
2.1 创建SqlMapConfig.xml
在src目录下创建SqlMapConfig.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="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
2.2 创建UserMapper.xml
在src目录下创建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="selectById" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
3. 编写Mapper接口
在对应的包下创建UserMapper接口,定义对应的操作。
package com.example.mapper;
public interface UserMapper {
User selectById(Integer id);
}
4. 使用MyBatis
4.1 创建SqlSessionFactory
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
4.2 创建SqlSession
SqlSession session = sqlSessionFactory.openSession();
User user = session.selectOne("com.example.mapper.UserMapper.selectById", 1);
session.close();
进阶应用
1. 动态SQL
MyBatis提供了强大的动态SQL功能,可以实现对SQL语句的灵活配置。
1.1 if标签
<select id="selectByCondition" resultType="User">
SELECT * FROM user
<where>
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
1.2 choose、when、otherwise标签
<select id="selectByCondition" resultType="User">
SELECT * FROM user
<choose>
<when test="name != null">
WHERE name = #{name}
</when>
<otherwise>
WHERE age = #{age}
</otherwise>
</choose>
</select>
2. 一对一、一对多关联
MyBatis支持一对一、一对多关联映射。
2.1 一对一关联
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" javaType="Address">
<id property="id" column="address_id"/>
<result property="province" column="province"/>
<result property="city" column="city"/>
</association>
</resultMap>
<select id="selectById" resultMap="userMap">
SELECT u.*, a.* FROM user u LEFT JOIN address a ON u.id = a.user_id WHERE u.id = #{id}
</select>
</mapper>
2.2 一对多关联
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="userMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<collection property="orders" ofType="Order">
<id property="id" column="order_id"/>
<result property="name" column="order_name"/>
</collection>
</resultMap>
<select id="selectById" resultMap="userMap">
SELECT u.*, o.* FROM user u LEFT JOIN order o ON u.id = o.user_id WHERE u.id = #{id}
</select>
</mapper>
实战案例
1. 用户管理系统
1.1 功能模块
- 用户信息管理
- 用户权限管理
- 用户登录/登出
1.2 技术栈
- Spring Boot
- MyBatis
- MySQL
- Thymeleaf
2. 图书管理系统
2.1 功能模块
- 图书信息管理
- 用户信息管理
- 借阅管理
2.2 技术栈
- Spring Boot
- MyBatis
- MySQL
- Layui
总结
通过本文的介绍,相信你已经对MyBatis有了基本的了解。在实际项目中,MyBatis可以帮助你快速开发高效的Java应用。希望本文对你有所帮助,祝你学习愉快!
