Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c737b5449 | ||
|
|
9a018b6af9 | ||
|
|
64724de96c | ||
|
|
8321032918 | ||
|
|
28b95468a3 | ||
|
|
828a7395d2 | ||
| 20ddc4412e | |||
| c65bd7b36f | |||
| 721cd48d96 | |||
| e2e5939810 | |||
| 670cd52c40 | |||
|
|
9104af2923 | ||
|
|
9f7baadf7a | ||
|
|
32d3de7550 | ||
|
|
bb2ddfc34a | ||
| 3615ed9eec |
107
README.md
107
README.md
@@ -4,11 +4,15 @@
|
||||
|
||||
用于生成全局自增序号,跳过的序号可以回收使用。
|
||||
|
||||
***本生成器内部使用transactionTemplate进行事务管理,如果你项目中给使用此生成器的方法或类加了`@Transactional`
|
||||
注解,则需将新建一个Service来专门负责调用此生成器的方法,同时要给新建的类增加`@Transactional`
|
||||
注解,并设置`propagation = Propagation.NOT_SUPPORTED`***
|
||||
|
||||
---
|
||||
|
||||
使用方法:
|
||||
|
||||
+ 在项目中放置jar包的地方把seq-1.8.2.jar、seq-1.8.2-sources.jar、seq-1.8.2-pom.xml复制过去
|
||||
+ 在项目中放置jar包的地方把seq-1.9.2.jar、seq-1.9.2-sources.jar、seq-1.9.2-pom.xml复制过去
|
||||
+ 在pom.xml中增加以下内容,然后执行maven命令:mvn clean
|
||||
|
||||
```xml
|
||||
@@ -18,7 +22,7 @@
|
||||
<dependency>
|
||||
<groupId>com.yanghuanglin</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.8.2</version>
|
||||
<version>1.9.2</version>
|
||||
<exclusions>
|
||||
<!-- 如若你项目中有引用spring-jdbc,则需要排除seq的jdbc依赖 -->
|
||||
<exclusion>
|
||||
@@ -50,13 +54,13 @@
|
||||
</goals>
|
||||
<configuration>
|
||||
<!-- ${project.basedir}表示当前项目的根目录 -->
|
||||
<file>${project.basedir}/lib/seq-1.8.2.jar</file>
|
||||
<pomFile>${project.basedir}/lib/seq-1.8.2-pom.xml</pomFile>
|
||||
<sources>${project.basedir}/lib/seq-1.8.2-sources.jar</sources>
|
||||
<file>${project.basedir}/lib/seq-1.9.2.jar</file>
|
||||
<pomFile>${project.basedir}/lib/seq-1.9.2-pom.xml</pomFile>
|
||||
<sources>${project.basedir}/lib/seq-1.9.2-sources.jar</sources>
|
||||
<repositoryLayout>default</repositoryLayout>
|
||||
<groupId>com.yanghuanglin</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.8.2</version>
|
||||
<version>1.9.2</version>
|
||||
<packaging>jar</packaging>
|
||||
<generatePom>true</generatePom>
|
||||
</configuration>
|
||||
@@ -202,16 +206,16 @@ TableConfig配置项,通过set方法设置(一般不用改,如果已有相
|
||||
|
||||
GeneratorConfig配置项,通过set方法设置
|
||||
|
||||
| 配置项 | 类型 | 默认值 | 说明 |
|
||||
|---------------------|------------------------------------------------------------------|------------------|------------------|
|
||||
| dataSource | javax.sql.DataSource | null | 数据源 |
|
||||
| monthZeroFilling | Boolean | true | 月份不足2位时自动补零 |
|
||||
| dayZeroFilling | Boolean | true | 当前日期不足2位时自动补零 |
|
||||
| autoCreate | Boolean | true | 开启自动建表 |
|
||||
| step | Integer | 1 | 序号增加时的步长 |
|
||||
| type | String | DEFAULT | 默认序号类型 |
|
||||
| minLength | Integer | 1 | 默认序号格式化后长度,不足的补零 |
|
||||
| tableConfig | com.yanghuanglin.seq.baseConfig.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||
| 配置项 | 类型 | 默认值 | 说明 |
|
||||
|------------------|---------------------------------------------|------------------|------------------|
|
||||
| dataSource | javax.sql.DataSource | null | 数据源 |
|
||||
| monthZeroFilling | Boolean | true | 月份不足2位时自动补零 |
|
||||
| dayZeroFilling | Boolean | true | 当前日期不足2位时自动补零 |
|
||||
| autoCreate | Boolean | true | 开启自动建表 |
|
||||
| step | Integer | 1 | 序号增加时的步长 |
|
||||
| type | String | DEFAULT | 默认序号类型 |
|
||||
| minLength | Integer | 1 | 默认序号格式化后长度,不足的补零 |
|
||||
| tableConfig | com.yanghuanglin.seq.baseConfig.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||
|
||||
---
|
||||
Generator方法如下:
|
||||
@@ -297,7 +301,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
*
|
||||
* @param sequences 生成的序号对象
|
||||
* @param pattern 格式
|
||||
@@ -308,7 +312,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
*
|
||||
* @param sequences 生成的序号对象
|
||||
* @param minLength 序号数字最小长度
|
||||
@@ -320,7 +324,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR#getPlaceholder()}(当前年份)、{@link FormatPlaceholder#MONTH#getPlaceholder()}(当前月份)、{@link FormatPlaceholder#DAY#getPlaceholder()}(当前日期)、{@link FormatPlaceholder#SEQ#getPlaceholder()}(生成的字符串序号)四个变量
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,pattern为#year##month##day#6#seq#,则会格式化为2022013061。此序号含义如下:
|
||||
* <p/>
|
||||
@@ -335,7 +339,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,minLength为4,pattern为#year##month##day#6#seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* <p/>
|
||||
@@ -351,44 +355,64 @@ public interface Generator {
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013061。此序号含义如下:
|
||||
* seq为1,fix为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013061。此序号含义如下:
|
||||
* <p/>
|
||||
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为{@link BaseConfig#getMinLength()}设置,默认为1,不足则补零]
|
||||
*
|
||||
* @param seq 需要格式化的序号
|
||||
* @param start 序号格式化后以什么字符串开头
|
||||
* @param fix 序号中的固定字符串
|
||||
* @param pattern 序号格式
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String start, String pattern);
|
||||
String format(Long seq, String fix, String pattern);
|
||||
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* seq为1,fix为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* <p/>
|
||||
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为4位,不足4位则补零]
|
||||
*
|
||||
* @param seq 需要格式化的序号
|
||||
* @param start 序号格式化后以什么字符串开头
|
||||
* @param fix 序号中的固定字符串
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 序号格式
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String start, Integer minLength, String pattern);
|
||||
String format(Long seq, String fix, Integer minLength, String pattern);
|
||||
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,fix为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* <p/>
|
||||
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为4位,不足4位则补零]
|
||||
*
|
||||
* @param seq 需要格式化的序号
|
||||
* @param fix 序号中的固定字符串
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 序号格式
|
||||
* @param year 格式化时使用的年
|
||||
* @param month 格式化时使用的月
|
||||
* @param day 格式化时使用的日
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String fix, Integer minLength, String pattern, Integer year, Integer month, Integer day);
|
||||
|
||||
/**
|
||||
* 将已格式化的序号解析为序号对象
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* 返回的序号对象{@link Sequences#getKey()}为null,{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
|
||||
* 返回的序号对象{@link Sequences#getKey()}为null,{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}可能有值
|
||||
* <p/>
|
||||
* 如果生成序号时,序号的key在年、月、日上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合,进而得到key
|
||||
* 如果生成序号时,序号的key在年、月、日上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}进行组合,进而得到key
|
||||
* <p/>
|
||||
* 例如:SNT序号每年都从1开始,则key应该是类似SNT2021、SNT2022这种格式,而在配置中,该序号的代码只是SNT,但是由于每年都要从1开始,所有应该每年有一个key,这个key就为SNT+年份,而这个年份就是此处解析后返回的对象中的{@link Sequences#getYear()}
|
||||
* <p/>
|
||||
@@ -400,6 +424,26 @@ public interface Generator {
|
||||
*/
|
||||
Sequences parse(String formatted, String pattern);
|
||||
|
||||
/**
|
||||
* 将已格式化的序号解析为序号对象
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* 返回的序号对象{@link Sequences#getKey()}为null,{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}可能有值
|
||||
* <p/>
|
||||
* 如果生成序号时,序号的key在年、月、日上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}进行组合,进而得到key
|
||||
* <p/>
|
||||
* 例如:SNT序号每年都从1开始,则key应该是类似SNT2021、SNT2022这种格式,而在配置中,该序号的代码只是SNT,但是由于每年都要从1开始,所有应该每年有一个key,这个key就为SNT+年份,而这个年份就是此处解析后返回的对象中的{@link Sequences#getYear()}
|
||||
* <p/>
|
||||
* 注意:序号格式和格式化后的字符串占位一定要匹配。如:处〔#year#〕#month#10801第#seq#号 对应 处〔2022〕0210801第10001号,而不能对应 处〔2022〕021110801第10001号
|
||||
*
|
||||
* @param formatted 格式化后的序号字符串
|
||||
* @param pattern 序号格式
|
||||
* @param fixLength 序号中的固定字符串的长度
|
||||
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、固定字符串(如果有)、序号的序号对象,其key需要根据情况手动设置,type为{@link GeneratorConfig#getType()}的默认值
|
||||
*/
|
||||
Sequences parse(String formatted, String pattern, Integer fixLength);
|
||||
|
||||
/**
|
||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||
* <p/>
|
||||
@@ -495,5 +539,4 @@ public interface Generator {
|
||||
*/
|
||||
void clearBefore(Date end);
|
||||
}
|
||||
|
||||
```
|
||||
36
pom.xml
36
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.yanghuanglin</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.8.2</version>
|
||||
<version>1.13.1</version>
|
||||
<name>seq</name>
|
||||
<description>seq</description>
|
||||
<properties>
|
||||
@@ -14,12 +14,31 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>5.3.15</version>
|
||||
<version>5.3.36</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.28</version>
|
||||
<version>8.0.33</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.com.kingbase</groupId>
|
||||
<artifactId>kingbase8</artifactId>
|
||||
<version>8.6.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.7.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.dameng</groupId>
|
||||
<artifactId>DmJdbcDriver18</artifactId>
|
||||
<version>8.1.3.140</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
@@ -30,6 +49,16 @@
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<excludes>
|
||||
<exclude>rebel.xml</exclude>
|
||||
<exclude>rebel-remote.xml</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
@@ -40,6 +69,7 @@
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.yanghuanglin.seq.dao.SequencesUnusedDao;
|
||||
import com.yanghuanglin.seq.dao.impl.SequencesDaoImpl;
|
||||
import com.yanghuanglin.seq.dao.impl.SequencesUnlockDaoImpl;
|
||||
import com.yanghuanglin.seq.dao.impl.SequencesUnusedDaoImpl;
|
||||
import com.yanghuanglin.seq.enums.DbType;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
@@ -30,6 +31,7 @@ public class BaseConfig {
|
||||
private Integer minLength;
|
||||
private Boolean monthZeroFilling;
|
||||
private Boolean dayZeroFilling;
|
||||
private DbType dbType;
|
||||
|
||||
private BaseConfig() {
|
||||
}
|
||||
@@ -161,13 +163,14 @@ public class BaseConfig {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
|
||||
this.transactionTemplate = new TransactionTemplate(transactionManager);
|
||||
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig(), generatorConfig.getDbType());
|
||||
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig(), generatorConfig.getDbType());
|
||||
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig(), generatorConfig.getDbType());
|
||||
this.step = generatorConfig.getStep();
|
||||
this.type = generatorConfig.getType();
|
||||
this.minLength = generatorConfig.getMinLength();
|
||||
this.monthZeroFilling = generatorConfig.getMonthZeroFilling();
|
||||
this.dayZeroFilling = generatorConfig.getDayZeroFilling();
|
||||
this.dbType = generatorConfig.getDbType();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.yanghuanglin.seq.config;
|
||||
|
||||
import com.yanghuanglin.seq.enums.DbType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* 生成器配置
|
||||
@@ -49,6 +53,11 @@ public class GeneratorConfig {
|
||||
*/
|
||||
private TableConfig tableConfig = new TableConfig();
|
||||
|
||||
/**
|
||||
* 数据库类型,默认情况下,根据dataSource中的DatabaseProductName自动获取
|
||||
*/
|
||||
private DbType dbType = null;
|
||||
|
||||
public GeneratorConfig() {
|
||||
}
|
||||
|
||||
@@ -121,4 +130,21 @@ public class GeneratorConfig {
|
||||
public void setTableConfig(TableConfig tableConfig) {
|
||||
this.tableConfig = tableConfig;
|
||||
}
|
||||
|
||||
public DbType getDbType() {
|
||||
if (this.dbType == null) {
|
||||
try {
|
||||
Connection connection = dataSource.getConnection();
|
||||
String productName = connection.getMetaData().getDatabaseProductName();
|
||||
this.dbType = DbType.of(productName);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("获取数据库类型失败", e);
|
||||
}
|
||||
}
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(DbType dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ public class TableConfig {
|
||||
*/
|
||||
private String createTimeColumn = "create_time";
|
||||
|
||||
/**
|
||||
* 序号表中最后使用时间
|
||||
*/
|
||||
private String updateTimeColumn = "update_time";
|
||||
|
||||
public String getTable() {
|
||||
return table;
|
||||
}
|
||||
@@ -71,4 +76,12 @@ public class TableConfig {
|
||||
public void setCreateTimeColumn(String createTimeColumn) {
|
||||
this.createTimeColumn = createTimeColumn.toLowerCase();
|
||||
}
|
||||
|
||||
public String getUpdateTimeColumn() {
|
||||
return updateTimeColumn;
|
||||
}
|
||||
|
||||
public void setUpdateTimeColumn(String updateTimeColumn) {
|
||||
this.updateTimeColumn = updateTimeColumn;
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,8 @@ public interface SequencesDao {
|
||||
*/
|
||||
boolean update(Sequences sequences);
|
||||
|
||||
/**
|
||||
* 创建表
|
||||
*/
|
||||
void createTable();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ import java.util.List;
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
public interface SequencesUnlockDao {
|
||||
/**
|
||||
* 查找某个未被锁定的序号
|
||||
*/
|
||||
SequencesUnlock find(SequencesUnlock sequencesUnlock);
|
||||
|
||||
/**
|
||||
* 保存使用中的序号
|
||||
*/
|
||||
@@ -40,5 +45,8 @@ public interface SequencesUnlockDao {
|
||||
*/
|
||||
boolean deleteByDate(Date begin, Date end);
|
||||
|
||||
/**
|
||||
* 创建表
|
||||
*/
|
||||
void createTable();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,11 @@ import java.util.List;
|
||||
*/
|
||||
public interface SequencesUnusedDao {
|
||||
|
||||
/**
|
||||
* 查找某个未被使用的序号
|
||||
*/
|
||||
SequencesUnused find(SequencesUnused sequencesUnused);
|
||||
|
||||
/**
|
||||
* 根据key,type查找seq最小的空闲序号
|
||||
*/
|
||||
@@ -36,6 +41,9 @@ public interface SequencesUnusedDao {
|
||||
*/
|
||||
boolean saveBatch(List<SequencesUnused> sequencesUnusedList);
|
||||
|
||||
/**
|
||||
* 创建表
|
||||
*/
|
||||
void createTable();
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.enums.DbType;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
public abstract class SequencesBase {
|
||||
protected DbType dbType;
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
protected DataSource dataSource;
|
||||
protected TableConfig tableConfig;
|
||||
|
||||
public SequencesBase(JdbcTemplate jdbcTemplate, TableConfig tableConfig, DbType dbType) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.dataSource = jdbcTemplate.getDataSource();
|
||||
this.tableConfig = tableConfig;
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行sql文件来创建表
|
||||
*
|
||||
* @param fileName 文件名,在 resources/数据库类型/ 下
|
||||
*/
|
||||
public void createTableByFile(String fileName) {
|
||||
try {
|
||||
Resource resource = new ClassPathResource(dbType.getSeries() + "/" + fileName);
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
|
||||
StringBuilder content = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
content.append(line).append(System.lineSeparator());
|
||||
}
|
||||
String sql = content.toString();
|
||||
switch (dbType) {
|
||||
case MySQL:
|
||||
sql = sql.replaceAll("`sequences(_unused|_unlock)`", String.format("%s$1", tableConfig.getTable()));
|
||||
sql = sql.replaceAll("`key`", String.format("`%s`", tableConfig.getKeyColumn()));
|
||||
sql = sql.replaceAll("`type`", String.format("`%s`", tableConfig.getTypeColumn()));
|
||||
sql = sql.replaceAll("`seq`", String.format("`%s`", tableConfig.getSeqColumn()));
|
||||
sql = sql.replaceAll("`create_time`", String.format("`%s`", tableConfig.getCreateTimeColumn()));
|
||||
break;
|
||||
case DM_DBMS:
|
||||
case PostgreSQL:
|
||||
case KingbaseES:
|
||||
sql = sql.replaceAll("\"sequences(_unused|_unlock)\"", String.format("%s$1", tableConfig.getTable()));
|
||||
sql = sql.replaceAll("\"key\"", String.format("\"%s\"", tableConfig.getKeyColumn()));
|
||||
sql = sql.replaceAll("\"type\"", String.format("\"%s\"", tableConfig.getTypeColumn()));
|
||||
sql = sql.replaceAll("\"seq\"", String.format("\"%s\"", tableConfig.getSeqColumn()));
|
||||
sql = sql.replaceAll("\"create_time\"", String.format("\"%s\"", tableConfig.getCreateTimeColumn()));
|
||||
break;
|
||||
}
|
||||
InputStream inputStream = new ByteArrayInputStream(sql.getBytes(StandardCharsets.UTF_8));
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(new InputStreamResource(inputStream));
|
||||
if (Objects.equals(dbType.getSeries(), DbType.DM_DBMS.getSeries()) && fileName.endsWith("trigger.sql")) {
|
||||
// 触发器文件不要通过;分隔,整个文件一起执行
|
||||
databasePopulator.setSeparator(ScriptUtils.EOF_STATEMENT_SEPARATOR);
|
||||
} else if (Objects.equals(dbType.getSeries(), DbType.PostgreSQL.getSeries()) && fileName.endsWith("function.sql")) {
|
||||
// 函数文件不要通过;分隔,整个文件一起执行
|
||||
databasePopulator.setSeparator(ScriptUtils.EOF_STATEMENT_SEPARATOR);
|
||||
}
|
||||
databasePopulator.execute(dataSource);
|
||||
} catch (IOException e) {
|
||||
System.err.println("执行SQL文件" + fileName + "失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据库类型,简单处理sql语句
|
||||
*
|
||||
* @param sqlString sql语句
|
||||
* @return 处理后的语句
|
||||
*/
|
||||
protected String dbAdapter(String sqlString) {
|
||||
switch (dbType) {
|
||||
case MySQL:
|
||||
break;
|
||||
case PostgreSQL:
|
||||
case DM_DBMS:
|
||||
case KingbaseES:
|
||||
sqlString = sqlString.replaceAll("`(.+?)`", "\"$1\"");
|
||||
break;
|
||||
}
|
||||
return sqlString;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,23 +2,23 @@ package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesDao;
|
||||
import com.yanghuanglin.seq.enums.DbType;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
/**
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
@SuppressWarnings("SqlResolve")
|
||||
public class SequencesDaoImpl implements SequencesDao {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final TableConfig tableConfig;
|
||||
@SuppressWarnings({"SqlResolve", "SqlSourceToSinkFlow"})
|
||||
public class SequencesDaoImpl extends SequencesBase implements SequencesDao {
|
||||
|
||||
public SequencesDaoImpl(JdbcTemplate jdbcTemplate, TableConfig tableConfig) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.tableConfig = tableConfig;
|
||||
public SequencesDaoImpl(JdbcTemplate jdbcTemplate, TableConfig tableConfig, DbType dbType) {
|
||||
super(jdbcTemplate, tableConfig, dbType);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,11 +26,12 @@ public class SequencesDaoImpl implements SequencesDao {
|
||||
String sql = "select * from `%s` where `%s`=? and `%s`=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn());
|
||||
try {
|
||||
return this.jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
|
||||
return this.jdbcTemplate.queryForObject(dbAdapter(sql), (rs, rowNum) -> {
|
||||
Sequences result = new Sequences();
|
||||
result.setKey(rs.getString(tableConfig.getKeyColumn()));
|
||||
result.setType(rs.getString(tableConfig.getTypeColumn()));
|
||||
result.setSeq(rs.getLong(tableConfig.getSeqColumn()));
|
||||
result.setUpdateTime(rs.getDate(tableConfig.getUpdateTimeColumn()));
|
||||
return result;
|
||||
}, sequences.getKey(), sequences.getType());
|
||||
} catch (EmptyResultDataAccessException ignored) {
|
||||
@@ -42,7 +43,7 @@ public class SequencesDaoImpl implements SequencesDao {
|
||||
public boolean save(Sequences sequences) {
|
||||
String sql = "insert into `%s`(`%s`,`%s`,`%s`) values(?,?,?)";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
int result = this.jdbcTemplate.update(sql, sequences.getKey(), sequences.getType(), sequences.getSeq());
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql), sequences.getKey(), sequences.getType(), sequences.getSeq());
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@@ -50,24 +51,18 @@ public class SequencesDaoImpl implements SequencesDao {
|
||||
public boolean update(Sequences sequences) {
|
||||
String sql = "update `%s` set `%s`=? where `%s`=? and `%s`=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getSeqColumn(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn());
|
||||
int result = this.jdbcTemplate.update(sql, sequences.getSeq(), sequences.getKey(), sequences.getType());
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql), sequences.getSeq(), sequences.getKey(), sequences.getType());
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable() {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `%s` ( " +
|
||||
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
|
||||
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
|
||||
" `%s` BIGINT ( 20 ) NOT NULL COMMENT '已使用到的序号'," +
|
||||
" PRIMARY KEY ( `%s`, `%s` ) " +
|
||||
" ) COMMENT '当前序号表'";
|
||||
sql = String.format(sql, tableConfig.getTable(),
|
||||
tableConfig.getKeyColumn(),
|
||||
tableConfig.getTypeColumn(),
|
||||
tableConfig.getSeqColumn(),
|
||||
tableConfig.getKeyColumn(),
|
||||
tableConfig.getTypeColumn());
|
||||
this.jdbcTemplate.execute(sql);
|
||||
this.createTableByFile("create_table_sequences.sql");
|
||||
if (Objects.equals(dbType.getSeries(), DbType.DM_DBMS.getSeries())) {
|
||||
this.createTableByFile("create_table_sequences_trigger.sql");
|
||||
} else if (Objects.equals(dbType.getSeries(), DbType.PostgreSQL.getSeries())) {
|
||||
this.createTableByFile("create_table_sequences_function.sql");
|
||||
this.createTableByFile("create_table_sequences_trigger.sql");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesUnlockDao;
|
||||
import com.yanghuanglin.seq.enums.DbType;
|
||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@@ -13,21 +15,36 @@ import java.util.List;
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
@SuppressWarnings("SqlResolve")
|
||||
public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final TableConfig tableConfig;
|
||||
@SuppressWarnings({"SqlResolve", "SqlSourceToSinkFlow"})
|
||||
public class SequencesUnlockDaoImpl extends SequencesBase implements SequencesUnlockDao {
|
||||
|
||||
public SequencesUnlockDaoImpl(JdbcTemplate jdbcTemplate, TableConfig tableConfig) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.tableConfig = tableConfig;
|
||||
public SequencesUnlockDaoImpl(JdbcTemplate jdbcTemplate, TableConfig tableConfig, DbType dbType) {
|
||||
super(jdbcTemplate, tableConfig, dbType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SequencesUnlock find(SequencesUnlock sequencesUnlock) {
|
||||
String sql = "select * from `%s_unlock` where `%s`=? and `%s`=? and `%s`=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
try {
|
||||
return this.jdbcTemplate.queryForObject(dbAdapter(sql), (rs, rowNum) -> {
|
||||
SequencesUnlock result = new SequencesUnlock();
|
||||
result.setKey(rs.getString(tableConfig.getKeyColumn()));
|
||||
result.setType(rs.getString(tableConfig.getTypeColumn()));
|
||||
result.setSeq(rs.getLong(tableConfig.getSeqColumn()));
|
||||
result.setCreateTime(rs.getDate(tableConfig.getCreateTimeColumn()));
|
||||
return result;
|
||||
}, sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq());
|
||||
} catch (EmptyResultDataAccessException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean save(SequencesUnlock sequencesUnlock) {
|
||||
String sql = "insert into `%s_unlock`(`%s`,`%s`,`%s`,`%s`) values(?,?,?,?)";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn(), tableConfig.getCreateTimeColumn());
|
||||
int result = this.jdbcTemplate.update(sql, sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq(), sequencesUnlock.getCreateTime());
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql), sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq(), sequencesUnlock.getCreateTime());
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@@ -38,9 +55,9 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
if (sequencesUnlock.getSeq() != null) {
|
||||
sql += " and `%s`=?";
|
||||
sql = String.format(sql, tableConfig.getSeqColumn());
|
||||
return this.jdbcTemplate.update(sql, sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq()) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq()) != 0;
|
||||
} else {
|
||||
return this.jdbcTemplate.update(sql, sequencesUnlock.getKey(), sequencesUnlock.getType()) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), sequencesUnlock.getKey(), sequencesUnlock.getType()) != 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +65,7 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
public List<SequencesUnlock> listAll() {
|
||||
String sql = "select * from `%s_unlock`";
|
||||
sql = String.format(sql, tableConfig.getTable());
|
||||
return this.jdbcTemplate.query(sql, rowMapper());
|
||||
return this.jdbcTemplate.query(dbAdapter(sql), rowMapper());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,15 +74,15 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
if (begin != null && end != null) {
|
||||
sql = "select * from `%s_unlock` where `%s`>=? and `%s`<=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.query(sql, rowMapper(), begin, end);
|
||||
return this.jdbcTemplate.query(dbAdapter(sql), rowMapper(), begin, end);
|
||||
} else if (begin != null) {
|
||||
sql = "select * from `%s_unlock` where `%s`>=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.query(sql, rowMapper(), begin);
|
||||
return this.jdbcTemplate.query(dbAdapter(sql), rowMapper(), begin);
|
||||
} else if (end != null) {
|
||||
sql = "select * from `%s_unlock` where `%s`<=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.query(sql, rowMapper(), end);
|
||||
return this.jdbcTemplate.query(dbAdapter(sql), rowMapper(), end);
|
||||
} else {
|
||||
return listAll();
|
||||
}
|
||||
@@ -75,7 +92,7 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
public boolean deleteAll() {
|
||||
String sql = "delete from `%s_unlock`";
|
||||
sql = String.format(sql, tableConfig.getTable());
|
||||
int result = this.jdbcTemplate.update(sql);
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql));
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@@ -85,15 +102,15 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
if (begin != null && end != null) {
|
||||
sql = "delete from `%s_unlock` where `%s`>=? and `%s`<=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.update(sql, begin, end) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), begin, end) != 0;
|
||||
} else if (begin != null) {
|
||||
sql = "delete from `%s_unlock` where `%s`>=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.update(sql, begin) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), begin) != 0;
|
||||
} else if (end != null) {
|
||||
sql = "delete from `%s_unlock` where `%s`<=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.update(sql, end) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), end) != 0;
|
||||
} else {
|
||||
return deleteAll();
|
||||
}
|
||||
@@ -101,22 +118,7 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
|
||||
@Override
|
||||
public void createTable() {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `%s_unlock` ( " +
|
||||
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
|
||||
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
|
||||
" `%s` BIGINT ( 20 ) NOT NULL COMMENT '尚未锁定的序号'," +
|
||||
" `%s` DATETIME NOT NULL COMMENT '使用时间'," +
|
||||
" PRIMARY KEY ( `%s`, `%s` ,`%s` ) " +
|
||||
" ) COMMENT '未锁定序号表'";
|
||||
sql = String.format(sql, tableConfig.getTable(),
|
||||
tableConfig.getKeyColumn(),
|
||||
tableConfig.getTypeColumn(),
|
||||
tableConfig.getSeqColumn(),
|
||||
tableConfig.getCreateTimeColumn(),
|
||||
tableConfig.getKeyColumn(),
|
||||
tableConfig.getTypeColumn(),
|
||||
tableConfig.getSeqColumn());
|
||||
this.jdbcTemplate.execute(sql);
|
||||
this.createTableByFile("create_table_sequences_unlock.sql");
|
||||
}
|
||||
|
||||
private RowMapper<SequencesUnlock> rowMapper() {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesUnusedDao;
|
||||
import com.yanghuanglin.seq.enums.DbType;
|
||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
@@ -18,14 +19,29 @@ import java.util.List;
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
@SuppressWarnings("SqlResolve")
|
||||
public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final TableConfig tableConfig;
|
||||
@SuppressWarnings({"SqlResolve", "SqlSourceToSinkFlow"})
|
||||
public class SequencesUnusedDaoImpl extends SequencesBase implements SequencesUnusedDao {
|
||||
|
||||
public SequencesUnusedDaoImpl(JdbcTemplate jdbcTemplate, TableConfig tableConfig) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.tableConfig = tableConfig;
|
||||
public SequencesUnusedDaoImpl(JdbcTemplate jdbcTemplate, TableConfig tableConfig, DbType dbType) {
|
||||
super(jdbcTemplate, tableConfig, dbType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SequencesUnused find(SequencesUnused sequencesUnused) {
|
||||
String sql = "select * from `%s_unused` where `%s`=? and `%s`=? and `%s`=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
try {
|
||||
return this.jdbcTemplate.queryForObject(dbAdapter(sql), (rs, rowNum) -> {
|
||||
SequencesUnused result = new SequencesUnused();
|
||||
result.setKey(rs.getString(tableConfig.getKeyColumn()));
|
||||
result.setType(rs.getString(tableConfig.getTypeColumn()));
|
||||
result.setSeq(rs.getLong(tableConfig.getSeqColumn()));
|
||||
result.setCreateTime(rs.getDate(tableConfig.getCreateTimeColumn()));
|
||||
return result;
|
||||
}, sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq());
|
||||
} catch (EmptyResultDataAccessException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,7 +49,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
String sql = "select * from `%s_unused` where `%s`=? and `%s`=? order by `%s` asc limit 0,1";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
try {
|
||||
return this.jdbcTemplate.queryForObject(sql, rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType());
|
||||
return this.jdbcTemplate.queryForObject(dbAdapter(sql), rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType());
|
||||
} catch (EmptyResultDataAccessException ignored) {
|
||||
return null;
|
||||
}
|
||||
@@ -44,7 +60,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
try {
|
||||
String sql = "select * from `%s_unused` where `%s`=? and `%s`=? order by `%s` desc limit 0,1";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
return this.jdbcTemplate.queryForObject(sql, rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType());
|
||||
return this.jdbcTemplate.queryForObject(dbAdapter(sql), rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType());
|
||||
} catch (EmptyResultDataAccessException ignored) {
|
||||
return null;
|
||||
}
|
||||
@@ -54,7 +70,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
public boolean delete(SequencesUnused sequencesUnused) {
|
||||
String sql = "delete from `%s_unused` where `%s`=? and `%s`=? and `%s`=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
int result = this.jdbcTemplate.update(sql, sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq());
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql), sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq());
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@@ -62,7 +78,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
public boolean save(SequencesUnused sequencesUnused) {
|
||||
String sql = "insert into `%s_unused`(`%s`,`%s`,`%s`,`%s`) values(?,?,?,?)";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn(), tableConfig.getCreateTimeColumn());
|
||||
int result = this.jdbcTemplate.update(sql, sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq(), sequencesUnused.getCreateTime());
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql), sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq(), sequencesUnused.getCreateTime());
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@@ -70,7 +86,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
public boolean saveBatch(List<SequencesUnused> sequencesUnusedList) {
|
||||
String sql = "insert into `%s_unused`(`%s`,`%s`,`%s`,`%s`) values(?,?,?,?)";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn(), tableConfig.getCreateTimeColumn());
|
||||
int[] result = this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
|
||||
int[] result = this.jdbcTemplate.batchUpdate(dbAdapter(sql), new BatchPreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
SequencesUnused sequencesUnused = sequencesUnusedList.get(i);
|
||||
@@ -90,29 +106,14 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
|
||||
@Override
|
||||
public void createTable() {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS `%s_unused` ( " +
|
||||
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
|
||||
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
|
||||
" `%s` BIGINT ( 20 ) NOT NULL COMMENT '闲置的的序号'," +
|
||||
" `%s` DATETIME NOT NULL COMMENT '设为闲置序号的时间'," +
|
||||
" PRIMARY KEY ( `%s`, `%s`, `%s` ) " +
|
||||
" ) COMMENT '闲置序号表'";
|
||||
sql = String.format(sql, tableConfig.getTable(),
|
||||
tableConfig.getKeyColumn(),
|
||||
tableConfig.getTypeColumn(),
|
||||
tableConfig.getSeqColumn(),
|
||||
tableConfig.getCreateTimeColumn(),
|
||||
tableConfig.getKeyColumn(),
|
||||
tableConfig.getTypeColumn(),
|
||||
tableConfig.getSeqColumn());
|
||||
this.jdbcTemplate.execute(sql);
|
||||
this.createTableByFile("create_table_sequences_unused.sql");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteAll() {
|
||||
String sql = "delete from `%s_unused`";
|
||||
sql = String.format(sql, tableConfig.getTable());
|
||||
int result = this.jdbcTemplate.update(sql);
|
||||
int result = this.jdbcTemplate.update(dbAdapter(sql));
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@@ -122,15 +123,15 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
if (begin != null && end != null) {
|
||||
sql = "delete from `%s_unused` where `%s`>=? and `%s`<=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.update(sql, begin, end) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), begin, end) != 0;
|
||||
} else if (begin != null) {
|
||||
sql = "delete from `%s_unused` where `%s`>=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.update(sql, begin) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), begin) != 0;
|
||||
} else if (end != null) {
|
||||
sql = "delete from `%s_unused` where `%s`<=?";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn());
|
||||
return this.jdbcTemplate.update(sql, end) != 0;
|
||||
return this.jdbcTemplate.update(dbAdapter(sql), end) != 0;
|
||||
} else {
|
||||
return deleteAll();
|
||||
}
|
||||
|
||||
37
src/main/java/com/yanghuanglin/seq/enums/DbType.java
Normal file
37
src/main/java/com/yanghuanglin/seq/enums/DbType.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.yanghuanglin.seq.enums;
|
||||
|
||||
public enum DbType {
|
||||
//MySQL数据库
|
||||
MySQL("mysql"),
|
||||
//达梦数据库
|
||||
DM_DBMS("dm"),
|
||||
//人大金仓数据库
|
||||
KingbaseES("pgsql"),
|
||||
//PostgreSQL数据库
|
||||
PostgreSQL("pgsql");
|
||||
|
||||
private final String series;
|
||||
|
||||
DbType(String series) {
|
||||
this.series = series;
|
||||
}
|
||||
|
||||
public String getSeries() {
|
||||
return series;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据库类型获取对应枚举
|
||||
*
|
||||
* @param productName 数据库类型
|
||||
* @return 枚举
|
||||
*/
|
||||
public static DbType of(String productName) {
|
||||
for (DbType value : DbType.values()) {
|
||||
if (value.name().equalsIgnoreCase(productName.replace(" ", "_"))) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("不支持的数据库类型");
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,11 @@ public enum FormatPlaceholder {
|
||||
*/
|
||||
DAY("#day#"),
|
||||
|
||||
/**
|
||||
* 序号格式字符中的固定字符串
|
||||
*/
|
||||
FIX("#fix#"),
|
||||
|
||||
/**
|
||||
* 序号格式字符中的格式化后的序号
|
||||
*/
|
||||
|
||||
@@ -78,7 +78,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
*
|
||||
* @param sequences 生成的序号对象
|
||||
* @param pattern 格式
|
||||
@@ -89,7 +89,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
*
|
||||
* @param sequences 生成的序号对象
|
||||
* @param minLength 序号数字最小长度
|
||||
@@ -101,7 +101,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR#getPlaceholder()}(当前年份)、{@link FormatPlaceholder#MONTH#getPlaceholder()}(当前月份)、{@link FormatPlaceholder#DAY#getPlaceholder()}(当前日期)、{@link FormatPlaceholder#SEQ#getPlaceholder()}(生成的字符串序号)四个变量
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,pattern为#year##month##day#6#seq#,则会格式化为2022013061。此序号含义如下:
|
||||
* <p/>
|
||||
@@ -116,7 +116,7 @@ public interface Generator {
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,minLength为4,pattern为#year##month##day#6#seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* <p/>
|
||||
@@ -132,44 +132,64 @@ public interface Generator {
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013061。此序号含义如下:
|
||||
* seq为1,fix为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013061。此序号含义如下:
|
||||
* <p/>
|
||||
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为{@link BaseConfig#getMinLength()}设置,默认为1,不足则补零]
|
||||
*
|
||||
* @param seq 需要格式化的序号
|
||||
* @param start 序号格式化后以什么字符串开头
|
||||
* @param fix 序号中的固定字符串
|
||||
* @param pattern 序号格式
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String start, String pattern);
|
||||
String format(Long seq, String fix, String pattern);
|
||||
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* seq为1,fix为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* <p/>
|
||||
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为4位,不足4位则补零]
|
||||
*
|
||||
* @param seq 需要格式化的序号
|
||||
* @param start 序号格式化后以什么字符串开头
|
||||
* @param fix 序号中的固定字符串
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 序号格式
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String start, Integer minLength, String pattern);
|
||||
String format(Long seq, String fix, Integer minLength, String pattern);
|
||||
|
||||
/**
|
||||
* 将生成的序号对象格式化为指定格式
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* seq为1,fix为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||
* <p/>
|
||||
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为4位,不足4位则补零]
|
||||
*
|
||||
* @param seq 需要格式化的序号
|
||||
* @param fix 序号中的固定字符串
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 序号格式
|
||||
* @param year 格式化时使用的年
|
||||
* @param month 格式化时使用的月
|
||||
* @param day 格式化时使用的日
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String fix, Integer minLength, String pattern, Integer year, Integer month, Integer day);
|
||||
|
||||
/**
|
||||
* 将已格式化的序号解析为序号对象
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* 返回的序号对象{@link Sequences#getKey()}为null,{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
|
||||
* 返回的序号对象{@link Sequences#getKey()}为null,{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}可能有值
|
||||
* <p/>
|
||||
* 如果生成序号时,序号的key在年、月、日上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合,进而得到key
|
||||
* 如果生成序号时,序号的key在年、月、日、固定字符串上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}进行组合,进而得到key
|
||||
* <p/>
|
||||
* 例如:SNT序号每年都从1开始,则key应该是类似SNT2021、SNT2022这种格式,而在配置中,该序号的代码只是SNT,但是由于每年都要从1开始,所有应该每年有一个key,这个key就为SNT+年份,而这个年份就是此处解析后返回的对象中的{@link Sequences#getYear()}
|
||||
* <p/>
|
||||
@@ -181,6 +201,34 @@ public interface Generator {
|
||||
*/
|
||||
Sequences parse(String formatted, String pattern);
|
||||
|
||||
/**
|
||||
* 将已格式化的序号解析为序号对象
|
||||
* <p/>
|
||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}(当前月份)、{@link FormatPlaceholder#DAY}(当前日期)、{@link FormatPlaceholder#FIX}(固定字符串)、{@link FormatPlaceholder#SEQ}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||
* <p/>
|
||||
* 返回的序号对象{@link Sequences#getKey()}为null,{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}可能有值
|
||||
* <p/>
|
||||
* 如果生成序号时,序号的key在年、月、日、固定字符串上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}、{@link Sequences#getFix()}进行组合,进而得到key
|
||||
* <p/>
|
||||
* 例如:SNT序号每年都从1开始,则key应该是类似SNT2021、SNT2022这种格式,而在配置中,该序号的代码只是SNT,但是由于每年都要从1开始,所有应该每年有一个key,这个key就为SNT+年份,而这个年份就是此处解析后返回的对象中的{@link Sequences#getYear()}
|
||||
* <p/>
|
||||
* 注意:序号格式和格式化后的字符串占位一定要匹配。如:处〔#year#〕#month#10801第#seq#号 对应 处〔2022〕0210801第10001号,而不能对应 处〔2022〕021110801第10001号
|
||||
*
|
||||
* @param formatted 格式化后的序号字符串
|
||||
* @param pattern 序号格式
|
||||
* @param fixLength 序号中的固定字符串的长度
|
||||
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、固定字符串(如果有)、序号的序号对象,其key需要根据情况手动设置,type为{@link GeneratorConfig#getType()}的默认值
|
||||
*/
|
||||
Sequences parse(String formatted, String pattern, Integer fixLength);
|
||||
|
||||
/**
|
||||
* 查询序号是否已被锁定
|
||||
*
|
||||
* @param sequences 需要查询的序号
|
||||
* @return 锁定结果
|
||||
*/
|
||||
boolean locked(Sequences sequences);
|
||||
|
||||
/**
|
||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||
* <p/>
|
||||
@@ -204,6 +252,14 @@ public interface Generator {
|
||||
*/
|
||||
boolean lock(Sequences sequences, boolean ignoreSeq);
|
||||
|
||||
/**
|
||||
* 将序号设为未使用
|
||||
*
|
||||
* @param sequences 需要设为未使用的序号
|
||||
* @return 设置结果
|
||||
*/
|
||||
boolean unused(Sequences sequences);
|
||||
|
||||
/**
|
||||
* 释放所有未使用的序号
|
||||
* <p/>
|
||||
@@ -275,4 +331,9 @@ public interface Generator {
|
||||
* @param end 结束时间
|
||||
*/
|
||||
void clearBefore(Date end);
|
||||
|
||||
/**
|
||||
* 创建表
|
||||
*/
|
||||
void createTable();
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static com.yanghuanglin.seq.enums.FormatPlaceholder.*;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class SequencesGenerator implements Generator {
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
private final SequencesDao sequencesDao;
|
||||
@@ -57,12 +58,16 @@ public class SequencesGenerator implements Generator {
|
||||
private synchronized void createTable(Boolean autoCreate) {
|
||||
if (!autoCreate)
|
||||
return;
|
||||
this.createTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void createTable() {
|
||||
this.sequencesDao.createTable();
|
||||
this.sequencesUnusedDao.createTable();
|
||||
this.sequencesUnlockDao.createTable();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean containSeq(String pattern) {
|
||||
return StringUtils.hasLength(pattern) && pattern.contains(SEQ.getPlaceholder());
|
||||
@@ -85,7 +90,11 @@ public class SequencesGenerator implements Generator {
|
||||
//根据传入的key和type新生成查询条件对象
|
||||
Sequences condition = new Sequences(key, type);
|
||||
|
||||
if (!withOutSeq) {
|
||||
if (Boolean.TRUE.equals(withOutSeq)) {
|
||||
//不包含序号
|
||||
condition.setWithOutSeq(true);
|
||||
return condition;
|
||||
} else {
|
||||
//不是不包含序号的序号对象(有可能格式中只配置了#year##month##day#,序号就不用自增,也不用入库,直接返回即可)
|
||||
//找到正在使用的最大序号
|
||||
Sequences sequences = sequencesDao.find(condition);
|
||||
@@ -119,9 +128,6 @@ public class SequencesGenerator implements Generator {
|
||||
}
|
||||
sequences.setWithOutSeq(false);
|
||||
return result ? sequences : null;
|
||||
} else {
|
||||
condition.setWithOutSeq(true);
|
||||
return condition;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -170,30 +176,48 @@ public class SequencesGenerator implements Generator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(Long seq, String start, String pattern) {
|
||||
return format(seq, start, minLength, pattern);
|
||||
public String format(Long seq, String fix, String pattern) {
|
||||
return format(seq, fix, minLength, pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(Long seq, String start, Integer minLength, String pattern) {
|
||||
if (start == null)
|
||||
start = "";
|
||||
String seqString = start + new Sequences(seq).format(minLength);
|
||||
public String format(Long seq, String fix, Integer minLength, String pattern) {
|
||||
return format(seq, fix, minLength, pattern, null, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(Long seq, String fix, Integer minLength, String pattern, Integer year, Integer month, Integer day) {
|
||||
if (fix == null)
|
||||
fix = "";
|
||||
String seqString = new Sequences(seq).format(minLength);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
int month = calendar.get(Calendar.MONTH) + 1;
|
||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
if (year == null)
|
||||
year = calendar.get(Calendar.YEAR);
|
||||
if (month == null)
|
||||
month = calendar.get(Calendar.MONTH) + 1;
|
||||
if (day == null)
|
||||
day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
pattern = pattern.replaceAll(YEAR.getPlaceholder(), String.valueOf(year));
|
||||
pattern = pattern.replaceAll(MONTH.getPlaceholder(), monthZeroFilling ? String.format("%02d", month) : String.valueOf(month));
|
||||
pattern = pattern.replaceAll(DAY.getPlaceholder(), dayZeroFilling ? String.format("%02d", day) : String.valueOf(day));
|
||||
pattern = pattern.replaceAll(FIX.getPlaceholder(), fix);
|
||||
pattern = pattern.replaceAll(SEQ.getPlaceholder(), seqString);
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sequences parse(String formatted, String pattern) {
|
||||
return parse(formatted, pattern, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sequences parse(String formatted, String pattern, Integer fixLength) {
|
||||
//如果解析时的格式中包含了固定字符串的占位符,则其固定字符串占位符的长度必须大于0,否则抛出异常。
|
||||
//如果不设置固定字符串的长度,就无法将固定字符串和序号分隔开
|
||||
if (pattern.contains(FIX.getPlaceholder()) && (fixLength == null || fixLength == 0))
|
||||
throw new NullPointerException("请设置固定字符串的长度,该长度需要大于0,否则无法区分固定字符串和序号。");
|
||||
//年、月、日、序号分隔特殊符号正则规则
|
||||
String splitRegString = "(" + YEAR.getPlaceholder() + "|" + MONTH.getPlaceholder() + "|" + DAY.getPlaceholder() + "|" + SEQ.getPlaceholder() + ")";
|
||||
String splitRegString = "(" + YEAR.getPlaceholder() + "|" + MONTH.getPlaceholder() + "|" + DAY.getPlaceholder() + "|" + FIX.getPlaceholder() + "|" + SEQ.getPlaceholder() + ")";
|
||||
//根据年、月、日、序号的特殊符号,对格式进行分隔,得到排除特殊符号后的字符串数组
|
||||
String[] split = pattern.split(splitRegString);
|
||||
|
||||
@@ -203,23 +227,25 @@ public class SequencesGenerator implements Generator {
|
||||
formatted = formatted.replace(splitString, "");
|
||||
}
|
||||
|
||||
//年、月、日的数字匹配正则规则
|
||||
//年、月、日、固定字符串 匹配正则规则
|
||||
String yearRegStr = "\\d{4}";
|
||||
String monthRegStr = "\\d{2}";
|
||||
String dayRegStr = "\\d{2}";
|
||||
String fixRegStr = (fixLength == null || fixLength == 0) ? null : ".{" + fixLength + "}";
|
||||
|
||||
//将序号格式分隔特殊符号字符串转为正则匹配规则
|
||||
Pattern seqPattern = Pattern.compile(splitRegString);
|
||||
//对序号格式进行匹配
|
||||
Matcher matcher = seqPattern.matcher(pattern);
|
||||
|
||||
//将年、月、日匹配规则字符串转为正则匹配规则
|
||||
//将年、月、日、固定字符串 匹配规则字符串转为正则匹配规则
|
||||
Pattern yearPattern = Pattern.compile(yearRegStr);
|
||||
Pattern monthPattern = Pattern.compile(monthRegStr);
|
||||
Pattern dayPattern = Pattern.compile(dayRegStr);
|
||||
Pattern fixPattern = StringUtils.hasLength(fixRegStr) ? Pattern.compile(fixRegStr) : null;
|
||||
|
||||
//默认的年、月、日均为空字符串
|
||||
String year = "", month = "", day = "", seq = "";
|
||||
//默认的年、月、日、固定字符串、序号均为空字符串
|
||||
String year = "", month = "", day = "", fix = "", seq = "";
|
||||
//根据序号匹配规则字符串查找字符串分组
|
||||
while (matcher.find()) {
|
||||
String group = matcher.group();
|
||||
@@ -233,7 +259,7 @@ public class SequencesGenerator implements Generator {
|
||||
if (yearMatcher.find()) {
|
||||
year = yearMatcher.group();
|
||||
}
|
||||
formatted = formatted.replaceFirst(yearRegStr, "");
|
||||
formatted = formatted.replaceFirst(yearPattern.pattern(), "");
|
||||
break;
|
||||
case MONTH:
|
||||
//若分组为月份分组,则将月份正则匹配到的字符串赋值给month,同时把格式化后的序号字符串中,对应月的字符串替换为空字符串
|
||||
@@ -241,7 +267,7 @@ public class SequencesGenerator implements Generator {
|
||||
if (monthMatcher.find()) {
|
||||
month = monthMatcher.group();
|
||||
}
|
||||
formatted = formatted.replaceFirst(monthRegStr, "");
|
||||
formatted = formatted.replaceFirst(monthPattern.pattern(), "");
|
||||
break;
|
||||
case DAY:
|
||||
//若分组为日期分组,则将日期正则匹配到的字符串赋值给day,同时把格式化后的序号字符串中,对应日期的字符串替换为空字符串
|
||||
@@ -249,7 +275,17 @@ public class SequencesGenerator implements Generator {
|
||||
if (dayMatcher.find()) {
|
||||
day = dayMatcher.group();
|
||||
}
|
||||
formatted = formatted.replaceFirst(dayRegStr, "");
|
||||
formatted = formatted.replaceFirst(dayPattern.pattern(), "");
|
||||
break;
|
||||
case FIX:
|
||||
if (fixPattern == null)
|
||||
break;
|
||||
//若分组为固定字符串分组,则将固定字符串正则匹配到的字符串赋值给fix,同时把格式化后的序号字符串中,对应固定字符串的字符串替换为空字符串
|
||||
Matcher fixMatcher = fixPattern.matcher(formatted);
|
||||
if (fixMatcher.find()) {
|
||||
fix = fixMatcher.group();
|
||||
}
|
||||
formatted = formatted.replaceFirst(fixPattern.pattern(), "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -262,6 +298,7 @@ public class SequencesGenerator implements Generator {
|
||||
sequences.setYear(StringUtils.hasLength(year) ? Integer.valueOf(year) : null);
|
||||
sequences.setMonth(StringUtils.hasLength(month) ? Integer.valueOf(month) : null);
|
||||
sequences.setDay(StringUtils.hasLength(day) ? Integer.valueOf(day) : null);
|
||||
sequences.setFix(StringUtils.hasLength(fix) ? fix : null);
|
||||
sequences.setSeq(StringUtils.hasLength(seq) ? Long.parseLong(seq) : 0L);
|
||||
sequences.setWithOutSeq(!StringUtils.hasLength(seq));
|
||||
sequences.setType(type);
|
||||
@@ -270,10 +307,30 @@ public class SequencesGenerator implements Generator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean lock(Sequences sequences) {
|
||||
if (sequences == null || sequences.getWithOutSeq())
|
||||
public boolean locked(Sequences sequences) {
|
||||
if (sequences == null || Boolean.TRUE.equals(sequences.getWithOutSeq()))
|
||||
return true;
|
||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||
|
||||
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
||||
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||
try {
|
||||
SequencesUnlock sequencesUnlock = sequencesUnlockDao.find(condition);
|
||||
return sequencesUnlock == null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
status.setRollbackOnly();
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean lock(Sequences sequences) {
|
||||
if (sequences == null || Boolean.TRUE.equals(sequences.getWithOutSeq()))
|
||||
return true;
|
||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||
|
||||
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
||||
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||
try {
|
||||
@@ -290,7 +347,7 @@ public class SequencesGenerator implements Generator {
|
||||
public synchronized boolean lock(Sequences sequences, boolean ignoreSeq) {
|
||||
if (!ignoreSeq)
|
||||
return lock(sequences);
|
||||
if (sequences == null || sequences.getWithOutSeq())
|
||||
if (sequences == null || Boolean.TRUE.equals(sequences.getWithOutSeq()))
|
||||
return true;
|
||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||
condition.setSeq(null);
|
||||
@@ -306,6 +363,24 @@ public class SequencesGenerator implements Generator {
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean unused(Sequences sequences) {
|
||||
if (sequences == null)
|
||||
return true;
|
||||
SequencesUnused condition = new SequencesUnused(sequences);
|
||||
condition.setCreateTime(new Date());
|
||||
//在未使用中表增加需要设为未使用的序号
|
||||
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||
try {
|
||||
return sequencesUnusedDao.save(condition);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
status.setRollbackOnly();
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean release() {
|
||||
//列出所有使用中表存在的序号
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.yanghuanglin.seq.po;
|
||||
|
||||
import com.yanghuanglin.seq.config.BaseConfig;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 当前序号
|
||||
*
|
||||
@@ -24,6 +26,11 @@ public class Sequences {
|
||||
*/
|
||||
protected Long seq = 0L;
|
||||
|
||||
/**
|
||||
* 最后使用时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 临时字段,序号对应的年份(如2022年)(如果能获取到的话)。
|
||||
* 该字段仅用于解析序号字符串时,解析出对应年份,用于合成key(如:序号对应的key为SNT+年份,返回的为其年份)
|
||||
@@ -42,6 +49,12 @@ public class Sequences {
|
||||
*/
|
||||
private transient Integer day;
|
||||
|
||||
/**
|
||||
* 临时字段,序号对应的固定字符串(A1)。
|
||||
* 该字段仅用于解析序号字符串时,解析出对应固定字符,用于合成key(如:序号对应的key为SNT+年份+月份+日期+固定字符串,返回的为其固定字符串)
|
||||
*/
|
||||
private transient String fix;
|
||||
|
||||
/**
|
||||
* 临时字段,表示序号对象不包含序号
|
||||
*/
|
||||
@@ -95,6 +108,14 @@ public class Sequences {
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
public Date getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
public void setUpdateTime(Date updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
public Integer getYear() {
|
||||
return year;
|
||||
}
|
||||
@@ -119,6 +140,14 @@ public class Sequences {
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
public String getFix() {
|
||||
return fix;
|
||||
}
|
||||
|
||||
public void setFix(String fix) {
|
||||
this.fix = fix;
|
||||
}
|
||||
|
||||
public Boolean getWithOutSeq() {
|
||||
return withOutSeq;
|
||||
}
|
||||
@@ -166,9 +195,11 @@ public class Sequences {
|
||||
"key='" + key + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", seq=" + seq +
|
||||
", updateTime=" + updateTime +
|
||||
", year=" + year +
|
||||
", month=" + month +
|
||||
", day=" + day +
|
||||
", fix='" + fix + '\'' +
|
||||
", withOutSeq=" + withOutSeq +
|
||||
'}';
|
||||
}
|
||||
|
||||
13
src/main/resources/dm/create_table_sequences.sql
Normal file
13
src/main/resources/dm/create_table_sequences.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "sequences"
|
||||
(
|
||||
"key" VARCHAR(64) NOT NULL,
|
||||
"type" VARCHAR(64) NOT NULL,
|
||||
"seq" BIGINT NOT NULL,
|
||||
"update_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY ("key", "type")
|
||||
);
|
||||
COMMENT ON TABLE "sequences" IS '当前序号表';
|
||||
COMMENT ON COLUMN "sequences"."key" IS '序号英文名称';
|
||||
COMMENT ON COLUMN "sequences"."type" IS '序号类型';
|
||||
COMMENT ON COLUMN "sequences"."seq" IS '已使用到的序号';
|
||||
COMMENT ON COLUMN "sequences"."update_time" IS '最后使用时间';
|
||||
8
src/main/resources/dm/create_table_sequences_trigger.sql
Normal file
8
src/main/resources/dm/create_table_sequences_trigger.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
CREATE OR REPLACE TRIGGER "SEQUENCES_UPDATE_TIME"
|
||||
BEFORE UPDATE
|
||||
ON "sequences"
|
||||
referencing OLD ROW AS "OLD" NEW ROW AS "NEW"
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
:NEW."UPDATE_TIME" := CURRENT_TIMESTAMP;
|
||||
END;
|
||||
13
src/main/resources/dm/create_table_sequences_unlock.sql
Normal file
13
src/main/resources/dm/create_table_sequences_unlock.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "sequences_unlock"
|
||||
(
|
||||
"key" VARCHAR(64) NOT NULL,
|
||||
"type" VARCHAR(64) NOT NULL,
|
||||
"seq" BIGINT NOT NULL,
|
||||
"create_time" TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY ("key", "type", "seq")
|
||||
);
|
||||
COMMENT ON TABLE "sequences_unlock" IS '未锁定序号表';
|
||||
COMMENT ON COLUMN "sequences_unlock"."key" IS '序号英文名称';
|
||||
COMMENT ON COLUMN "sequences_unlock"."type" IS '序号类型';
|
||||
COMMENT ON COLUMN "sequences_unlock"."seq" IS '尚未锁定的序号';
|
||||
COMMENT ON COLUMN "sequences_unlock"."create_time" IS '使用时间';
|
||||
13
src/main/resources/dm/create_table_sequences_unused.sql
Normal file
13
src/main/resources/dm/create_table_sequences_unused.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "sequences_unused"
|
||||
(
|
||||
"key" VARCHAR(64) NOT NULL,
|
||||
"type" VARCHAR(64) NOT NULL,
|
||||
"seq" BIGINT NOT NULL,
|
||||
"create_time" TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY ("key", "type", "seq")
|
||||
);
|
||||
COMMENT ON TABLE "sequences_unused" IS '闲置序号表';
|
||||
COMMENT ON COLUMN "sequences_unused"."key" IS '序号英文名称';
|
||||
COMMENT ON COLUMN "sequences_unused"."type" IS '序号类型';
|
||||
COMMENT ON COLUMN "sequences_unused"."seq" IS '闲置的的序号';
|
||||
COMMENT ON COLUMN "sequences_unused"."create_time" IS '设为闲置序号的时间';
|
||||
8
src/main/resources/mysql/create_table_sequences.sql
Normal file
8
src/main/resources/mysql/create_table_sequences.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS `sequences`
|
||||
(
|
||||
`key` VARCHAR(64) NOT NULL COMMENT '序号英文名称',
|
||||
`type` VARCHAR(64) NOT NULL COMMENT '序号类型',
|
||||
`seq` BIGINT(20) NOT NULL COMMENT '已使用到的序号',
|
||||
`update_time` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后使用时间',
|
||||
PRIMARY KEY (`key`, `type`)
|
||||
) COMMENT '当前序号表';
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS `sequences_unlock`
|
||||
(
|
||||
`key` VARCHAR(64) NOT NULL COMMENT '序号英文名称',
|
||||
`type` VARCHAR(64) NOT NULL COMMENT '序号类型',
|
||||
`seq` BIGINT(20) NOT NULL COMMENT '尚未锁定的序号',
|
||||
`create_time` DATETIME NOT NULL COMMENT '使用时间',
|
||||
PRIMARY KEY (`key`, `type`, `seq`)
|
||||
) COMMENT '未锁定序号表';
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS `sequences_unused`
|
||||
(
|
||||
`key` VARCHAR(64) NOT NULL COMMENT '序号英文名称',
|
||||
`type` VARCHAR(64) NOT NULL COMMENT '序号类型',
|
||||
`seq` BIGINT(20) NOT NULL COMMENT '闲置的的序号',
|
||||
`create_time` DATETIME NOT NULL COMMENT '设为闲置序号的时间',
|
||||
PRIMARY KEY (`key`, `type`, `seq`)
|
||||
) COMMENT '闲置序号表';
|
||||
13
src/main/resources/pgsql/create_table_sequences.sql
Normal file
13
src/main/resources/pgsql/create_table_sequences.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "sequences"
|
||||
(
|
||||
"key" VARCHAR(64) NOT NULL,
|
||||
"type" VARCHAR(64) NOT NULL,
|
||||
"seq" INT8 NOT NULL,
|
||||
"update_time" TIMESTAMP,
|
||||
PRIMARY KEY ("key", "type")
|
||||
);
|
||||
COMMENT ON TABLE "sequences" IS '当前序号表';
|
||||
COMMENT ON COLUMN "sequences"."key" IS '序号英文名称';
|
||||
COMMENT ON COLUMN "sequences"."type" IS '序号类型';
|
||||
COMMENT ON COLUMN "sequences"."seq" IS '已使用到的序号';
|
||||
COMMENT ON COLUMN "sequences"."update_time" IS '最后使用时间';
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE OR REPLACE FUNCTION SEQUENCES_UPDATE_TIME() RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW."update_time" := current_timestamp;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TRIGGER IF EXISTS SEQUENCES_UPDATE_TIME ON "sequences" CASCADE;
|
||||
CREATE TRIGGER SEQUENCES_UPDATE_TIME BEFORE INSERT OR UPDATE ON "sequences" FOR EACH ROW EXECUTE PROCEDURE SEQUENCES_UPDATE_TIME();
|
||||
13
src/main/resources/pgsql/create_table_sequences_unlock.sql
Normal file
13
src/main/resources/pgsql/create_table_sequences_unlock.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "sequences_unlock"
|
||||
(
|
||||
"key" VARCHAR(64) NOT NULL,
|
||||
"type" VARCHAR(64) NOT NULL,
|
||||
"seq" INT8 NOT NULL,
|
||||
"create_time" TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY ("key", "type", "seq")
|
||||
);
|
||||
COMMENT ON TABLE "sequences_unlock" IS '未锁定序号表';
|
||||
COMMENT ON COLUMN "sequences_unlock"."key" IS '序号英文名称';
|
||||
COMMENT ON COLUMN "sequences_unlock"."type" IS '序号类型';
|
||||
COMMENT ON COLUMN "sequences_unlock"."seq" IS '尚未锁定的序号';
|
||||
COMMENT ON COLUMN "sequences_unlock"."create_time" IS '使用时间';
|
||||
13
src/main/resources/pgsql/create_table_sequences_unused.sql
Normal file
13
src/main/resources/pgsql/create_table_sequences_unused.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS "sequences_unused"
|
||||
(
|
||||
"key" VARCHAR(64) NOT NULL,
|
||||
"type" VARCHAR(64) NOT NULL,
|
||||
"seq" INT8 NOT NULL,
|
||||
"create_time" TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY ("key", "type", "seq")
|
||||
);
|
||||
COMMENT ON TABLE "sequences_unused" IS '闲置序号表';
|
||||
COMMENT ON COLUMN "sequences_unused"."key" IS '序号英文名称';
|
||||
COMMENT ON COLUMN "sequences_unused"."type" IS '序号类型';
|
||||
COMMENT ON COLUMN "sequences_unused"."seq" IS '闲置的的序号';
|
||||
COMMENT ON COLUMN "sequences_unused"."create_time" IS '设为闲置序号的时间';
|
||||
@@ -1,15 +1,16 @@
|
||||
import com.kingbase8.ds.KBSimpleDataSource;
|
||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||
import com.yanghuanglin.seq.config.BaseConfig;
|
||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.generator.Generator;
|
||||
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import dm.jdbc.driver.DmdbDataSource;
|
||||
import org.junit.Test;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.postgresql.ds.PGSimpleDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
@@ -21,15 +22,14 @@ import java.util.concurrent.TimeUnit;
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
public class SeqTest {
|
||||
private static final MysqlDataSource dataSource = new MysqlDataSource();
|
||||
private static final DataSource dataSource;
|
||||
private static final Generator generator;
|
||||
|
||||
static {
|
||||
dataSource.setURL("jdbc:mysql://127.0.0.1:3306/sequence");
|
||||
dataSource.setUser("root");
|
||||
dataSource.setPassword("root");
|
||||
dataSource = dm();
|
||||
|
||||
GeneratorConfig generatorConfig = new GeneratorConfig(dataSource);
|
||||
System.out.println("DbType: " + generatorConfig.getDbType());
|
||||
|
||||
TableConfig tableConfig = new TableConfig();
|
||||
tableConfig.setTable("sequences");
|
||||
@@ -43,6 +43,43 @@ public class SeqTest {
|
||||
generator = new SequencesGenerator(generatorConfig);
|
||||
}
|
||||
|
||||
private static DataSource mysql() {
|
||||
MysqlDataSource mysqlDataSource = new MysqlDataSource();
|
||||
mysqlDataSource.setURL("jdbc:mysql://127.0.0.1:3306/sequence");
|
||||
mysqlDataSource.setUser("root");
|
||||
mysqlDataSource.setPassword("root");
|
||||
return mysqlDataSource;
|
||||
}
|
||||
|
||||
private static DataSource kingbase8() {
|
||||
KBSimpleDataSource kbDataSource = new KBSimpleDataSource();
|
||||
kbDataSource.setURL("jdbc:kingbase8://127.0.0.1:54321/cemis?currentSchema=sequence");
|
||||
kbDataSource.setUser("kingbase");
|
||||
kbDataSource.setPassword("kingbase");
|
||||
return kbDataSource;
|
||||
}
|
||||
|
||||
private static DataSource pgsql() {
|
||||
PGSimpleDataSource pgDataSource = new PGSimpleDataSource();
|
||||
pgDataSource.setURL("jdbc:postgresql://127.0.0.1:54321/cemis?currentSchema=sequence");
|
||||
pgDataSource.setUser("kingbase");
|
||||
pgDataSource.setPassword("kingbase");
|
||||
return pgDataSource;
|
||||
}
|
||||
|
||||
private static DataSource dm() {
|
||||
DmdbDataSource dmdbDataSource = new DmdbDataSource();
|
||||
dmdbDataSource.setURL("jdbc:dm://127.0.0.1:5236?schema=sequence");
|
||||
dmdbDataSource.setUser("SYSDBA");
|
||||
dmdbDataSource.setPassword("SYSDBA");
|
||||
return dmdbDataSource;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createTable() {
|
||||
generator.createTable();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateTest() {
|
||||
//释放未锁定序列号
|
||||
@@ -51,12 +88,17 @@ public class SeqTest {
|
||||
Set<String> set = new HashSet<>();
|
||||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(100));
|
||||
for (int i = 0; i < 5; i++) {
|
||||
final int finalI = i;
|
||||
threadPoolExecutor.execute(() -> {
|
||||
Sequences sequences = generator.generate("SNT", "MISSION");
|
||||
String formattedSeq = generator.format(sequences.getSeq(), "处〔#year#〕10801#seq#");
|
||||
String pattern = "处〔#year#〕#fix##seq#";
|
||||
String fix = "10801" + finalI;
|
||||
String formattedSeq = generator.format(sequences.getSeq(), fix, pattern);
|
||||
generator.lock(sequences);
|
||||
set.add(formattedSeq);
|
||||
System.out.println(formattedSeq);
|
||||
Sequences parse = generator.parse(formattedSeq, pattern, fix.length());
|
||||
System.out.println(parse);
|
||||
});
|
||||
}
|
||||
threadPoolExecutor.shutdown();
|
||||
@@ -80,10 +122,11 @@ public class SeqTest {
|
||||
|
||||
@Test
|
||||
public void parseTest() {
|
||||
String seqPattern = "ZZF#year##month##seq#";
|
||||
String formatted = "ZZF20220200008";
|
||||
String seqPattern = "ZZF#year##month##fix##seq#";
|
||||
String formatted = "ZZF202202A18";
|
||||
|
||||
Sequences sequences = generator.parse(formatted, seqPattern);
|
||||
//A1视为固定字符串,其长度为2
|
||||
Sequences sequences = generator.parse(formatted, seqPattern, 2);
|
||||
|
||||
String key = "zzfCode" + sequences.getYear();
|
||||
|
||||
@@ -91,4 +134,35 @@ public class SeqTest {
|
||||
sequences.setType("MISSION");
|
||||
System.out.println(sequences);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unusedTest() {
|
||||
Sequences sequences = new Sequences();
|
||||
sequences.setKey("0010001$distrainCode2024");
|
||||
sequences.setType("MISSION");
|
||||
sequences.setSeq(1L);
|
||||
boolean result = generator.unused(sequences);
|
||||
assert result;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lockTest() {
|
||||
generator.generate("0010001$distrainCode2024","MISSION");
|
||||
Sequences sequences = new Sequences();
|
||||
sequences.setKey("0010001$distrainCode2024");
|
||||
sequences.setType("MISSION");
|
||||
sequences.setSeq(1L);
|
||||
boolean result = generator.lock(sequences);
|
||||
assert result;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lockedTest() {
|
||||
Sequences sequences = new Sequences();
|
||||
sequences.setKey("0010001$distrainCode2024");
|
||||
sequences.setType("MISSION");
|
||||
sequences.setSeq(1L);
|
||||
boolean result = generator.locked(sequences);
|
||||
System.out.println("locked: " + result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user