NutzCN Logo
问答 @Prev(els = {@EL("uuid()")}) 有内置得分布式id吗
发布于 1277天前 作者 wx_bo9oii7p4jiduf25sh2a 1150 次浏览 复制 上一个帖子 下一个帖子
标签:

@Prev(els = {@EL("uuid()")}) 我想将uuid换成分布式id,有没有内置得方法呢?如果没有我写好了获取分布式得id改怎么在这个el里边调用了

2 回复
/**
 * @author wizzer(wizzer.cn) on 2018/3/17.
 */
public interface IdGenerator extends RunMethod {
    /**
     * 主键生成器规则
     * @param tableName 表名
     * @param prefix 主键前缀
     * @return
     * @throws Exception
     */
    String next(String tableName, String prefix) throws Exception;
}

@IocBean
public class RedisIdGenerator implements IdGenerator {

    @Inject
    protected JedisAgent jedisAgent;
    private final static int ID_LENGTH = 16;

    public RedisIdGenerator() {
    }

    public RedisIdGenerator(JedisAgent jedisAgent) {
        this.jedisAgent = jedisAgent;
    }

    @Override
    public String next(String tableName, String prefix) {
        String key = prefix.toUpperCase();
        if (key.length() > ID_LENGTH) {
            key = key.substring(0, ID_LENGTH);
        }
        try (Jedis jedis = jedisAgent.getResource()) {
            String ym = Times.format("yyyyMM", new Date());
            String id = String.valueOf(jedis.incr("budwk:ig:cms:" + tableName.toUpperCase() + ym));
            return key + ym + Strings.alignRight(id, 10, '0');
        }
    }

    @Override
    public Object run(List<Object> fetchParam) {
        return next((String) fetchParam.get(0), (String) fetchParam.get(1));
    }

    @Override
    public String fetchSelf() {
        return "ig";
    }

}

项目启动时

//注册主键生成器
//CustomMake.me().register("ig", ioc.get(RedisIdGenerator.class));

POJO类使用

@PrevInsert(els = {@EL("ig(view.tableName,'')")})
添加回复
请先登陆
回到顶部