public enum IdType {
AUTO(0), //数据库 ID 自增
NONE(1), //无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUT(2), //insert 前自行 set 主键值
ASSIGN_ID(3), //自定义实现可以通过接口IdentifierGenerator的方法nextId来实现,默认是实现类DefaultIdentifierGenerator
ASSIGN_UUID(4); //分配 UUID
}
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);
}
}
@Configuration
public class MybatisPlusConfig {
@Bean
public CustomIdGenerator customIdGenerator(){
return new CustomIdGenerator();
}
@Data
public class User extends Model
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;
}
@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), [email protected](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, '[email protected]', 0, '2022-10-12T15:01:36.866', 0, 'FAB1')
public interface IdentifierGenerator {
default boolean assignId(Object idValue) {
return StringUtils.checkValNull(idValue);
}
Number nextId(Object entity);
default String nextUUID(Object entity) {
return IdWorker.get32UUID();
}
}
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;
}
}
@Data
public class User extends Model
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;
}
@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), [email protected](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, '[email protected]', 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