SpringBoot 集成 Mybatis-Plus:快速上手指南
SpringBoot 集成 Mybatis-Plus:快速上手指南
Mybatis-Plus 是 Mybatis 的增强工具,可以方便地进行 CRUD 操作,提高了开发效率。本文将介绍如何在 SpringBoot 中集成 Mybatis-Plus,并通过示例代码演示其基本使用。
一、创建 SpringBoot 项目
首先我们需要创建一个 SpringBoot 项目,可以使用 IDEA 或者 https://start.spring.io/ 进行创建。这里我们使用 IDEA 进行创建。
选择 Spring Initializr 创建一个新项目,选择 Web、MySql、Mybatis、Lombok、DevTools、Mybatis-Plus 依赖,如下图所示:
[图片示例]
点击 Next 按钮,输入 Group、Artifact、Name,点击 Finish 按钮即可创建项目。
二、添加配置文件
- application.yml
在 src/main/resources 目录下创建 application.yml 文件,并添加以下配置:
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.example.demo.entity
其中,配置了数据源的驱动、url、用户名、密码,以及 Mybatis-Plus 的 mapper 文件路径和实体类扫描路径。
- MybatisPlusConfig.java
在 src/main/java/com/example/demo/config/ 目录下创建 MybatisPlusConfig.java 文件,并添加以下配置:
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setLimit(500);
return paginationInterceptor;
}
}
其中,@MapperScan 注解指定了 Mybatis-Plus 的 Mapper 接口扫描路径,@Bean 注解创建了 Mybatis-Plus 的分页插件 PaginationInterceptor。
三、创建实体类和 Mapper 接口
- 创建实体类
在 src/main/java/com/example/demo/entity/ 目录下创建 User.java 实体类,并添加以下代码:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
其中,@Data 注解添加了 getter、setter、equals、hashCode、toString 方法,@AllArgsConstructor 和 @NoArgsConstructor 注解分别添加了有参和无参构造方法。
- 创建 Mapper 接口
在 src/main/java/com/example/demo/mapper/ 目录下创建 UserMapper.java 接口,并添加以下代码:
public interface UserMapper extends BaseMapper<User> {}
其中,继承了 Mybatis-Plus 的 BaseMapper 接口,可以直接使用 Mybatis-Plus 提供的 CRUD 方法。
- 创建 Mapper XML 文件
在 src/main/resources/mapper/ 目录下创建 UserMapper.xml 文件,并添加以下代码:
<?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.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
id, name, age, email
</sql>
<select id="selectById" resultMap="BaseResultMap">
select <include refid="Base_Column_List" /> from user where id=#{id}
</select>
<select id="selectList" resultMap="BaseResultMap">
select <include refid="Base_Column_List" /> from user
</select>
<insert id="insert" parameterType="com.example.demo.entity.User">
insert into user (name, age, email) values (#{name}, #{age}, #{email})
</insert>
<update id="updateById" parameterType="com.example.demo.entity.User">
update user set name=#{name}, age=#{age}, email=#{email} where id=#{id}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
delete from user where id=#{id}
</delete>
</mapper>
其中,定义了查询、插入、更新、删除等 SQL 语句。
四、测试
- 创建 Controller
在 src/main/java/com/example/demo/controller/ 目录下创建 UserController.java 文件,并添加以下代码:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
return userMapper.selectById(id);
}
@GetMapping("/")
public List<User> getList() {
return userMapper.selectList(null);
}
@PostMapping("/")
public String add(@RequestBody User user) {
userMapper.insert(user);
return "success";
}
@PutMapping("/")
public String update(@RequestBody User user) {
userMapper.updateById(user);
return "success";
}
@DeleteMapping("/{id}")
public String deleteById(@PathVariable Long id) {
userMapper.deleteById(id);
return "success";
}
}
其中,使用了 @Autowired 注解自动注入了 UserMapper 接口。
- 启动项目并测试
启动项目,并使用 Postman 或者浏览器进行测试。
-
查询所有用户:
GET http://localhost:8080/user/
-
查询指定用户:
GET http://localhost:8080/user/1
-
添加用户:
POST http://localhost:8080/user/
Body:
{ "name": "test", "age": 18, "email": "test@example.com" } -
更新用户:
PUT http://localhost:8080/user/
Body:
{ "id": 1, "name": "test", "age": 18, "email": "test@example.com" } -
删除用户:
DELETE http://localhost:8080/user/1
至此,SpringBoot 集成 Mybatis-Plus 完成。
更多 Mybatis-Plus 功能和示例代码,请参考官方文档: https://mp.baomidou.com/
原文地址: https://www.cveoy.top/t/topic/lEjV 著作权归作者所有。请勿转载和采集!