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接口
第一步 打包后
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<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.然后创建实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public 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接口
1
2
3@Mapper public interface WorkerNodeMapper extends BaseMapper<WorkNode>{ }
5.接口及实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public 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的配置类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62@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.测试
1
2
3
4
5
6
7
8
9
10
11
12@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内容请搜索靠谱客的其他文章。
发表评论 取消回复