在mybatis-plus中ID生成策略主要是注解@TableId(type=IdType,XXXX)来生成,主要有以下类别

public enum IdType {

AUTO(0), //数据库 ID 自增

NONE(1), //无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)

INPUT(2), //insert 前自行 set 主键值

ASSIGN_ID(3), //自定义实现可以通过接口IdentifierGenerator的方法nextId来实现,默认是实现类DefaultIdentifierGenerator

ASSIGN_UUID(4); //分配 UUID

}

这里主要介绍一下自定义ID生成

1.生成CustomIdGenerator类

public class CustomIdGenerator implements IdentifierGenerator {

@Override

public Number nextId(Object entity) {

Date date=new Date();

System.out.println( date);

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

String id=format.format( date) ;

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

return Long.parseLong(id);

}

}

2.在Configuration类生成Bean

@Configuration

public class MybatisPlusConfig {

@Bean

public CustomIdGenerator customIdGenerator(){

return new CustomIdGenerator();

}

3.在实体类别指ID.Type为ASSIGN_ID

@Data

public class User extends Model<User> {

private Long id;

@TableId(type = IdType.ASSIGN_ID)

private String specid;

private String name;

private Integer age;

private String email;

private int deleted;

@TableField(fill = FieldFill.INSERT_UPDATE)

private LocalDateTime createtime;

@Version

private long version;

private String fab;

}

4.准备测试方法

@Test

public void testSave(){

for(long i=25;i<26;i++) {

User user = new User();

user.setId(400+i);

user.setAge((int)i+30);

user.setEmail("zoulinlin"+i+"@163.com");

user.setName("zouli"+i);

int row = userMapper.insert(user);

System.out.println(row);

}

}

运行log:

Wed Oct 12 15:01:36 GMT+08:00 2022

generator id:20221012150136

insertFill

2022-10-12 15:01:39.725 DEBUG 19684 --- [ main] c.m.service.mapper.UserMapper.insert : ==> Preparing: INSERT INTO user (specid, id, name, age, email, deleted, createtime, version, FAB) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'FAB1')

2022-10-12 15:01:39.772 DEBUG 19684 --- [ main] c.m.service.mapper.UserMapper.insert : ==> Parameters: 20221012150136(String), 425(Long), zouli25(String), 55(Integer), zoulinlin25@163.com(String), 0(Integer), 2022-10-12T15:01:36.866(LocalDateTime), 0(Long)

Consume Time:79 ms 2022-10-12 15:01:39

Execute SQL:INSERT INTO user (specid, id, name, age, email, deleted, createtime, version, FAB) VALUES ('20221012150136', 425, 'zouli25', 55, 'zoulinlin25@163.com', 0, '2022-10-12T15:01:36.866', 0, 'FAB1')

从上面可以看出我们生成的ID只能是数据值,那么如何我们想生成字符串的如何进行?

1.首先我们可以看一下接口IdentifierGenerator代码,里面有一个nextUUID方法,其实是给IdType.ASSIGN_UUID使用

public interface IdentifierGenerator {

default boolean assignId(Object idValue) {

return StringUtils.checkValNull(idValue);

}

Number nextId(Object entity);

default String nextUUID(Object entity) {

return IdWorker.get32UUID();

}

}

2.根据1中的理解在自定义ID生成类把nextUUID方法覆盖

public class CustomIdGenerator implements IdentifierGenerator {

@Override

public Number nextId(Object entity) {

Date date=new Date();

System.out.println( date);

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

String id=format.format( date) ;

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

return Long.parseLong(id);

}

@Override

public String nextUUID(Object entity) {

Date date=new Date();

System.out.println( date);

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

String id=format.format( date) ;

return "lot-"+id;

}

}

3.在实体中把id类型修改为ASSIGN_UUID

@Data

public class User extends Model<User> {

private Long id;

@TableId(type = IdType.ASSIGN_UUID)

private String specid;

private String name;

private Integer age;

private String email;

private int deleted;

@TableField(fill = FieldFill.INSERT_UPDATE)

private LocalDateTime createtime;

@Version

private long version;

private String fab;

}

4.准备测试方法

@Test

public void testSave(){

for(long i=26;i<27;i++) {

User user = new User();

user.setId(400+i);

user.setAge((int)i+30);

user.setEmail("zoulinlin"+i+"@163.com");

user.setName("zouli"+i);

int row = userMapper.insert(user);

System.out.println(row);

}

}

运行log

Wed Oct 12 15:09:44 GMT+08:00 2022

insertFill

2022-10-12 15:09:47.266 DEBUG 14532 --- [ main] c.m.service.mapper.UserMapper.insert : ==> Preparing: INSERT INTO user (specid, id, name, age, email, deleted, createtime, version, FAB) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'FAB1')

2022-10-12 15:09:47.322 DEBUG 14532 --- [ main] c.m.service.mapper.UserMapper.insert : ==> Parameters: lot-20221012150944(String), 426(Long), zouli26(String), 56(Integer), zoulinlin26@163.com(String), 0(Integer), 2022-10-12T15:09:44.528(LocalDateTime), 0(Long)

Consume Time:138 ms 2022-10-12 15:09:47

Execute SQL:INSERT INTO user (specid, id, name, age, email, deleted, createtime, version, FAB) VALUES ('lot-20221012150944', 426, 'zouli26', 56, 'zoulinlin26@163.com', 0, '2022-10-12T15:09:44.528', 0, 'FAB1')

2022-10-12 15:09:47.462 DEBUG 14532 --- [ main] c.m.service.mapper.UserMapper.insert : <== Updates: 1

1

从以上运行日志中启用新的规则

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐