Mybatis-Plus之CRUD操作
MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发,提高效率而生。
官网:baomidou官网(MyBatisPlus官网)
1、主要的依赖库
1.1 mybatis-plus依赖
注意:mybatis-plus的依赖和mybatis的依赖不能混用,不然版本不同的话,就会出现冲突。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
1.2 druid依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
1.3 Lombook插件
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
2、CRUD样例
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>maybatisplus_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maybatisplus_01_quickstart</name>
<description>maybatisplus_01_quickstart</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2 实体类User
package com.example.maybatisplus_01_quickstart.domain;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private String password;
private Integer age;
private String tel;
}
2.3 UserDao实现增删改查操作
package com.example.maybatisplus_01_quickstart.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.maybatisplus_01_quickstart.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<User> {
}
2.4 Test类
package com.example.maybatisplus_01_quickstart;
import com.example.maybatisplus_01_quickstart.dao.UserDao;
import com.example.maybatisplus_01_quickstart.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class test1 {
@Autowired
private UserDao userDao;
//新增操作
@Test
public void testSave() {
User user = new User();
user.setName("蛋糕");
user.setPassword("1888888");
user.setAge(22);
user.setTel("439923993");
userDao.insert(user);
}
//删除操作
@Test
public void testDelete() {
userDao.deleteById(1552215061978689537L);
}
//修改操作
@Test
public void testUpdate() {
User user = new User();
user.setId(1L);
user.setName("西红柿");
user.setPassword("wdefef");
userDao.updateById(user);
}
//通过ID查询
@Test
public void testGetById() {
User user = userDao.selectById(1L);
System.out.println(user);
}
//查询所有操作
@Test
public void contextLoads() {
List<User> userList = userDao.selectList(null);
System.out.println(userList);
}
}