Spring Boot整合Mybatis Plus
本文将以最简单的方式带你学会Spring Boot整合Mybatis Plus实现基本的CRUD,冲冲冲!
一、简单介绍
Mybatis Plus,简称MP,官网https://mp.baomidou.com/
在MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
意愿:成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
二、环境准备和基本配置
1.创建Spring Boot项目,引入依赖
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 整合mybatis plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!-- test依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>2.修改配置文件
server
port8099
spring
datasource
driver-class-namecom.mysql.jdbc.Driver
usernameroot
password12345
urljdbcmysql//127.0.0.13306/test
typecom.alibaba.druid.pool.DruidDataSource3.数据库准备
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;4.项目结构和实体类准备
![590.png)] Spring Boot整合Mybatis Plus[极简教程]_mysql](https://file.cfanz.cn/uploads/png/2022/05/24/11/9526TcZP54.png)
实体类:
public class Student {
private Integer id;
private String name;
private Integer age;
//构造方法......
//Getter、Setter......
}三、CRUD操作
1.继承BaseMapper
首先将Mapper层的接口集成BaseMapper,其中T就是需要操作的实体类泛型。
public interface StudentMapper extends BaseMapper<Student> {
}2.创建Service层接口和实现类
接口
public interface StudentService {
/**
* 保存Student
*
* @param student
* @return
*/
int saveStudent(Student student);
/**
* 根据id查找
*
* @param id
* @return
*/
Student findStudentById(int id);
/**
* 查找全部
*
* @return
*/
List<Student> fingAllStudnet();
/**
* 删除一个
*
* @param id
* @return
*/
int deleteStudentById(int id);
/**
* 修改一个
*
* @param student
* @return
*/
int updateStudentById(Student student);
/**
* 根据姓模糊查询
*
* @param firstName
* @return
*/
List<Student> findStudentByFirstName(Student firstName);
}实现类
public class StudentServiceImpl implements StudentService {
private StudentMapper studentMapper;
public int saveStudent(Student student) {
return 0;
}
public Student findStudentById(int id) {
return null;
}
public List<Student> findAllStudent() {
return null;
}
public int deleteStudentById(int id) {
return 0;
}
public int updateStudentById(Student student) {
return 0;
}
public List<Student> findStudentByFirstName(String firstName) {
return null;
}
}3.方法的实现
下面我们来一一的实现这些方法
(1)保存一个
public int saveStudent(Student student) {
return studentMapper.insert(student);
}测试:
private StudentService studentService;
void testSaveStudent() {
Student student = new Student(1,"张三", 23);
System.out.println(studentService.saveStudent(student));
}结果:
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_spring boot_02](https://file.cfanz.cn/uploads/png/2022/05/24/11/dY3HRc2117.png)
(2)根据id查找
public Student findStudentById(int id) {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.eq("id", id);
return studentMapper.selectOne(wrapper);
}测试
void testFindStuById() {
System.out.println(studentService.findStudentById(1));
}结果:
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_ide_03](https://file.cfanz.cn/uploads/png/2022/05/24/11/S1GaLed8T9.png)
(3)查找全部
在查找全部之前我们先多插入一点
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_ide_04](https://file.cfanz.cn/uploads/png/2022/05/24/11/L40ddW2adF.png)
public List<Student> findAllStudent() {
return studentMapper.selectList(new QueryWrapper<>());
}测试
void testFindAllStu(){
System.out.println(studentService.findAllStudent());
}结果:
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_java_05](https://file.cfanz.cn/uploads/png/2022/05/24/11/bX47Oc27EF.png)
(4)删除一个
public int deleteStudentById(int id) {
return studentMapper.deleteById(id);
}测试
void testRemoveStudent(){
System.out.println(studentService.deleteStudentById(1));
}结果
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_ide_06](https://file.cfanz.cn/uploads/png/2022/05/24/11/U3D563W75a.png)
(5)修改一个
public int updateStudentById(Student student) {
return studentMapper.updateById(student);
}测试
void testUpdateStu(){
System.out.println(studentService.updateStudentById(new Student(2,"张小四",200)));
}结果
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_mysql_07](https://file.cfanz.cn/uploads/png/2022/05/24/11/3a0NCfKRH4.png)
(6)根据姓模糊查询
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_spring boot_08](https://file.cfanz.cn/uploads/png/2022/05/24/11/e18Zc5ab23.png)
找出全部姓张的人
public List<Student> findStudentByFirstName(String firstName) {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.likeRight("name", firstName);
return studentMapper.selectList(wrapper);
}测试
void testLikeFind(){
System.out.println(studentService.findStudentByFirstName("张"));
}结果
![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_java_09](https://file.cfanz.cn/uploads/png/2022/05/24/11/035B24dE1e.png)
四、可能遇到的问题
1.让主键自增
(type = IdType.AUTO)
private Integer id;2.实体类和表名不一致
//@TableName("数据库表名")
("student")3.实体类属性和表的字段名不一致
(value = "id", type = IdType.AUTO)
private Integer id;
("name")
private String name;
("age")
private Integer age;4.插入数据后让自增id回写(很实用)
(value = "id", type = IdType.AUTO) //只能有一个@TableId
private Integer id;
("name")
private String name;
("age")
private Integer age;测试下:
// Service
public int saveStudent(Student student) {
int result = studentMapper.insert(student);
System.out.println(student);
return result;
}
// Test //
void testSaveStudent() {
Student student = new Student( "张四", 23);
System.out.println(studentService.saveStudent(student));
}![在这里插入图片描述 Spring Boot整合Mybatis Plus[极简教程]_spring boot_10](https://file.cfanz.cn/uploads/png/2022/05/24/11/40b2RR51B3.png)









