twitter分布式主鍵id生成器


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;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM