ID 使用 mybatis-plus 自动生成雪花id
This commit is contained in:
@ -9,7 +9,7 @@ import lombok.experimental.Accessors;
|
||||
@Accessors(chain = true)
|
||||
public class Demo {
|
||||
|
||||
private String id;
|
||||
private Long id;
|
||||
private String col1;
|
||||
private String col2;
|
||||
private String createBy;
|
||||
|
@ -10,6 +10,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.mapper.DemoMapper;
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.model.Demo;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MybatisPlusExampleTest {
|
||||
@ -22,10 +24,12 @@ public class MybatisPlusExampleTest {
|
||||
@Test
|
||||
public void testInsert() {
|
||||
Demo demo = new Demo();
|
||||
demo.setId("10");
|
||||
demo.setCol1("test");
|
||||
demo.setCol2("test01");
|
||||
// demo.setId("10");
|
||||
// demo.setCol1("test");
|
||||
// demo.setCol2("test01");
|
||||
//
|
||||
|
||||
System.out.println(IdUtil.getSnowflake(30, 30).nextId());
|
||||
Assertions.assertThat(mapper.insert(demo)).isGreaterThan(0);
|
||||
}
|
||||
//
|
||||
|
@ -37,30 +37,30 @@ public class JpaStarterConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入元对象字段填充(用于插入时对公共字段的填充)
|
||||
*
|
||||
* @param metaObject 元对象
|
||||
*/
|
||||
* 插入元对象字段填充(用于插入时对公共字段的填充)
|
||||
*
|
||||
* @param metaObject 元对象
|
||||
*/
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
Object obj = getFieldValByName("createBy", metaObject);
|
||||
obj = getFieldValByName("createDate", metaObject);
|
||||
if (obj == null) {
|
||||
setFieldValByName("createDate", LocalDateTime.now(), metaObject);
|
||||
}
|
||||
obj = getFieldValByName("updateDate", metaObject);
|
||||
if (obj == null) {
|
||||
setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
|
||||
}
|
||||
obj = getFieldValByName("tenantId", metaObject);
|
||||
if (obj == null) {
|
||||
setFieldValByName("tenantId", "ebtp_mall", metaObject);
|
||||
}
|
||||
obj = getFieldValByName("tenantName", metaObject);
|
||||
if (obj == null) {
|
||||
setFieldValByName("tenantName", "ebtp_mall", metaObject);
|
||||
}
|
||||
setFieldValByName("deleteFlag", CommonConstants.STATUS_NORMAL, metaObject);
|
||||
// Object obj = getFieldValByName("createBy", metaObject);
|
||||
// obj = getFieldValByName("createDate", metaObject);
|
||||
// if (obj == null) {
|
||||
// setFieldValByName("createDate", LocalDateTime.now(), metaObject);
|
||||
// }
|
||||
// obj = getFieldValByName("updateDate", metaObject);
|
||||
// if (obj == null) {
|
||||
// setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
|
||||
// }
|
||||
// obj = getFieldValByName("tenantId", metaObject);
|
||||
// if (obj == null) {
|
||||
// setFieldValByName("tenantId", "ebtp_mall", metaObject);
|
||||
// }
|
||||
// obj = getFieldValByName("tenantName", metaObject);
|
||||
// if (obj == null) {
|
||||
// setFieldValByName("tenantName", "ebtp_mall", metaObject);
|
||||
// }
|
||||
// setFieldValByName("deleteFlag", CommonConstants.STATUS_NORMAL, metaObject);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,8 +2,21 @@
|
||||
mybatis-plus.configuration.map-undersore-to-camel-case=true
|
||||
# 对所有的 resultMap 都进行自动映射
|
||||
mybatis-plus.configuration.auto-mapping-behavior=full
|
||||
|
||||
# 实体扫描,多个package用逗号或者分号分隔
|
||||
mybatis-plus.typeAliasesPackage=com.chinaunicom.mall.ebtp
|
||||
|
||||
# 项目统一mapper存放位置
|
||||
mybatis-plus.mapper-locations=classpath*:com/chinaunicom/mall/ebtp/**/mapper/*Mapper.xml
|
||||
|
||||
# 主键类型 0: 数据库ID自增, 1: 用户输入ID, 2: 全局唯一ID(数字类型), 3: UUID;
|
||||
mybatis-plus.global-config.id-type=3
|
||||
# 机器 ID 部分
|
||||
mybatis-plus.global-config.workerId=1
|
||||
# 数据中心 ID 部分 (workerId 和 datacenterId 一起配置才能重新初始化 Sequence)
|
||||
mybatis-plus.global-config.datacenterId=20
|
||||
# 字段策略 0: 忽略判断, 1:非 NULL 判断, 2: 非空判断
|
||||
mybatis-plus.global-config.field-strategy=2
|
||||
# 逻辑已删除值
|
||||
mybatis-plus.global-config.db-config.logic-delete-value=0
|
||||
# 逻辑未删除值
|
||||
|
Reference in New Issue
Block a user