本文主要是mybatisplus和springboot的整合
- 导入依赖:
<?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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chao</groupId>
<artifactId>springboot-mybatisplus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatisplus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>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>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</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>
- 编写实体类:
package com.chao.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")//这个是把数据库中的表和自己写的实体类相对应起来,如果不写那么你的实体类的类名就必须和数据库中的一致
public class User {
private Integer id;
private String username;
private String password;
private String name;
private Integer age;
private String email;
}
- 编写接口:
package com.chao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chao.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
- 配置文件:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
- 编写测试类:
package com.chao;
import com.chao.mapper.UserMapper;
import com.chao.pojo.User;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void contextLoads() {
List<User> users = userMapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
}
- 结果:
插入数据演示
测试:
@Test
public void testInsert() {
User user = new User();
user.setAge(16);
user.setEmail("1@com");
user.setUsername("caocao");
user.setName("曹操");
user.setPassword("123");
int result = userMapper.insert(user);//返回影响的行数
System.out.println(user.getId());//插入后id会回填到User对象中
}
User类中加入一个注解:@TableId
注解@TableField
它主要被用于三种情况。一是当对象属性名和字段名不一样的时。二是当数据库没有该属性名时。三是当不想查出某个属性名时。
举例说明:
- 属性名和字段名不一样
- 数据库没有该属性名
- 不想查出某个属性名时
根据id进行更新
@Test
public void testUpdateById() {
User user = new User();
user.setId(1);
user.setPassword("555");
user.setAge(37);
int i = userMapper.updateById(user);
System.out.println(i);
}
根据条件更新两种方法
- 常规操作QueryWrapper
@Test
public void testUpdate01() {
User user = new User();
user.setAge(33);
user.setPassword("99999");
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username","zhangsan");//根据条件更新,指定数据库字段名为username并且值为zhangsan的数据进行更新
int result = userMapper.update(user, queryWrapper);
System.out.println(result);
}
- 用UpdateWrapper
@Test
public void testUpdate02() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("username","zhangsan")
.set("age",22).set("password","555555");
int result = userMapper.update(null, updateWrapper);
System.out.println(result);
}
删除的集中方法
- 根据deleteByMap
@Test
public void testDeleteMap() {
Map<String,Object> map = new HashMap<>();
map.put("username","zhangsan");
map.put("password","11111");
//map集合含多个条件,条件之间用and连接,根据多条件删除
int result = userMapper.deleteByMap(map);
System.out.println(result);
}
结果:
- 直接根据id删除deleteId
- 根据delete
@Test
public void testDelete() {
//用法一:
/* QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username","caocao")
.eq("password",123);*/
//用法二:
User user = new User();
user.setUsername("caocao");
user.setPassword("123");
QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
int result = userMapper.delete(queryWrapper);
System.out.println(result);
}
结果:
4. 根据deleteBatchIds批量删除
@Test
public void testDeleteBatchIds() {
//根据id批量删除
int result = userMapper.deleteBatchIds(Arrays.asList(7, 8));
System.out.println(result);
}
结果:
查询操作
- selectById
@Test
public void testSelectById() {
System.out.println(userMapper.selectById(0));
}
结果:
2. selectBatchIds(批量查询)
@Test
public void testSelectBatchIds() {
System.out.println(userMapper.selectBatchIds(Arrays.asList(1,2)));
}
- selectOne
@Test
public void testSelectOne() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("username","zhangsan");
//查询的数据超过一条时,会抛出异常
System.out.println(userMapper.selectOne(wrapper));
}
- SelectCount根据条件查询得到记录数
@Test
public void testSelectCount() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age",17)
.ge("age",28);
userMapper.selectCount(wrapper);
}
结果:
5. selectList
@Test
public void testSelectList() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("email","cn");
System.out.println(userMapper.selectList(wrapper));
}
结果:
分页查询
- 先写一个分页的配置类配置插件
- 测试
@Test
public void testSelectPage() {
Page<User> page = new Page<>(1,1);
//把所以的数据先查出来然后把这些数据分页
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("email","cn");
//分页
IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
System.out.println("数据总条数"+iPage.getTotal());
System.out.println("数据总页数"+iPage.getPages());
System.out.println("当前页"+ iPage.getCurrent());
//获得值
List<User> records = iPage.getRecords();
for (User record : records) {
System.out.println(record);
}
结果: