本文将以最简单的方式带你学会Spring Boot整合Mybatis Plus实现基本的CRUD,冲冲冲!
Mybatis Plus,简称MP,官网https://mp.baomidou.com/
在MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
意愿
:成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
<!--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>
server:
port: 8099
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 12345
url: jdbc:mysql://127.0.0.1:3306/test
type: com.alibaba.druid.pool.DruidDataSource
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;
实体类:
public class Student {
private Integer id;
private String name;
private Integer age;
//构造方法......
//Getter、Setter......
}
首先将Mapper层的接口集成BaseMapper,其中T就是需要操作的实体类泛型
。
@Mapper
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}
接口
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);
}
实现类
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public int saveStudent(Student student) {
return 0;
}
@Override
public Student findStudentById(int id) {
return null;
}
@Override
public List<Student> findAllStudent() {
return null;
}
@Override
public int deleteStudentById(int id) {
return 0;
}
@Override
public int updateStudentById(Student student) {
return 0;
}
@Override
public List<Student> findStudentByFirstName(String firstName) {
return null;
}
}
下面我们来一一的实现这些方法
@Override
public int saveStudent(Student student) {
return studentMapper.insert(student);
}
测试:
@Autowired
private StudentService studentService;
@Test
void testSaveStudent() {
Student student = new Student(1,"张三", 23);
System.out.println(studentService.saveStudent(student));
}
结果:
@Override
public Student findStudentById(int id) {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.eq("id", id);
return studentMapper.selectOne(wrapper);
}
测试
@Test
void testFindStuById() {
System.out.println(studentService.findStudentById(1));
}
结果:
在查找全部之前我们先多插入一点
@Override
public List<Student> findAllStudent() {
return studentMapper.selectList(new QueryWrapper<>());
}
测试
@Test
void testFindAllStu(){
System.out.println(studentService.findAllStudent());
}
结果:
@Override
public int deleteStudentById(int id) {
return studentMapper.deleteById(id);
}
测试
@Test
void testRemoveStudent(){
System.out.println(studentService.deleteStudentById(1));
}
结果
@Override
public int updateStudentById(Student student) {
return studentMapper.updateById(student);
}
测试
@Test
void testUpdateStu(){
System.out.println(studentService.updateStudentById(new Student(2,"张小四",200)));
}
结果
找出全部姓张的人
@Override
public List<Student> findStudentByFirstName(String firstName) {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.likeRight("name", firstName);
return studentMapper.selectList(wrapper);
}
测试
@Test
void testLikeFind(){
System.out.println(studentService.findStudentByFirstName("张"));
}
结果
@TableId(type = IdType.AUTO)
private Integer id;
//@TableName("数据库表名")
@TableName("student")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField("name")
private String name;
@TableField("age")
private Integer age;
@TableId(value = "id", type = IdType.AUTO) //只能有一个@TableId
private Integer id;
@TableField("name")
private String name;
@TableField("age")
private Integer age;
测试下:
////////////// Service////////////
@Override
public int saveStudent(Student student) {
int result = studentMapper.insert(student);
System.out.println(student);
return result;
}
////////////// Test //////////////
@Test
void testSaveStudent() {
Student student = new Student( "张四", 23);
System.out.println(studentService.saveStudent(student));
}