spring boot 版本: 2.0.6.RELEASE
mybatis-plus 3.1.2
Druid 1.1.10
oracle 11g
网上的文章都是说怎样在mysql环境下集成的。我在oracle环境下集成uidgenerator。
第一步:建表
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;
第二步:从官方网站下载uid-generator
官方代码地址:https://github.com/baidu/uid-generator
打包后 ,安装到本地的仓库里。这里给你一个命令参考。
mvn install:install-file -Dfile=E:\uid-generator\target\uid-generator-1.0.0-SNAPSHOT.jar -DgroupId=com.baidu.fsg -DartifactId=uid-generator -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar
第三步:在项目里集成打包的jar.在pom.xml中增加依赖。
com.baidu.fsg uid-generator 1.0.0-SNAPSHOT
第四步:用mybatis-plus 的code -generator 自动生成 WORKER_NODE表的映射文件,
如何使用自动生成工具,可以到mybatis-plus官网上学习。https://mp.baomidou.com/
注意,如果生成了WorkerNodeEntity 更改为WorkerNodePlusEntity 避免与com.baidu.fsg.uid.worker.entity.WorkerNodeEntity冲突。
第五步:修改生成的WorkerNodeMapper.xml 内容, 注意修改命名空间
注意:ID的类型在mysql里对应BIGINT ,useGeneratedKeys="false" ,oracle不支持自增长列.
INSERT INTO WORKER_NODE (HOST_NAME, PORT, TYPE, LAUNCH_DATE, MODIFIED, CREATED) VALUES ( #{hostName}, #{port}, #{type}, #{launchDate}, systimestamp, systimestamp)
第六步:修改 WorkerNodeMapper 接口文件 ,增加接口
@Repository public interface WorkerNodeMapper extends BaseMapper{ /** * Get {@link WorkerNodeEntity} by node host * * @param host * @param port * @return */ WorkerNodeEntity getWorkerNodeByHostPort(@Param("host") String host, @Param("port") String port); /** * Add {@link WorkerNodeEntity} * * @param workerNodeEntity */ void addWorkerNode(WorkerNodeEntity workerNodeEntity); }
第七步:复制源码DisposableWorkerIdAssigner.java到项目里。
修改注入的WorkerNodeDAO为WorkerNodeMapper。
第八步:创建配置类:
package com.InsurMutilStation.pubbiz.WorkerNode.service; import com.baidu.fsg.uid.UidGenerator; import com.baidu.fsg.uid.impl.CachedUidGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration //xml配置的方式 //@ImportResource(locations = {"classpath:config/cached-uid-spring.xml"}) public class IdGeneratorConfiguration { @Bean(name = "disposableWorkerIdAssigner") public DisposableWorkerIdAssigner disposableWorkerIdAssigner(){ DisposableWorkerIdAssigner disposableWorkerIdAssigner = new DisposableWorkerIdAssigner(); return disposableWorkerIdAssigner; } @Bean(name="cachedUidGenerator") public UidGenerator cachedUidGenerator(DisposableWorkerIdAssigner disposableWorkerIdAssigner){ CachedUidGenerator cachedUidGenerator = new CachedUidGenerator(); cachedUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner); return cachedUidGenerator; } }
第九步:增加注解
@EnableAsync(proxyTargetClass = true)
在主类上增加该注解。这里很少有人提到。否则无法注入类。强制使用cglib来做spring动态代理。
第十步:使用该类生成全局ID
public class WorkerNodeServiceImpl extends BaseServiceImplimplements WorkerNodeService { @Autowired private UidGenerator uidGenerator; @Override public UidGeneratorReturnIdVo getUidGeneratorId() throws Exception { UidGeneratorReturnIdVo idVo = new UidGeneratorReturnIdVo(); long uid = uidGenerator.getUID(); String sUid = Convert.toStr(uid); idVo.setUid(sUid); return idVo; } }