1. 程式人生 > >twitter分布式主鍵id生成器

twitter分布式主鍵id生成器

created 主鍵 insert reat -c over factory work ann

pom

<!--生成id-->
        <dependency>
            <groupId>com.github.bingoohuang</groupId>
            <artifactId>idworker-client</artifactId>
            <version>1.0</version>
        </dependency>

添加到spring中

@Bean
public Sid returnSid(){
    
return new Sid(); }

使用方法

package com.lzh.service.impl;

import com.lzh.dao.VideosMapper;
import com.lzh.pojo.Videos;
import com.lzh.service.VideoService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * Created by 敲代碼的卡卡羅特 * on 2018/11/3 18:08. */ @Service public class VideoServiceImpl implements VideoService { @Autowired private VideosMapper videosMapper; @Autowired
private Sid sid; @Transactional(propagation = Propagation.REQUIRED) @Override public String saveVideo(Videos video) { String id = sid.nextShort(); video.setId(id); videosMapper.insertSelective(video); return id; } }

twitter分布式主鍵id生成器