MyBatis 是一个优秀的持久层框架,它消除了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
MyBatis 简介
MyBatis 是一个半自动化的持久层框架,它将 SQL 映射和对象映射结合起来,使得开发者只需关注 SQL 语句和对象之间的映射关系,而无需编写繁琐的 JDBC 代码。MyBatis 旨在简化数据库操作,提高开发效率。
为什么选择 MyBatis?
- 简单易用:MyBatis 提供了简单的 XML 或注解配置,让开发者能够快速上手。
- 灵活配置:MyBatis 支持自定义 SQL 映射,满足各种复杂的查询需求。
- 支持缓存:MyBatis 支持一级缓存和二级缓存,提高查询效率。
- 支持插件:MyBatis 支持自定义插件,扩展功能。
搭建 MyBatis 环境
1. 添加依赖
首先,需要在项目中添加 MyBatis 的依赖。以下是一个简单的 Maven 依赖配置:
<dependencies>
<!-- MyBatis 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- MySQL 驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
2. 配置数据源
在 resources 目录下创建 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/mydb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射器 -->
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
3. 创建实体类和映射文件
在 com.example.mapper 包下创建 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="selectUser" resultType="com.example.entity.User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
在 com.example.entity 包下创建 User 实体类:
package com.example.entity;
public class User {
private Integer id;
private String name;
private String email;
// 省略 getter 和 setter 方法
}
使用 MyBatis 进行数据库操作
在 com.example.mapper 包下创建 UserMapper 接口:
package com.example.mapper;
public interface UserMapper {
User selectUser(Integer id);
}
在 com.example.mapper.impl 包下创建 UserMapperImpl 类,实现 UserMapper 接口:
package com.example.mapper.impl;
import com.example.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class UserMapperImpl implements UserMapper {
private final SqlSessionFactory sqlSessionFactory;
public UserMapperImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User selectUser(Integer id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
return session.selectOne("com.example.mapper.UserMapper.selectUser", id);
}
}
}
在 com.example.service 包下创建 UserService 类,使用 UserMapper 进行数据库操作:
package com.example.service;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.mapper.impl.UserMapperImpl;
public class UserService {
private final UserMapper userMapper;
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User getUser(Integer id) {
return userMapper.selectUser(id);
}
}
在 com.example.controller 包下创建 UserController 类,处理 HTTP 请求:
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUser(@PathVariable Integer id) {
return userService.getUser(id);
}
}
总结
通过以上步骤,我们已经成功搭建了一个基于 MyBatis 的 Java 开源框架,并实现了数据库查询功能。MyBatis 简化了数据库操作,提高了开发效率,是 Java 开发中常用的持久层框架之一。希望本文能帮助你快速上手 MyBatis。
