微服务 教程
1085个小节阅读:195.4k
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
接下来我们在SpringBoot项目中使用MyBatisPlus
创建SpringBoot项目,添加MyBatisPlus起步依赖
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- MyBatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
在SpringBoot配置文件中配置数据源
xxxxxxxxxx
# 数据源
spring
datasource
driver-class-name com.mysql.cj.jdbc.Driver
url jdbc mysql ///school?serverTimezone=UTC
username root
password root
编写实体类
xxxxxxxxxx
public class Student {
private Integer id;
private String name;
private String email;
private String gender;
private Integer age;
}
编写Mapper接口,继承BaseMapper
xxxxxxxxxx
public interface StudentMapper extends BaseMapper<Student> {
}
在 SpringBoot启动类中添加 @MapperScan
注解,扫描Mapper文件夹
xxxxxxxxxx
"com.itbaizhan.mpdemo2.mapper") (
public class SpringbootMpApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMpApplication.class, args);
}
}
测试Mapper方法
xxxxxxxxxx
public class StudentMapperTest {
private StudentMapper studentMapper;
public void testFindById(){
Student student = studentMapper.selectById(1);
System.out.println(student);
}
}
1. 在MyBatisPlus中,SpringBoot通过扫描Mapper文件夹
A @Mapper
B @ComponentScan
C @MapperScan
D @Component
2. 在MyBatisPlus中,Mapper接口需要继承
A Mapper
B BaseMapper
C CommonMapper
D MyMapper
1=>C 2=>B