一、准备工作
1、在数据库中创建User表
![](https://i-blog.csdnimg.cn/direct/0e176575466f4b7a90ca3972d29a50a5.png)
2、创建MybatisPlus项目
(1)mapper层
package com.kuang.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kuang.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
(2)entity层
package com.kuang.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor //有参构造器
@NoArgsConstructor //无参构造器
public class User {
private Long Id;
private String Name;
private Integer Age;
private Integer Class;
}
(3)Service层和impl层
package com.kuang.sevice;
import com.baomidou.mybatisplus.extension.service.IService;
import com.kuang.entity.User;
import org.springframework.stereotype.Service;
@Service
public interface UserService extends IService<User> {
}
package com.kuang.sevice.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuang.mapper.UserMapper;
import com.kuang.entity.User;
import com.kuang.sevice.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{
}
(4)测试类
package com.kuang;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuang.mapper.UserMapper;
import com.kuang.entity.User;
import com.kuang.sevice.impl.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserServiceImpl userService;
//测试方法写在里面
}
(5)项目依赖
<?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>
<groupId>com.kuang</groupId>
<artifactId>mybatis_plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_plus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、查询功能
1、使用MybatisPlus实现
//通过Id查询用户
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
//根据id批量查询
@Test
public void selectBatchIds(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
}
//条件查询
@Test
public void selectByMap(){
HashMap<String,Object> map = new HashMap<>();
map.put("name","小文");
map.put("age",20);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
2、使用QueryWrapper实现
//按条件查询
@Test
public void testQuery(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.ne("name","开心") //不等于
.le("age",25) //小于
.orderByAsc("age"); //升序
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
//根据值 进行范围查询
@Test
public void test07(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.between("bj",1,2);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
//根据条件模糊查询
@Test
public void test08(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.like("age",5);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
三、删除功能
1、使用MybatisPlus实现
//单条数据删除 根据id
@Test
public void testDeleteById(){
userMapper.deleteById(4L);
}
//批量删除 根据id
@Test
public void testDeleteBatchId(){
userMapper.deleteBatchIds(Arrays.asList(1L,2L));
}
//通过map删除 条件删除 根据姓名
@Test
public void testdeleteByMap(){
Map<String, Object> map = new HashMap<>();
map.put("name","xiaotian");
userMapper.deleteByMap(map);
}
2、使用QueryWrapper实现
//根据条件判断来删除用户
@Test
public void test06(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.eq("id",14) //等于
.eq("bj",4);
int rows = userMapper.delete(queryWrapper);
System.out.println("影响的行数: "+rows);
}
四、新增功能
1、使用MybatisPlus实现
//批量新增用户 可新增单条数据和多条数据
@Test
public void testBatchInsert() {
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setName("us7");
user1.setAge(34);
user1.setBj(1);
userList.add(user1);
User user2 = new User();
user2.setName("us8");
user2.setAge(52);
user2.setBj(3);
userList.add(user2);
userService.saveBatch(userList);
}
五、修改功能
1、使用MybatisPlus实现
//根据id修改
@Test
public void test03() {
User user1 = new User();
user1.setId(12L);
user1.setName("帆帆");
user1.setAge(27);
user1.setBj(4);
userMapper.updateById(user1);
}
//根据name修改
@Test
public void test04(){
User user = new User();
user.setAge(42);
user.setBj(3);
userMapper.update(user,new UpdateWrapper<User>().eq("name","天天"));
}
2、使用QueryWrapper实现
//根据条件判断来修改用户信息
@Test
void test05() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.eq("id",13) //等于
.eq("name","us1");
User user = new User();
user.setAge(88);
user.setName("凡哥");
user.setBj(5);
int rows = userMapper.update(user,queryWrapper);
System.out.println("影响的行数: "+rows);
}