Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Snowflake algorithm ID Generation Strategy for mybatis-plus

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how mybatis-plus uses snowflake algorithm ID generation strategy". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Mybatis-plus can specify the primary key generation policy @ TableId (value= "id", type=IdType.ASSIGN_ID) value through the @ TableId annotation to describe the AUTO database ID self-increasing NONE stateless. This type is the unset primary key type (global is equal to global, global INPUT is equal to INPUT). Before INPUTinsert, the primary key value ASSIGN_ID assigns ID (primary key type is Number (Long and Integer) or String) (since 3.3.0) ASSIGN_UUID assigns UUID using interface IdentifierGenerator's method nextId (default implementation class is DefaultIdentifierGenerator snowflake algorithm), primary key type is String (since 3.3.0), and interface IdentifierGenerator's method nextUUID (default default method) ID_WORKER distributed globally unique ID long integer type (please use ASSIGN_ID) UUID32 bit UUID string (please use ASSIGN_UUID) ID_WORKER_STR distributed globally unique ID string type (please use ASSIGN_ID)

The above annotation sets the value, which corresponds to the process of judging this type and setting ID in MybatisDefaultParameterHandler. When judging idType.getKey () = = IdType.ASSIGN_ID.getKey (), a snowflake algorithm ID is created using identifierGenerator.nextId (entity).

Protected static void populateKeys (TableInfo tableInfo, MetaObject metaObject, Object entity) {IdType idType = tableInfo.getIdType (); String keyProperty = tableInfo.getKeyProperty (); if (StringUtils.isNotBlank (keyProperty) & & null! = idType & & idType.getKey () > = 3) {IdentifierGenerator identifierGenerator = GlobalConfigUtils.getGlobalConfig (tableInfo.getConfiguration ()). GetIdentifierGenerator (); Object idValue = metaObject.getValue (keyProperty) If (StringUtils.checkValNull (idValue)) {if (idType.getKey () = = IdType.ASSIGN_ID.getKey ()) {if (Number.class.isAssignableFrom (tableInfo.getKeyType () {metaObject.setValue (keyProperty, identifierGenerator.nextId (entity)) } else {metaObject.setValue (keyProperty, identifierGenerator.nextId (entity). ToString ());}} else if (idType.getKey () = = IdType.ASSIGN_UUID.getKey ()) {metaObject.setValue (keyProperty, identifierGenerator.nextUUID (entity)) }

After version 3.x, the default is to use IdType.ASSIGN_ID, that is, snowflake algorithm. If you need to use the primary key to augment, you need to modify IdType.AUTO.

Take a look at the source code and probably find out how to implement the snowflake algorithm.

First, the interface for the primary key generation strategy is IdentifierGenerator. The default implementation class in mp is DefaultIdentifierGenerator, which is the implementation class of Snowflake algorithm. The Sequence used in this class is the implementation class of the snowflake algorithm.

What if you want to create your own snowflake algorithm and manually create ID

There is an IdWork class under the com.baomidou.mybatisplus.core.toolkit package. This class uses the above DefaultIdentifierGenerator to obtain the snowflake algorithm ID. We can directly use the IdWork method of the same name getId or getIdStr to manually obtain a snowflake algorithm ID.

Long id=IdWorker.getIdStr () Custom ID Generator implementation

First, use spring scan annotations to create bean using

@ Componentpublic class CustomIdGenerator implements IdentifierGenerator {@ Override public Long nextId (Object entity) {/ / you can use the currently passed class full class name as bizKey, or extract parameters to generate bizKey for distributed Id call generation. String bizKey = entity.getClass (). GetName (); / / call distributed ID according to bizKey to generate long id =....; / / return the generated id value. Return id;}}

Second, directly configure the written CustomIdGenerator implementation class as a bean

@ Beanpublic IdentifierGenerator idGenerator () {return new CustomIdGenerator ();} public static void main (String [] args) {/ / return value 1385106677482582018 System.out.println (IdWorker.getId ()) in Mybatis-plus; / / return value "1385106677482582019" System.out.println (IdWorker.getIdStr ()) } this is the content of "how to use snowflake algorithm ID generation strategy for mybatis-plus". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 256

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report