MybatisPlus之新增操作并返回主键ID

在应用mybatisplus持久层框架的项目中,经常遇到执行新增操作后需要获取主键ID的场景,下面将分析及测试过程记录分享出来。

1、MybatisPlus新增方法

在这里插入图片描述

持久层新增方法源码如下:

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);
    ...
}

业务层新增方法源码如下:

public interface IService<T> {
    int DEFAULT_BATCH_SIZE = 1000;

    default boolean save(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().insert(entity));
    }

    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveBatch(Collection<T> entityList) {
        return this.saveBatch(entityList, 1000);
    }

    boolean saveBatch(Collection<T> entityList, int batchSize);

    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveOrUpdateBatch(Collection<T> entityList) {
        return this.saveOrUpdateBatch(entityList, 1000);
    }

    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

	...
}

2、表结构

DROP TABLE IF EXISTS t_user;

CREATE TABLE `t_user` (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `name` varchar(30) NOT NULL DEFAULT '' COMMENT '姓名',
  `age` int(11) NULL DEFAULT NULL COMMENT '年龄',
  `gender` tinyint(2) NOT NULL DEFAULT 0 COMMENT '性别,0:女 1:男',
  PRIMARY KEY (`id`)
) COMMENT = '用户表';

3、实体类

@Data
@TableName("t_user")
public class User {
    /**
     * 主键 ID, @TableId 注解定义字段为表的主键,type 表示主键类型,IdType.AUTO 表示随着数据库 ID 自增
     */
    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 姓名
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 性别
     */
    private Integer gender;
}

@TableName 表名注解,标识实体类对应的表。

  • 当实体类名称和实际表名一致时,如实体名为 User, 表名为 user ,可不用添加该注解,Mybatis Plus 会自动识别并映射到该表。
  • 当实体类名称和实际表名不一致时,如实体名为 User, 表名为 t_user,需手动添加该注解,并填写实际表名称。

@TableId 主键注解,声明实体类中的主键对应的字段。

在这里插入图片描述

IdType 主键类型

在这里插入图片描述

4、新增操作

Mybatis Plus 对 Mapper 层和 Service 层都将常见的增删改查操作都封装好了,只需简单的继承,即可轻松搞定对数据的增删改查,此次分析新增数据这块。

4.1、mapper层

定义一个 UserMapper , 让其继承 BaseMapper,如下

public interface UserMapper extends BaseMapper<User> {
}

BaseMapper 提供的新增方法仅一个 insert() 方法,如下

User user = new User();
user.setName("犬小哈");
user.setAge(30);
user.setGender(1);

userMapper.insert(user);

// 获取插入数据的主键 ID
Long id = user.getId();

System.out.println("id:" + id);

4.2、service层

Mybatis Plus 同样也封装了通用的 Service 层 CRUD 操作,并且提供了更丰富的方法。

public interface UserService extends IService<User> {
}

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

先定义 UserService 接口 ,让其继承自 IService,再定义实现类 UserServiceImpl,让其继承自 ServiceImpl, 同时实现 UserService 接口,这样就可以让 UserService 拥有了基础通用的 CRUD 功能。
与 Mapper 层不同的是,Service 层的新增方法均以 save 开头,并且功能更丰富,方法如下:
// 新增数据
sava(T) : boolean
// 伪批量插入,实际上是通过 for 循环一条一条的插入
savaBatch(Collection) : boolean
// 伪批量插入,int 表示批量提交数,默认为 1000
savaBatch(Collection, int) : boolean
// 新增或更新(单条数据)
saveOrUpdate(T) : boolean
// 批量新增或更新
saveOrUpdateBatch(Collection) : boolean
// 批量新增或更新(可指定批量提交数)
saveOrUpdateBatch(Collection, int) : boolean

4.2.1、sava(T)

// 新增数据
// 实际执行 SQL : INSERT INTO user ( name, age, gender ) VALUES ( '曹操 ', 30, 1 )
User user = new User();
user.setName("曹操 ");
user.setAge(30);
user.setGender(1);

boolean isSuccess = userService.save(user);

// 返回主键ID
Long id = user.getId();
System.out.println("isSuccess:" + isSuccess);
System.out.println("主键 ID: " + id);

4.2.2、savaBatch(Collection)

伪批量插入,注意,命名虽然包含了批量的意思,但这不是真的批量插入,不信的话,可以实际测试一下:

// 批量插入
List<User> users = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    User user = new User();
    user.setName("曹操" + i);
    user.setAge(i);
    user.setGender(1);
    users.add(user);
}
boolean isSuccess = userService.saveBatch(users);
System.out.println("isSuccess:" + isSuccess);

Mybatis Plus 这个伪批量插入性能其实好些,内部会将每次的插入语句缓存起来,等到达到 1000 条的时候,才会统一推给数据库,虽然最终在数据库那边还是一条一条的执行 INSERT,但还是在和数据库交互的 IO 上做了优化。

4.2.3、savaBatch(Collection, int)

多了个 batchSize 参数,可以手动指定批处理的大小,即多少 SQL 操作执行一次,默认为 1000。

4.2.4、saveOrUpdate(T)

保存或者更新。即当你需要执行的数据,数据库中不存在时,就执行插入操作,如下:

// 实际执行 SQL : INSERT INTO user ( name, age, gender ) VALUES ( '曹操', 60, 1 )
User user = new User();
user.setName("曹操");
user.setAge(60);
user.setGender(1);
userService.saveOrUpdate(user);

当你需要执行的数据,数据库中已存在时,就执行更新操作。框架是如何判断该记录是否存在呢? 如设置了主键 ID,因为主键 ID 必须是唯一的,Mybatis Plus 会先执行查询操作,判断数据是否存在,存在即执行更新,否则,执行插入操作,如下:

User user = new User();
// 设置了主键字段
user.setId(1L);
user.setName("曹操");
user.setAge(60);
user.setGender(1);
userService.saveOrUpdate(user);

若是需要根据其他字段判断数据是否存在,可以通过对选定字段添加唯一索引的方式实现。
通过on udplicate key update 的方式实现批量的插入或更新,这种方式需要有唯一索引,通过唯一索引去判断是否冲突,有冲突就会更新,没有冲突就会插入数据。

4.2.5、saveOrUpdateBatch(Collection)

批量保存或者更新,示例代码如下:

List<User> users = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    User user = new User();
    user.setId(Long.valueOf(i));
    user.setName("曹操" + i);
    user.setAge(i+1);
    user.setGender(1);
    users.add(user);
}
userService.saveOrUpdateBatch(users);

4.2.6、saveOrUpdateBatch(Collection, int)

批量保存或者更新(可手动指定批量大小),示例代码如下:

List<User> users = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    User user = new User();
    user.setId(Long.valueOf(i));
    user.setName("曹操" + i);
    user.setAge(i+1);
    user.setGender(1);
    users.add(user);
}
userService.saveOrUpdateBatch(users, 100);

猜你喜欢

转载自blog.csdn.net/leijie0322/article/details/134295953