ID 使用 mybatis-plus 自动生成雪花id
This commit is contained in:
@ -8,8 +8,8 @@ import lombok.experimental.Accessors;
|
|||||||
@Data
|
@Data
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
public class Demo {
|
public class Demo {
|
||||||
|
|
||||||
private String id;
|
private Long id;
|
||||||
private String col1;
|
private String col1;
|
||||||
private String col2;
|
private String col2;
|
||||||
private String createBy;
|
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.mapper.DemoMapper;
|
||||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.model.Demo;
|
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.model.Demo;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class MybatisPlusExampleTest {
|
public class MybatisPlusExampleTest {
|
||||||
@ -22,10 +24,12 @@ public class MybatisPlusExampleTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testInsert() {
|
public void testInsert() {
|
||||||
Demo demo = new Demo();
|
Demo demo = new Demo();
|
||||||
demo.setId("10");
|
// demo.setId("10");
|
||||||
demo.setCol1("test");
|
// demo.setCol1("test");
|
||||||
demo.setCol2("test01");
|
// demo.setCol2("test01");
|
||||||
|
//
|
||||||
|
|
||||||
|
System.out.println(IdUtil.getSnowflake(30, 30).nextId());
|
||||||
Assertions.assertThat(mapper.insert(demo)).isGreaterThan(0);
|
Assertions.assertThat(mapper.insert(demo)).isGreaterThan(0);
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
|
@ -35,32 +35,32 @@ public class JpaStarterConfiguration {
|
|||||||
setFieldValByName("updateDate", now, metaObject);
|
setFieldValByName("updateDate", now, metaObject);
|
||||||
setFieldValByName("lastUpdateTime", now, metaObject);
|
setFieldValByName("lastUpdateTime", now, metaObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入元对象字段填充(用于插入时对公共字段的填充)
|
* 插入元对象字段填充(用于插入时对公共字段的填充)
|
||||||
*
|
*
|
||||||
* @param metaObject 元对象
|
* @param metaObject 元对象
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void insertFill(MetaObject metaObject) {
|
public void insertFill(MetaObject metaObject) {
|
||||||
Object obj = getFieldValByName("createBy", metaObject);
|
// Object obj = getFieldValByName("createBy", metaObject);
|
||||||
obj = getFieldValByName("createDate", metaObject);
|
// obj = getFieldValByName("createDate", metaObject);
|
||||||
if (obj == null) {
|
// if (obj == null) {
|
||||||
setFieldValByName("createDate", LocalDateTime.now(), metaObject);
|
// setFieldValByName("createDate", LocalDateTime.now(), metaObject);
|
||||||
}
|
// }
|
||||||
obj = getFieldValByName("updateDate", metaObject);
|
// obj = getFieldValByName("updateDate", metaObject);
|
||||||
if (obj == null) {
|
// if (obj == null) {
|
||||||
setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
|
// setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
|
||||||
}
|
// }
|
||||||
obj = getFieldValByName("tenantId", metaObject);
|
// obj = getFieldValByName("tenantId", metaObject);
|
||||||
if (obj == null) {
|
// if (obj == null) {
|
||||||
setFieldValByName("tenantId", "ebtp_mall", metaObject);
|
// setFieldValByName("tenantId", "ebtp_mall", metaObject);
|
||||||
}
|
// }
|
||||||
obj = getFieldValByName("tenantName", metaObject);
|
// obj = getFieldValByName("tenantName", metaObject);
|
||||||
if (obj == null) {
|
// if (obj == null) {
|
||||||
setFieldValByName("tenantName", "ebtp_mall", metaObject);
|
// setFieldValByName("tenantName", "ebtp_mall", metaObject);
|
||||||
}
|
// }
|
||||||
setFieldValByName("deleteFlag", CommonConstants.STATUS_NORMAL, metaObject);
|
// setFieldValByName("deleteFlag", CommonConstants.STATUS_NORMAL, metaObject);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,22 @@
|
|||||||
mybatis-plus.configuration.map-undersore-to-camel-case=true
|
mybatis-plus.configuration.map-undersore-to-camel-case=true
|
||||||
# 对所有的 resultMap 都进行自动映射
|
# 对所有的 resultMap 都进行自动映射
|
||||||
mybatis-plus.configuration.auto-mapping-behavior=full
|
mybatis-plus.configuration.auto-mapping-behavior=full
|
||||||
|
|
||||||
|
# 实体扫描,多个package用逗号或者分号分隔
|
||||||
|
mybatis-plus.typeAliasesPackage=com.chinaunicom.mall.ebtp
|
||||||
|
|
||||||
# 项目统一mapper存放位置
|
# 项目统一mapper存放位置
|
||||||
mybatis-plus.mapper-locations=classpath*:com/chinaunicom/mall/ebtp/**/mapper/*Mapper.xml
|
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
|
mybatis-plus.global-config.db-config.logic-delete-value=0
|
||||||
# 逻辑未删除值
|
# 逻辑未删除值
|
||||||
mybatis-plus.global-config.db-config.logic-not-delete-value=1
|
mybatis-plus.global-config.db-config.logic-not-delete-value=1
|
||||||
|
Reference in New Issue
Block a user