引言
在Java开发中,持久层(Persistence Layer)是不可或缺的一部分,它负责将对象数据持久化到数据库中。MyBatis作为一款优秀的持久层框架,以其简单易用、灵活强大的特点,深受开发者喜爱。本文将带你入门MyBatis,并通过实战案例,让你轻松掌握持久层开发技巧。
一、MyBatis简介
1.1 什么是MyBatis
MyBatis是一个支持定制化SQL、存储过程以及高级映射的持久层框架。它消除了几乎所有的JDBC代码和手动设置参数以及获取结果集的工作。
1.2 MyBatis的优势
- 简化数据库操作,提高开发效率
- 易于使用,灵活配置
- 高度可扩展,支持自定义插件
- 支持多种数据库
二、MyBatis入门
2.1 环境搭建
- 下载MyBatis官方文档:MyBatis官方文档
- 创建Maven项目,并添加依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
2.2 配置文件
- 创建
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/test?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
2.3 创建Mapper接口
- 创建
UserMapper.java接口,定义SQL语句
package com.example.mapper;
public interface UserMapper {
int insert(User user);
User selectById(int id);
}
2.4 创建Mapper XML文件
- 创建
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">
<insert id="insert" parameterType="User">
INSERT INTO user (name, age) VALUES (#{name}, #{age})
</insert>
<select id="selectById" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
三、MyBatis实战案例
3.1 添加用户
- 创建
User实体类
package com.example.entity;
public class User {
private int id;
private String name;
private int age;
// 省略getter和setter方法
}
- 编写Mapper接口和XML文件
package com.example.mapper;
public interface UserMapper {
int insert(User user);
}
<?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">
<insert id="insert" parameterType="User">
INSERT INTO user (name, age) VALUES (#{name}, #{age})
</insert>
</mapper>
- 编写Service层代码
package com.example.service;
import com.example.entity.User;
import com.example.mapper.UserMapper;
public class UserService {
private final UserMapper userMapper;
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
public void addUser(User user) {
userMapper.insert(user);
}
}
- 编写Controller层代码
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/user")
public User addUser(@RequestBody User user) {
userService.addUser(user);
return user;
}
}
3.2 查询用户
- 编写Mapper接口和XML文件
package com.example.mapper;
public interface UserMapper {
User selectById(int id);
}
<?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="User">
SELECT * FROM user WHERE id = #{id}
</select>
</mapper>
- 编写Service层代码
package com.example.service;
import com.example.entity.User;
import com.example.mapper.UserMapper;
public class UserService {
private final UserMapper userMapper;
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
public User getUserById(int id) {
return userMapper.selectById(id);
}
}
- 编写Controller层代码
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 getUserById(@PathVariable int id) {
return userService.getUserById(id);
}
}
四、总结
通过本文的介绍,相信你已经对MyBatis有了初步的了解。MyBatis以其简洁易用、灵活强大的特点,成为了Java开发中持久层开发的利器。希望本文能帮助你轻松掌握MyBatis的入门技巧,在今后的开发中更加得心应手。
