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

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