我是靠谱客的博主 辛勤指甲油,最近开发中收集的这篇文章主要介绍SpringBoot整合UidGenerator 通过mybatis-plus oracle版本,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.SpringBoot整合百度开源UidGenerator 并且替换默认的xml映射的方式为MyBatisPlus

2.SpringBoot版本为 2.2.8.RELEASE;MyBatisPlus版本为 3.3.2;UidGenerator版本为1.0.0-SNAPSHOT

3.替换默认的work_node 表结构和对应的实体类

4.替换默认的DisposableWorkerIdAssigner 并且修改注入的mapper接口为service接口

第一步 打包后

<dependency>
<groupId>com.baidu.fsg</groupId>
<artifactId>uid-generator</artifactId>
<version>${uid-generator.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
复制代码

 

2.建表

CREATE TABLE WORKER_NODE
(
ID  number(9) NOT NULL   ,
HOST_NAME VARCHAR(64) NOT NULL ,
PORT VARCHAR(64) NOT NULL ,
TYPE INT NOT NULL ,
LAUNCH_DATE DATE ,
MODIFIED TIMESTAMP NOT NULL ,
CREATED TIMESTAMP NOT NULL,
PRIMARY KEY(ID)
);

--创建同义词,自已更改所属用户,这里用的his_ext用户

create or replace public synonym WORKER_NODE for   his_ext.WORKER_NODE

-- 创建序列  WorkerNodeId --
create sequence WorkerNodeId_Seq
increment by 1
start with 1
minvalue 1
maxvalue 999999999;

创建触发器.让ID列自增

create or replace trigger WORKER_NODE_trigger before
insert on WORKER_NODE for each row
when (new.ID is null)
begin
select WorkerNodeId_Seq.nextval into:new.ID from dual;
end;
 

3.然后创建实体类

public class WorkNode {
@TableId(value = "work_node_id",type = IdType.AUTO)
private Long work_node_id;
@TableField(value = "host_name")
private String host_name;
@TableField(value = "port")
private String port;
@TableField(value = "type")
private Integer type;
@TableField(value = "launch_date")
private Date launch_date;
@TableField(value = "modeified")
private Date modeified;
@TableField(value = "created")
private Date created;
}

 4.mapper接口

@Mapper
public interface WorkerNodeMapper extends BaseMapper<WorkNode>{
}

 5.接口及实现类

public interface IWorkNodeService extends IService<WorkNode>{
public WorkNode getWorkerNodeByHostPort(String host,String port);
public void addWorkerNode(WorkNode workNode);
}
复制代码
@Service("workNodeService")
public class WorkNodeServiceImpl extends ServiceImpl<WorkerNodeMapper, WorkNode> implements IWorkNodeService{
@Override
public WorkNode getWorkerNodeByHostPort(String host, String port) {
QueryWrapper<WorkNode> queryWrapper=new QueryWrapper<>();
queryWrapper.lambda().eq(WorkNode::getHost_name, host).eq(WorkNode::getPort, port);
return getOne(queryWrapper);
}
@Override
public void addWorkerNode(WorkNode workNode) {
workNode.setCreated(new Date());
workNode.setModeified(new Date());
save(workNode);
}
}

 6.UidGenerator的配置类

@Configuration
public class IdGeneratorConfiguration {
/*
该类是在默认的基础上修改的
*/
@Bean
public DisposableWorkerIdAssigner disposableWorkerIdAssigner() {
return new DisposableWorkerIdAssigner();
}
//默认注入的id生成器,使用时只需从容器取即可
@Bean
public CachedUidGenerator cachedUidGenerator() {
CachedUidGenerator cachedUidGenerator = new CachedUidGenerator();
cachedUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner());
return cachedUidGenerator;
}
}
复制代码
public class DisposableWorkerIdAssigner implements WorkerIdAssigner {
private static final Logger LOGGER = LoggerFactory.getLogger(DisposableWorkerIdAssigner.class);
/*
@Autowired
private WorkerNodeMapper workerNodeMapper;
*/
//修改默认注入mapper为service接口
@Autowired
private IWorkNodeService workNodeService;
/**
* Assign worker id base on database.<p>
* If there is host name & port in the environment, we considered that the node runs in Docker container<br>
* Otherwise, the node runs on an actual machine.
*
* @return assigned worker id
*/
@Transactional
public long assignWorkerId() {
// build worker node entity
WorkNode workNode = buildWorkerNode();
// add worker node for new (ignore the same IP + PORT)
workNodeService.addWorkerNode(workNode);
LOGGER.info("Add worker node:" + workNode);
return workNode.getWork_node_id();
}
/**
* Build worker node entity by IP and PORT
*/
private WorkNode buildWorkerNode() {
WorkNode workNode = new WorkNode();
if (DockerUtils.isDocker()) {
workNode.setType(WorkerNodeType.CONTAINER.value());
workNode.setHost_name(DockerUtils.getDockerHost());
workNode.setPort(DockerUtils.getDockerPort());
workNode.setLaunch_date(new Date());
} else {
workNode.setType(WorkerNodeType.ACTUAL.value());
workNode.setHost_name(NetUtils.getLocalAddress());
workNode.setPort(System.currentTimeMillis() + "-" + RandomUtils.nextInt(100000));
workNode.setLaunch_date(new Date());
}
return workNode;
}
}

 7.测试

@RestController
public class IndexController {
//从容器中获取在配置类中注入的实例
@Autowired
private UidGenerator uidGenerator;
@GetMapping("/index")
public String index(){
System.err.println(uidGenerator.getUID());
return "success";
}
}

最后

以上就是辛勤指甲油为你收集整理的SpringBoot整合UidGenerator 通过mybatis-plus oracle版本的全部内容,希望文章能够帮你解决SpringBoot整合UidGenerator 通过mybatis-plus oracle版本所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部