java—如何为grails中的域类生成自定义字母数字id?

ahy6op9u  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(296)

伙计们
我有一个域名客户我一直在使用默认的自动增量值或gorm来处理我的id。但是,现在我想改变id从自动生成6个字母数字字符,你知道他们必须是唯一的吗?那我怎么做呢?拜托?

gupuwyp2

gupuwyp21#

将id属性定义为objectid可以生成12个字母数字字符的id

v2g6jxz6

v2g6jxz62#

跟随这个亲爱的:

public static String getAlphaNumbericRandom(int length) {
         String chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            int numberOfCodes = 0;//controls the length of alpha numberic string
            String code = "";
            while (numberOfCodes < length) {
                char c = chars.charAt((int) (Math.random() * chars.length()));
                code += c;
                numberOfCodes++;
            }
            return code;
        }

调用此方法并传递长度(例如4、5等)

相关问题