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

}

猜你喜欢

转载自www.cnblogs.com/coder-lzh/p/9908363.html