🍀 前言
博客地址:
👋 简介
上一章我们简单的学习了下CRUD的操作,本章将分别详细介绍下mapper和service两个接口的新增操作的常用方法。
📖 正文
1 前置准备
本章节将以角色表来进行操作
创建一个测试类
package com.power.mpdemo;
import com.power.mpdemo.mapper.RoleMapper;
import com.power.mpdemo.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author power
* @time 2023/12/21 15:25
* @Description
*/
@SpringBootTest
public class RoleTests {
@Autowired
private RoleMapper roleMapper;
@Autowired
private IRoleService roleService;
}
2 Mapper接口
// 插入一条记录
int insert(T entity);
因为mapper接口新增的方法只有这一个,并且上一章已经介绍过了,这里就不详细介绍
3 Service接口
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量),伪批量插入,实际上是通过 for 循环一条一条的插入
boolean saveBatch(Collection<T> entityList);
// 插入(批量),伪批量插入,int 表示批量提交数,默认为 1000
boolean saveBatch(Collection<T> entityList, int batchSize);
// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入(可指定批量提交数)
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
3.1 saveBatch
需求:批量新增1个admin,3个测试角色
@Test
public void saveBatch() {
List<Role> list = new ArrayList<>();
list.add(Role.builder().roleCode("ADMIN").roleName("超级管理员").build());
for (int i = 1; i < 4; i++) {
list.add(Role.builder().roleCode("TEST0" + i).roleName("测试角色" + i + "号").build());
}
boolean b = roleService.saveBatch(list);
System.out.println("批量新增接:" + b);
}
// 批量新增结果:true
实际执行的SQL
INSERT INTO tb_role ( role_name, role_code ) VALUES ( '超级管理员', 'ADMIN' )
INSERT INTO tb_role ( role_name, role_code ) VALUES ( '测试角色1号', 'TEST01' )
INSERT INTO tb_role ( role_name, role_code ) VALUES ( '测试角色2号', 'TEST02' )
INSERT INTO tb_role ( role_name, role_code ) VALUES ( '测试角色3号', 'TEST03' )
通过查看saveBatch(Collection<T> entityList)
的源码,我们发现,其实它调用了saveBatch(Collection<T> entityList, int batchSize)
这个方法,batchSize参数默认传入了1000,表示多少sql执行一次。
3.2 saveOrUpdate
保存或更新:当数据库不存在的时候就插入,存在的就更新
需求:新增数据,不设置主键
@Test
public void saveOrUpdate(){
Role role = Role.builder().roleCode("TEST05").roleName("测试角色5号").build();
boolean b = roleService.saveOrUpdate(role);
System.out.println("新增或更新结果:" + b);
}
// 新增或更新结果:true
实际执行的SQL表示,执行了INSERT INTO
的操作
INSERT INTO tb_role ( role_name, role_code ) VALUES ( '测试角色5号', 'TEST05' )
再次执行saveOrUpdate
操作,这次设置主键id
@Test
public void saveOrUpdate(){
Role role = Role.builder().id(5L).roleCode("ABC").roleName("测试角色ABC").build();
boolean b = roleService.saveOrUpdate(role);
System.out.println("新增或更新结果:" + b);
}
SQL执行结果表示先通过id先查询,如果查询到了数据,那么就会执行UPDATE
更新的操作
SELECT id,role_name,role_code,description,create_time,update_time,is_deleted FROM tb_role WHERE id=5
UPDATE tb_role SET role_name='测试角色ABC', role_code='ABC' WHERE id=5
如果指定了id在数据库中不存在,那么还是会执行添加的操作
@Test
public void saveOrUpdate(){
Role role = Role.builder().id(6L).roleCode("TEST06").roleName("测试角色6号").build();
boolean b = roleService.saveOrUpdate(role);
System.out.println("新增或更新结果:" + b);
}
SELECT id,role_name,role_code,description,create_time,update_time,is_deleted FROM tb_role WHERE id=6
INSERT INTO tb_role ( id, role_name, role_code ) VALUES ( 6, '测试角色6号', 'TEST06' )
备注:saveOrUpdateBatch
的方法与saveBatch
的使用操作一样,这里就不继续演示了