Skip to content

12. IdUtil

晓寻遥 edited this page Nov 6, 2021 · 1 revision

Introduce

generate some unique primary key IDs quickly and efficiently.

Method

UUID

Introduce

generate high-performance UUID.

Example

public void UUID(){
		// simpleUUID --> 2126330ed4bd4e5aa09085cacc27575f
		System.out.println( "simpleUUID --> " + IdUtil.simpleUUID());
		// fastUUID --> d52a62c9-2f88-4c1f-8989-115985b00c58
		System.out.println( "fastUUID --> " + IdUtil.fastUUID());
		// randomUUID --> 80dee6b8-f0f4-4ed4-adb0-d5692104aefe
		System.out.println( "randomUUID --> " + IdUtil.randomUUID());
		// fastSimpleUUID --> accf9816b58a413caf851b58427fcd02
		System.out.println( "fastSimpleUUID --> " + IdUtil.fastSimpleUUID());

}

objectID

Introduce

ObjectId is a unique ID generation strategy for MongoDB databases, and is a variant of UUID version1.

Example

public void objectId(){
		// objectId --> 61861d37-508ef630-2a5f8662
		System.out.println( "objectId --> " + IdUtil.objectId());
}

SnowflakeId

Introduce

in a distributed system, there are some scenarios that require the use of a globally unique ID. Sometimes we hope to use a simpler ID and hope that the ID can be generated in an orderly manner.

Example

public void test_(){
		Snowflake a = IdUtil.createSnowflakeId(11, 1);
		// SnowflakeId --> 654365844147933184
		System.out.println( "SnowflakeId --> " + a.nextId());
}

More

to learn more, refer to the API documentation

Clone this wiki locally