【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus

在Spring Boot 3.0中,你可以使用MyBatis Plus来简化数据库操作。以下是一个基本的集成示例:

1.添加依赖到你的pom.xml:

<dependencies>

<!-- Spring Boot Starter -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<!-- MyBatis Plus Starter -->

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>3.x.x</version> <!-- 请使用最新版本 -->

</dependency>

<!-- 数据库驱动,以MySQL为例 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

</dependencies>

2.配置application.properties或application.yml:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

spring.datasource.username=root

spring.datasource.password=yourpassword

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis-plus.mapper-locations=classpath:/mappers/**/*.xml

mybatis-plus.type-aliases-package=com.yourpackage.model

3.创建实体类和Mapper接口:

// 实体类

@Data

public class User {

private Long id;

private String name;

private Integer age;

private String email;

}

// Mapper接口

@Mapper

public interface UserMapper extends BaseMapper<User> {

// 这里可以添加自定义方法

}

4.在Spring Boot启动类上添加@MapperScan注解:

@SpringBootApplication

@MapperScan("com.yourpackage.mapper")

public class YourApplication {

public static void main(String[] args) {

SpringApplication.run(YourApplication.class, args);

}

}

5.使用MyBatis Plus提供的服务进行操作:

@Service

public class UserService {

@Autowired

private UserMapper userMapper;

public boolean saveUser(User user) {

return userMapper.insert(user) > 0;

}

public List<User> getAllUsers() {

return userMapper.selectList(null);

}

}

以上代码展示了如何在Spring Boot 3.0项目中集成MyBatis Plus。你需要替换数据库连接信息、实体类、Mapper接口和你的应用包路径。这样,你就可以使用MyBatis Plus提供的方法来简化数据库操作。

相关推荐
豌豆花下猫1 小时前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
尚学教辅学习资料1 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
晚安里2 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
秋难降2 小时前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
上官浩仁2 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
Access开发易登软件3 小时前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发
叫我阿柒啊3 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
中国胖子风清扬3 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
bobz9654 小时前
分析 docker.service 和 docker.socket 这两个服务各自的作用
后端
野犬寒鸦4 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode