Spring Boot是一个用于创建独立的、基于Spring的生产级应用程序的框架,它简化了Spring应用的初始搭建以及开发过程。MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。将Spring Boot和MyBatis结合使用,可以高效地开发数据驱动的应用程序。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis - spring - boot - starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql - connector - java</artifactId>
<version>8.0.26</version>
</dependency>
spring.datasource.url = jdbc:mysql://localhost:3306/mydb?useSSL = false&serverTimezone = UTC
spring.datasource.username = root
spring.datasource.password = your_password
spring.datasource.driver - class - name = com.mysql.cj.jdbc.Driver
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb?useSSL = false&serverTimezone = UTC
username: root
password: your_password
driver - class - name: com.mysql.cj.jdbc.Driver
CREATE TABLE user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50),
password VARCHAR(50)
);
public class User {
private Integer id;
private String username;
private String password;
// 生成getter和setter方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Mapper
public interface UserMapper {
User selectUserById(Integer id);
int insertUser(User user);
int updateUser(User user);
int deleteUserById(Integer id);
}
<?xml version="1.0" encoding="UTF - 8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis - 3.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="selectUserById" resultMap="userResultMap">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insertUser">
INSERT INTO user (username, password) VALUES (#{username}, #{password})
</insert>
<update id="updateUser">
UPDATE user SET username = #{username}, password = #{password} WHERE id = #{id}
</update>
<delete id="deleteUserById">
DELETE FROM user WHERE id = #{id}
</delete>
<resultMap id="userResultMap" type="com.example.demo.entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
</mapper>
mybatis.mapper - locations = classpath:mapper/*.xml
mybatis:
mapper - locations: classpath:mapper/*.xml
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(Integer id) {
return userMapper.selectUserById(id);
}
public int addUser(User user) {
return userMapper.insertUser(user);
}
public int updateUserInfo(User user) {
return userMapper.updateUser(user);
}
public int deleteUser(int id) {
return userMapper.deleteUserById(id);
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Integer id) {
return userService.getUserById(id);
}
@PostMapping("/user")
public int addUser(@RequestBody User user) {
return userService.addUser(user);
}
@PutMapping("/user")
public int updateUser(@RequestBody User user) {
return userService.updateUserInfo(user);
}
@DeleteMapping("/user/{id}")
public int deleteUser(@PathVariable("id") Integer id) {
return userService.deleteUser(id);
}
}
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Transactional
public int addUser(User user) {
// 一些业务逻辑判断等操作
int result = userMapper.insertUser(user);
// 如果插入成功后还有其他操作,如更新相关联的数据等
return result;
}
}
<?xml version="1.0" encoding="UTF - 8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis - 3.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>
<!-- 其他SQL语句定义等 -->
</mapper>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper - spring - boot - starter</artifactId>
<version>1.3.0</version>
</dependency>
pagehelper:
helper - dialect: mysql
reasonable: true
support - methods - arguments: true
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getUsersByPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
return userMapper.selectAllUsers();
}
}
通过以上的配置和操作步骤,就可以在Spring Boot项目中成功地集成和使用MyBatis进行高效的数据持久化操作,无论是简单的CRUD操作还是涉及到高级功能如事务管理、缓存和插件的使用等。