Spring Boot 中使用 Mybatis Plus的操作方法
投稿:mrr
本文介绍了如何在 Spring Boot 项目中集成 Mybatis Plus,Spring Boot 与 MyBatis Plus 的集成非常简单,通过自动配置和简洁的 API,可以大大减少开发中常见的数据库操作代码,需要的朋友参考下吧
Spring Boot 中使用 Mybatis Plus
在现代的企业级开发中,MyBatis Plus 是 MyBatis 的增强工具,它简化了很多常见的数据库操作。通过 Spring Boot 集成 MyBatis Plus,可以快速构建高效、简洁的数据库操作层。本文将介绍如何在 Spring Boot 项目中集成 MyBatis Plus。
1. 添加 Maven 依赖
在Spring Boot 项目的 pom.xml
文件中添加如下相关的依赖:
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.7</version> </dependency>
2. 在配置文件中加入相关配置
spring: datasource: url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull username: root password: xxxxxx hikari: # 数据库连接池 minimum-idle: 5 # 连接池最小空闲连接数 maximum-pool-size: 20 # 连接池最大连接数 auto-commit: true # 自动提交从连接池中返回的连接 idle-timeout: 30000 # 连接允许在连接池中闲置的最长时间 pool-name: SpringBootDemo-HikariCP # 连接池的用户定义名称 max-lifetime: 1800000 # 连接池中连接最长生命周期 connection-timeout: 30000 # 等待来自连接池的连接的最大毫秒数 connection-test-query: SELECT 1 # 连接池连接测试语句
3. 创建 Mybatis 配置类
// 扫描 Mapper 所在包路径 @MapperScan("com.xxxx.xxx") @Configuration public class MybatisPlusConfig { /** * 添加 Mybatis Plus 分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
4. 创建实体类
public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; @TableLogic private boolean isDeleted; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public boolean isDeleted() { return isDeleted; } public void setDeleted(boolean deleted) { isDeleted = deleted; } }
5.创建Mapper
MyBatis Plus 提供了基础的 Mapper 接口,继承它即可拥有常用的增、删、改、查功能。
public interface UserMapper extends BaseMapper<User> { }
6. 创建 Service
Mybatis Plus 的 ServiceImpl 是实现了 IService 接口的抽象类, 对 Mapper 进行了增强封装
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
Service接口层
public interface UserService extends IService<User> { }
7. 结论
本文介绍了如何在 Spring Boot 项目中集成 Mybatis Plus,Spring Boot 与 MyBatis Plus 的集成非常简单,通过自动配置和简洁的 API,可以大大减少开发中常见的数据库操作代码。MyBatis Plus 提供了很多实用的功能,如分页查询、条件构造、自动填充等,能大大提高开发效率。
到此这篇关于Spring Boot 中使用 Mybatis Plus的文章就介绍到这了,更多相关Spring Boot使用 Mybatis Plus内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- 5分钟快速搭建SpringBoot3 + MyBatis-Plus工程/项目的实现示例
- 解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题
- Spring Boot 中整合 MyBatis-Plus详细步骤(最新推荐)
- Spring Boot 集成 MyBatis 全面讲解(最新推荐)
- SpringBoot同时集成Mybatis和Mybatis-plus框架
- Springboot使用MybatisPlus实现mysql乐观锁
- 浅谈Spring Boot、MyBatis、MyBatis-Plus 依赖版本对应关系
- Spring Boot Mybatis++ 2025详解