转载

在系统中生成业务ID的几种方法

在系统中,除了使用数据库表本身的Id,如何生成各种业务Id?一下记录几种生成Id的方式:

  1. 使用数据库表记录生成的Id,以MySQL为例:

1) 首先创建一个数据库表,来记录当前的业务Id

CREATE TABLE `global_auto_number` (   `id` varchar(32) NOT NULL,   `version_optimized_lock` int(11) NOT NULL,   `business_key` varchar(255) NOT NULL COMMENT 'Can use full business class name as business key',   `current_num` bigint(22) NOT NULL COMMENT 'current number',   PRIMARY KEY (`id`),   UNIQUE KEY `UK_AUTO_NUMBER` (`business_key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

2) 获取Id的方法:

int currentCount = jdbcTemplate.update("update global_auto_number set current_num = LAST_INSERT_ID(current_num + 1) where business_key = ?", new Object[] {businessKey});  if(currentCount == 0) {      jdbcTemplate.update("insert into global_auto_number (id, version_optimized_lock, business_key, current_num) values (?, 1, ?, 0)", new Object[] {UUIDGenerator.generateUUID(),businessKey});      jdbcTemplate.update("update global_auto_number set current_num = LAST_INSERT_ID(current_num + 1) where business_key = ?", new Object[] {businessKey}); } return jdbcTemplate.queryForLong("select LAST_INSERT_ID()");

3) 按照业务逻辑格式化获取的Id。例如:

String.format("SEQ%09d", 123)

4) 注意事务需要用REQUIRES_NEW,否则在并发环境下会出现大量乐观锁问题。

2. 使用随机数来生成随机的Id,例如使用同一个Id来追踪后台响应用户操作的各种log

public static String generateRandomId() {       byte[] bytes = new byte[10];       try {          SecureRandom.getInstance("SHA1PRNG").nextBytes(bytes);       }       catch (NoSuchAlgorithmException e) {          throw new RuntimeException(e);       }       return toHexString(bytes);    }        final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();    public static String toHexString(byte[] bytes) {        char[] hexChars = new char[bytes.length * 2];        for ( int j = 0; j < bytes.length; j++ ) {            int v = bytes[j] & 0xFF;            hexChars[j * 2] = hexArray[v >>> 4];            hexChars[j * 2 + 1] = hexArray[v & 0x0F];        }        return new String(hexChars);    }
正文到此结束
Loading...