1. 简单介绍

嗨,大家好,今天给想给大家分享一下关于Mybatis-plus 的 Service 层的一些方法的使用今天没有总结,因为都是一些API没有什么可以总结的,直接看着调用就可以了。
下边的连接也可以看到同样的内容: 这里地址就是带中文的

https://wnagzainote.yuque.com/books/share/46f28001-903f-4fb6-abdc-ecf9c2bf02bb?# 《MyBatis Plus 学习》

下面介绍怎样使用 IServer 提供的 saveOrUpdate 方法保存或更新数据到数据库

2. 接口说明

接口提供了如下四个 saveOrUpdate 方法:

// 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. 参数说明

  • entity:实体对象
  • updateWrapper:实体对象封装操作类 UpdateWrapper
  • entityList:实体对象集合
  • batchSize:插入批次数量

4. 实例代码

4.1 更新或新增单个实体

如果 ID 为 9991 的用户信息不存在,则新增用户信息;如果存在,则修改用户信息

import com.hxstrive.mybatis_plus.model.UserBean;
import com.hxstrive.mybatis_plus.service.UserService;
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.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
class SaveOrUpdate1Test {
 
    @Autowired
    private UserService userService;
 
    @Test
    void contextLoads() {
        UserBean entity = new UserBean(9991, "name-update", "男", 40);
        boolean flag = userService.saveOrUpdate(entity);
        System.out.println("flag=" + flag);
    }
 
}

4.2 根据 Wrapper 查询对象批量更新数据

@Test
void contextLoads() {
    UserBean entity = new UserBean();
    entity.setName("name-update");
 
    UpdateWrapper<UserBean> wrapper = new UpdateWrapper<>();
    wrapper.gt("user_id", 9992);
    wrapper.le("user_id", 9999);
 
    boolean flag = userService.saveOrUpdate(entity, wrapper);
    System.out.println("flag=" + flag);
}

4.3 批量插入或更新数据

@Test
void contextLoads() {
    List<UserBean> userBeanList = new ArrayList<>();
    userBeanList.add(new UserBean(9992, "username-update"));
    userBeanList.add(new UserBean(9993, "username-update"));
    userBeanList.add(new UserBean(9994, "username-update"));
    boolean flag = userService.saveOrUpdateBatch(userBeanList);
    System.out.println("flag=" + flag);
}

4.4 批量插入或更新数据,并且指定每个批次大小为 3

@Test
void contextLoads() {
    List<UserBean> userBeanList = new ArrayList<>();
    userBeanList.add(new UserBean(9992, "username-update"));
    userBeanList.add(new UserBean(9993, "username-update"));
    userBeanList.add(new UserBean(9994, "username-update"));
    userBeanList.add(new UserBean(9995, "username-update"));
    userBeanList.add(new UserBean(9996, "username-update"));
    userBeanList.add(new UserBean(9997, "username-update"));
    userBeanList.add(new UserBean(9998, "username-update"));
    userBeanList.add(new UserBean(9000, "username-save"));
    boolean flag = userService.saveOrUpdateBatch(userBeanList, 3);
    System.out.println("flag=" + flag);
}

5. saveOrUpdate(T entity) 方法的实现的原理

5.1 原理介绍

  1. 判断实体 @TableId 注解修饰的 ID 字段记录是否存在
  2. 如果不存在,则执行 save 方法,保存数据
  3. 如果存在,则执行 update 方法,更新数据

5.2 原生方法的实现

/**
 * TableId 注解存在更新记录,否插入一条记录
 *
 * @param entity 实体对象
 * @return boolean
 */
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveOrUpdate(T entity) {
    if (null != entity) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
        String keyProperty = tableInfo.getKeyProperty();
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
        Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
        return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
    }
    return false;
}

5.3 简单描述的实现逻辑

public void saveOrUpdate(T entity) {
  if(null == selectById(entity.getId())) {
    save(entity);
  } else {
    update(entity);
  }
}

5.4 注意事项说明

  1. 请注意,这里我们所描述的一切方法都是基于 Service 层来说的
  2. 请注意,这里我们所描述的一切方法都是不是基于 Mapper 层来说的
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐