教你用JAVA ID生成器去生成逻辑主键

<script type="text/javascript"> google_ad_client = "pub-8800625213955058"; /* 336x280, 创建于 07-11-21 */ google_ad_slot = "0989131976"; google_ad_width = 336; google_ad_height = 280; // </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 在一个数据库设计里,假如使用了逻辑主键,那么你一般都需要一个ID生成器去生成逻辑主键。 在许多数据库里面,都提供了ID生成的机制,如Oracle中的sequence,MSSQL中的identity,可惜这些方法各种数据库都不同的,所以很多人愿意找寻一种通用的方式。 编写代码,1、2、3……这样一直累加是最直接的想法,JAVA用以下方式去实现 private static AtomicInteger uniqueId = new AtomicInteger(0); public static String nextId() { return Integer.toString(uniqueId.incrementAndGet()); } 当然,这样太简单了,并且一重新启动,计数器就归 0 了,一般的做法可以用 时间 计数器 的方式, private static final long ONE_STEP = 10; private static final long BASE = 1129703383453l; private static final Lock LOCK = new ReentrantLock(); private static long lastTime = System.currentTimeMillis(); private static short lastCount = 0; /** * a time (as returned by {@link System#currentTimeMillis()}) at which * the VM that this UID was generated in was alive * @serial */ private final long time; /** * 16-bit number to distinguish UID instances created * in the same VM with the same time value * @serial */ private final short count; /** * Generates a UID that is unique over time with * respect to the host that it was generated on. */ public UID() { LOCK.lock(); try { if (lastCount == ONE_STEP) { boolean done = false; while (!done) { long now = System.currentTimeMillis(); if (now == lastTime) { // pause for a second to wait for time to change try { Thread.currentThread().sleep(1); } catch (java.lang.InterruptedException e) { } // ignore exception continue; } else { lastTime = now; lastCount = 0; done = true; } } } time = lastTime; count = lastCount ; } finally { LOCK.unlock(); } } 在一个群集的环境里面,通常还需要加上IP的前缀,即 IP 时间 计数器,这个就是JAVA原版本的实现了。
在 MyBatis Plus 中,你可以自定义主键生成器生成自定义的主键值。以下是一个示例: 1. 创建一个实现了 IdentifierGenerator 接口的自定义主键生成器类,例如 CustomIdGenerator: ```java public class CustomIdGenerator implements IdentifierGenerator { @Override public Serializable nextId(Object entity) { // 这里编写自己的主键生成逻辑 // 返回生成主键值 } } ``` 2. 在实体类中使用 @TableId 注解指定使用自定义主键生成器,例如: ```java public class User { @TableId(type = IdType.ASSIGN_ID, generator = "customIdGenerator") private Long id; // 其他属性和方法省略 } ``` 3. 在 MyBatis Plus 的配置文件中配置自定义主键生成器,例如 application.properties 或 application.yml: ```yaml mybatis-plus: global-config: db-config: id-type: auto # 设置全局的主键类型为自动增长 configuration: id-generator-type: custom # 设置自定义主键生成器 ``` 4. 在 MyBatis Plus 的配置类中将自定义主键生成器注册到 MyBatis 的全局配置中,例如: ```java @Configuration public class MybatisPlusConfig { @Bean public IdentifierGenerator customIdGenerator() { return new CustomIdGenerator(); } @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 添加其他拦截器 return interceptor; } } ``` 通过以上步骤,你就可以使用自定义的主键生成器生成自定义的主键值了。请根据你的实际需求来编写自己的主键生成逻辑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值