MyBtais-Plus⾃定义主键key⽣成策略
⾃定义主键key⽣成策略
场景分析
在⽇常开发中我们对于保存数据库中的⼀条记录往往采⽤uuid形式,但在某些特定场景我们需要根据⾃定义id作为数据主
package ;
import nerator;
import ent;
import ist;
import tions;
import ;
import ;
@Component
public class CustomerKeyGenerator implements IKeyGenerator {
/**
*
* @param incrementerName 增量器名称,@KeySequence中的value之会传⼊该参数中
* @return
*/
@Override
public String executeSql(String incrementerName) {
//todo ⾃定义id代码编写位置
String id = getId();
String customId = "20210915-" + "⼗四运开幕式";
return "lect " + "'" + customId + "'" + "from dual";
}
package ;
import ;
import uence;
3.在主键字段id上使⽤@TableId(value = "id",type = IdType.INPUT),其中type =
IdType.INPUT表⽰⽤户⾃定义输⼊类型,该类型可以通过⾃⼰注册⾃动填充插件进⾏填充。
⾄此就完成了对MySQL数据库数据⾃定义主键操作,其实MP还为我们提供了中队的主键策略,这个在官⽹上可以看见,如下
TIP
主键⽣成策略必须使⽤INPUT
⽀持⽗类定义@KeySequence⼦类继承使⽤
⽀持主键类型指定(3.3.0开始⾃动识别主键类型)
内置⽀持:
DB2KeyGenerator
H2KeyGenerator
KingbaKeyGenerator
OracleKeyGenerator
PostgreKeyGenerator
如果内置⽀持不满⾜你的需求,可实现IKeyGenerator接⼝来进⾏扩展
本文发布于:2023-11-25 10:44:13,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/88/34902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:MyBtais.doc
本文 PDF 下载地址:MyBtais.pdf
留言与评论(共有 0 条评论) |