建筑类专业做教育的网站,Python视频直播网站开发,苏州企业服务平台,濮阳家电网站建设主键策略#xff08;ID自动生成#xff09;
以下是MyBatis-Plus中常见的几种主键生成策略及其对应的枚举值#xff08;3.3.0之前的版本#xff09;#xff1a;
主键生成策略枚举值数据库自增IdType.AUTO用户输入IdType.INPUT分布式全局唯一IDIdType.ID_WORKER分布式全局…主键策略ID自动生成
以下是MyBatis-Plus中常见的几种主键生成策略及其对应的枚举值3.3.0之前的版本
主键生成策略枚举值数据库自增IdType.AUTO用户输入IdType.INPUT分布式全局唯一IDIdType.ID_WORKER分布式全局唯一ID字符串类型IdType.ID_WORKER_STRUUIDIdType.UUID雪花算法全局唯一IDIdType.SNOWFLAKE雪花算法全局唯一ID字符串类型IdType.SNOWFLAKE_STR
3.3.0之后的版本
值描述AUTO数据库自增适用于MySQL、SQL Server等数据库INPUT手动输入适用于全局唯一ID的情况自定义ASSIGN_UUID32位UUID字符串ASSIGN_ID分布式全局唯一ID雪花算法生成NONE无状态可以通过全局唯一ID进行填充
public enum IdType {AUTO(0), //数据库自增长mysql的自增长主键NONE(1), //未设置INPUT(2), //自定义设置ASSIGN_ID(3), //分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),//使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)ASSIGN_UUID(4); //分配 UUID,主键类型为 String(since 3.3.0),//使用接口IdentifierGenerator的方法nextUUID(默认default 方法)private final int key;private IdType(int key) {this.key key;}public int getKey() {return this.key;}
}
配置文件中全局配置
#配置数据源
spring:datasource:druid:url: jdbc:mysql://localhost:3306/book_db?useSSLfalseserverTimezoneAsia/ShanghaiallowPublicKeyRetrievaltrueusername: rootpassword: 123driver-class-name: com.mysql.cj.jdbc.Driver#mybatisplus配置
mybatis-plus:global-config:db-config:#配置id自增长ID自动生成策略id-type: autoconfiguration:#配置mybatisplus日志log-impl: org.apache.ibatis.logging.stdout.StdOutImplmapper-locations: classpath:/mapper/*.mapper.xml #xml文件的位置(resources下的mapper文件夹)
注 实体类TableId注解的type属性会覆盖全局属性优先以各实体类的配置为主未配置的则全局配置生效
公共字段自动填充的使用
实体类公共字段 添加TableField注解配置fill属性
值描述INSERT插入时填充UPDATE更新时填充INSERT_UPDATE插入和更新时填充DEFAULT默认不填充
实现MetaObjectHandler接口重写insertFill和updateFill方法并注册为Bean
Slf4j
Component
public class MybatisplusHandler implements MetaObjectHandler {// 插入时的填充策略/*** 数据创建时间的属性名*/public static final String FIELD_CREATE_TIME createTime;/*** 数据最后修改时间的属性名*/public static final String FIELD_UPDATE_TIME updateTime;public static final String FIELD_CREATE_USER createUser;public static final String FIELD_UPDATE_USER updateUser;Overridepublic void insertFill(MetaObject metaObject) {LocalDateTime now LocalDateTime.now();Long id BaseContext.getCurrentId();log.info(开始执行插入时的自动填充);log.info(metaob{},metaObject.toString());this.strictInsertFill(metaObject,FIELD_CREATE_USER, Long.class, id);this.strictUpdateFill(metaObject,FIELD_UPDATE_USER, Long.class, id);this.strictInsertFill(metaObject,FIELD_CREATE_TIME, LocalDateTime.class, now);this.strictUpdateFill(metaObject,FIELD_UPDATE_TIME, LocalDateTime.class, now);}Overridepublic void updateFill(MetaObject metaObject) {LocalDateTime now LocalDateTime.now();Long id BaseContext.getCurrentId();this.strictUpdateFill(metaObject,FIELD_UPDATE_TIME, LocalDateTime.class, now);this.strictUpdateFill(metaObject,FIELD_UPDATE_USER, Long.class, id);}
}注意事项在使用公共字段自动填充和id自动生成时自定义的insert和update的Mapper方法不得进行判空否则自动填充不生效。这是由于mybatisplus的底层是先执行自定义的SQL后填充。此时公共自段未传入如果进行判空的话mybatis就不会拼接该字段后面填充时也不会插入该字段。 insert idinsert parameterTypecom.sky.entity.EmployeeINSERT INTO employeetrim prefix( suffix) suffixOverrides,id,if testname ! nullname,/ifif testusername ! nullusername,/ifif testpassword ! nullpassword,/ifif testphone ! nullphone,/ifif testsex ! nullsex,/ifif testidNumber ! nullid_number,/ifif teststatus ! nullstatus,/ifcreate_time,update_time,create_user,update_user/trimVALUEStrim prefix( suffix) suffixOverrides,#{id,jdbcTypeBIGINT},if testname ! null#{name,jdbcTypeVARCHAR},/ifif testusername ! null#{username,jdbcTypeVARCHAR},/ifif testpassword ! null#{password,jdbcTypeVARCHAR},/ifif testphone ! null#{phone,jdbcTypeVARCHAR},/ifif testsex ! null#{sex,jdbcTypeVARCHAR},/ifif testidNumber ! null#{idNumber,jdbcTypeVARCHAR},/ifif teststatus ! null#{status,jdbcTypeINTEGER},/if#{createTime,jdbcTypeTIMESTAMP},#{updateTime,jdbcTypeTIMESTAMP},#{createUser,jdbcTypeBIGINT},#{updateUser,jdbcTypeBIGINT}/trim/insert 文章转载自: http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.lfcfn.cn.gov.cn.lfcfn.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.ymyhg.cn.gov.cn.ymyhg.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.gywxq.cn.gov.cn.gywxq.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn