Compare commits
5 Commits
54e2f9dffb
...
1.4.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19fb15bd03 | ||
|
|
956e707399 | ||
|
|
6bfcb1fd51 | ||
|
|
f6750280d8 | ||
|
|
0fa5cc1e7e |
173
README.md
173
README.md
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
使用方法:
|
使用方法:
|
||||||
|
|
||||||
+ 在项目中放置jar包的地方把seq-1.1.0.jar、seq-1.1.0-sources.jar、seq-1.1.0-pom.xml复制过去
|
+ 在项目中放置jar包的地方把seq-1.4.1.jar、seq-1.4.1-sources.jar、seq-1.4.1-pom.xml复制过去
|
||||||
+ 在pom.xml中增加以下内容,然后执行maven命令:mvn clean
|
+ 在pom.xml中增加以下内容,然后执行maven命令:mvn clean
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.yanghuanglin</groupId>
|
<groupId>com.yanghuanglin</groupId>
|
||||||
<artifactId>seq</artifactId>
|
<artifactId>seq</artifactId>
|
||||||
<version>1.0.0</version>
|
<version>1.4.1</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<!-- 如若你项目中有引用spring-jdbc,则需要排除seq的jdbc依赖 -->
|
<!-- 如若你项目中有引用spring-jdbc,则需要排除seq的jdbc依赖 -->
|
||||||
<exclusion>
|
<exclusion>
|
||||||
@@ -50,13 +50,13 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<!-- ${project.basedir}表示当前项目的根目录 -->
|
<!-- ${project.basedir}表示当前项目的根目录 -->
|
||||||
<file>${project.basedir}/lib/seq-1.1.0.jar</file>
|
<file>${project.basedir}/lib/seq-1.4.1.jar</file>
|
||||||
<pomFile>${pom.basedir}/lib/seq-1.1.0-pom.xml</pomFile>
|
<pomFile>${pom.basedir}/lib/seq-1.4.1-pom.xml</pomFile>
|
||||||
<sources>${project.basedir}/lib/seq-1.1.0-sources.jar</sources>
|
<sources>${project.basedir}/lib/seq-1.4.1-sources.jar</sources>
|
||||||
<repositoryLayout>default</repositoryLayout>
|
<repositoryLayout>default</repositoryLayout>
|
||||||
<groupId>com.yanghuanglin</groupId>
|
<groupId>com.yanghuanglin</groupId>
|
||||||
<artifactId>seq</artifactId>
|
<artifactId>seq</artifactId>
|
||||||
<version>1.0.0</version>
|
<version>1.4.1</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<generatePom>true</generatePom>
|
<generatePom>true</generatePom>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -71,10 +71,10 @@
|
|||||||
+ springboot中配置方式一(优先):直接注入已有jdbcTemplate和transactionTemplate
|
+ springboot中配置方式一(优先):直接注入已有jdbcTemplate和transactionTemplate
|
||||||
|
|
||||||
```java
|
```java
|
||||||
package com.yanghuanglin.springseq.config;
|
package com.yanghuanglin.springseq.baseConfig;
|
||||||
|
|
||||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
import com.yanghuanglin.seq.baseConfig.GeneratorConfig;
|
||||||
import com.yanghuanglin.seq.config.TableConfig;
|
import com.yanghuanglin.seq.baseConfig.TableConfig;
|
||||||
import com.yanghuanglin.seq.generator.Generator;
|
import com.yanghuanglin.seq.generator.Generator;
|
||||||
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -107,11 +107,11 @@ public class SeqGeneratorConfig {
|
|||||||
public TableConfig tableConfig() {
|
public TableConfig tableConfig() {
|
||||||
TableConfig tableConfig = new TableConfig();
|
TableConfig tableConfig = new TableConfig();
|
||||||
//自定义表名、字段名
|
//自定义表名、字段名
|
||||||
//tableConfig.setTable("sequences");
|
tableConfig.setTable("sequences");
|
||||||
//tableConfig.setKeyColumn("SEQUENCE_KEY");
|
tableConfig.setKeyColumn("SEQUENCE_KEY");
|
||||||
//tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
||||||
//tableConfig.setSeqColumn("SEQUENCE_NEXT_ID");
|
tableConfig.setSeqColumn("NEXT_ID");
|
||||||
//tableConfig.setCreateTimeColumn("CREATE_TIME");
|
tableConfig.setCreateTimeColumn("CREATE_TIME");
|
||||||
return tableConfig;
|
return tableConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,10 +142,10 @@ public class SeqGeneratorConfig {
|
|||||||
+ springboot中配置方式二:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
+ springboot中配置方式二:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
||||||
|
|
||||||
```java
|
```java
|
||||||
package com.yanghuanglin.springseq.config;
|
package com.yanghuanglin.springseq.baseConfig;
|
||||||
|
|
||||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
import com.yanghuanglin.seq.baseConfig.GeneratorConfig;
|
||||||
import com.yanghuanglin.seq.config.TableConfig;
|
import com.yanghuanglin.seq.baseConfig.TableConfig;
|
||||||
import com.yanghuanglin.seq.generator.Generator;
|
import com.yanghuanglin.seq.generator.Generator;
|
||||||
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -172,11 +172,11 @@ public class SeqGeneratorConfig {
|
|||||||
public TableConfig tableConfig() {
|
public TableConfig tableConfig() {
|
||||||
TableConfig tableConfig = new TableConfig();
|
TableConfig tableConfig = new TableConfig();
|
||||||
//自定义表名、字段名
|
//自定义表名、字段名
|
||||||
//tableConfig.setTable("sequences");
|
tableConfig.setTable("sequences");
|
||||||
//tableConfig.setKeyColumn("SEQUENCE_KEY");
|
tableConfig.setKeyColumn("SEQUENCE_KEY");
|
||||||
//tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
||||||
//tableConfig.setSeqColumn("SEQUENCE_NEXT_ID");
|
tableConfig.setSeqColumn("NEXT_ID");
|
||||||
//tableConfig.setCreateTimeColumn("CREATE_TIME");
|
tableConfig.setCreateTimeColumn("CREATE_TIME");
|
||||||
return tableConfig;
|
return tableConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,6 +184,7 @@ public class SeqGeneratorConfig {
|
|||||||
* 序号生成器配置类
|
* 序号生成器配置类
|
||||||
* @param tableConfig 序号表配置类
|
* @param tableConfig 序号表配置类
|
||||||
*/
|
*/
|
||||||
|
@DependsOn("tableConfig")
|
||||||
@Bean
|
@Bean
|
||||||
public GeneratorConfig generatorConfig(TableConfig tableConfig) {
|
public GeneratorConfig generatorConfig(TableConfig tableConfig) {
|
||||||
GeneratorConfig generatorConfig = new GeneratorConfig();
|
GeneratorConfig generatorConfig = new GeneratorConfig();
|
||||||
@@ -195,6 +196,7 @@ public class SeqGeneratorConfig {
|
|||||||
* 注册序号生成器类
|
* 注册序号生成器类
|
||||||
* @param generatorConfig 序号生成器配置类
|
* @param generatorConfig 序号生成器配置类
|
||||||
*/
|
*/
|
||||||
|
@DependsOn("generatorConfig")
|
||||||
@Bean
|
@Bean
|
||||||
public Generator generator(GeneratorConfig generatorConfig) {
|
public Generator generator(GeneratorConfig generatorConfig) {
|
||||||
return new SequencesGenerator(generatorConfig);
|
return new SequencesGenerator(generatorConfig);
|
||||||
@@ -205,7 +207,7 @@ public class SeqGeneratorConfig {
|
|||||||
+ 使用:
|
+ 使用:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
package com.yanghuanglin.springseq.config;
|
package com.yanghuanglin.springseq.baseConfig;
|
||||||
|
|
||||||
import com.yanghuanglin.seq.generator.Generator;
|
import com.yanghuanglin.seq.generator.Generator;
|
||||||
import com.yanghuanglin.seq.po.Sequences;
|
import com.yanghuanglin.seq.po.Sequences;
|
||||||
@@ -272,17 +274,19 @@ TableConfig配置项,通过set方法设置(一般不用改,如果已有相
|
|||||||
GeneratorConfig配置项,通过set方法设置
|
GeneratorConfig配置项,通过set方法设置
|
||||||
|
|
||||||
| 配置项 | 类型 | 默认值 | 说明 |
|
| 配置项 | 类型 | 默认值 | 说明 |
|
||||||
|---------------------|------------------------------------------------------------------|------------------|----------|
|
|---------------------|------------------------------------------------------------------|------------------|------------------|
|
||||||
| dataSource | javax.sql.DataSource | null | 数据源 |
|
| dataSource | javax.sql.DataSource | null | 数据源 |
|
||||||
| jdbcTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 数据库操作模板 |
|
| jdbcTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 数据库操作模板 |
|
||||||
| transactionTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 事务操作模板 |
|
| transactionTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 事务操作模板 |
|
||||||
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
|
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
|
||||||
| autoCreate | Boolean | true | 开启自动建表 |
|
| autoCreate | Boolean | true | 开启自动建表 |
|
||||||
| step | Integer | 1 | 序号增加时的步长 |
|
| step | Integer | 1 | 序号增加时的步长 |
|
||||||
| tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 |
|
| type | String | DEFAULT | 默认序号类型 |
|
||||||
|
| minLength | Integer | 1 | 默认序号格式化后长度,不足的补零 |
|
||||||
|
| tableConfig | com.yanghuanglin.seq.baseConfig.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||||
|
|
||||||
以上配置中,jdbcTemplate和transactionTemplate优先级最高,如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置,则dataSource和transactionManager无效;
|
以上配置中,jdbcTemplate和transactionTemplate优先级最高,如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置,则dataSource和transactionManager无效;
|
||||||
进行这几种组合:dataSource+autoCreate+step+tableConfig,jdbcTemplate+transactionTemplate+autoCreate+step+tableConfig,jdbcTemplate+transactionManager+autoCreate+step+tableConfig
|
可进行这几种组合:dataSource+autoCreate+step+minLength+tableConfig,jdbcTemplate+transactionTemplate+autoCreate+step+minLength+tableConfig,jdbcTemplate+transactionManager+autoCreate+step+minLength+tableConfig
|
||||||
|
|
||||||
---
|
---
|
||||||
Generator方法如下:
|
Generator方法如下:
|
||||||
@@ -290,6 +294,9 @@ Generator方法如下:
|
|||||||
```java
|
```java
|
||||||
package com.yanghuanglin.seq.generator;
|
package com.yanghuanglin.seq.generator;
|
||||||
|
|
||||||
|
import com.yanghuanglin.seq.config.BaseConfig;
|
||||||
|
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||||
|
import com.yanghuanglin.seq.enums.FormatPlaceholder;
|
||||||
import com.yanghuanglin.seq.po.Sequences;
|
import com.yanghuanglin.seq.po.Sequences;
|
||||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||||
@@ -298,21 +305,17 @@ import java.util.Date;
|
|||||||
|
|
||||||
public interface Generator {
|
public interface Generator {
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的年
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
|
* <p/>
|
||||||
|
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找不到记录,说明该组合的序号对象还未初次生成,返回的是seq为step的序号对象,该对象数据会写入到{@link SequencesUnlock}中。
|
||||||
|
* <p/>
|
||||||
|
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除,然后写入到{@link SequencesUnlock}中。
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @param key 数据字典中的编码
|
||||||
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
String YEAR = "#year#";
|
Sequences generate(String key);
|
||||||
/**
|
|
||||||
* 序号格式字符中的月
|
|
||||||
*/
|
|
||||||
String MONTH = "#month#";
|
|
||||||
/**
|
|
||||||
* 序号格式字符中的日
|
|
||||||
*/
|
|
||||||
String DAY = "#day#";
|
|
||||||
/**
|
|
||||||
* 序号格式字符中的格式化后的序号
|
|
||||||
*/
|
|
||||||
String SEQ = "#seq#";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据传入的key和type生成可用的序号对象。
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
@@ -340,10 +343,48 @@ public interface Generator {
|
|||||||
*/
|
*/
|
||||||
String generate(String key, String type, Integer minLength);
|
String generate(String key, String type, Integer minLength);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
*
|
||||||
|
* @param sequences 生成的序号对象
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的字符串
|
||||||
|
*/
|
||||||
|
String format(Sequences sequences, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
*
|
||||||
|
* @param sequences 生成的序号对象
|
||||||
|
* @param minLength 序号数字最小长度
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的字符串
|
||||||
|
*/
|
||||||
|
String format(Sequences sequences, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR#getPlaceholder()}(当前年份)、{@link FormatPlaceholder#MONTH#getPlaceholder()}(当前月份)、{@link FormatPlaceholder#DAY#getPlaceholder()}(当前日期)、{@link FormatPlaceholder#SEQ#getPlaceholder()}(生成的字符串序号)四个变量
|
||||||
|
* <p/>
|
||||||
|
* seq为1,pattern为#year##month##day#6#seq#,则会格式化为2022013061。此序号含义如下:
|
||||||
|
* <p/>
|
||||||
|
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为{@link BaseConfig#getMinLength()}设置,默认为1,不足则补零]
|
||||||
|
*
|
||||||
|
* @param seq 需要格式化的序号
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的序号字符串
|
||||||
|
*/
|
||||||
|
String format(Long seq, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将生成的序号对象格式化为指定格式
|
* 将生成的序号对象格式化为指定格式
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link #YEAR}(当前年份)、{@link #MONTH}(当前月份)、{@link #DAY}(当前日期)、{@link #SEQ}(生成的字符串序号)四个变量
|
* pattern支持:{@link FormatPlaceholder#YEAR}}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
* <p/>
|
* <p/>
|
||||||
* seq为1,minLength为4,pattern为#year##month##day#6#seq#,则会格式化为2022013060001。此序号含义如下:
|
* seq为1,minLength为4,pattern为#year##month##day#6#seq#,则会格式化为2022013060001。此序号含义如下:
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -352,14 +393,30 @@ public interface Generator {
|
|||||||
* @param seq 需要格式化的序号
|
* @param seq 需要格式化的序号
|
||||||
* @param minLength 序号最小长度,不足的会补零
|
* @param minLength 序号最小长度,不足的会补零
|
||||||
* @param pattern 格式
|
* @param pattern 格式
|
||||||
* @return 格式化后的字符串
|
* @return 格式化后的序号字符串
|
||||||
*/
|
*/
|
||||||
String format(Long seq, Integer minLength, String pattern);
|
String format(Long seq, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
* <p/>
|
||||||
|
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013061。此序号含义如下:
|
||||||
|
* <p/>
|
||||||
|
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为{@link BaseConfig#getMinLength()}设置,默认为1,不足则补零]
|
||||||
|
*
|
||||||
|
* @param seq 需要格式化的序号
|
||||||
|
* @param start 序号格式化后以什么字符串开头
|
||||||
|
* @param pattern 序号格式
|
||||||
|
* @return 格式化后的序号字符串
|
||||||
|
*/
|
||||||
|
String format(Long seq, String start, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将生成的序号对象格式化为指定格式
|
* 将生成的序号对象格式化为指定格式
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link #YEAR}(当前年份)、{@link #MONTH}(当前月份)、{@link #DAY}(当前日期)、{@link #SEQ}(生成的字符串序号)四个变量
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
* <p/>
|
* <p/>
|
||||||
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -368,11 +425,30 @@ public interface Generator {
|
|||||||
* @param seq 需要格式化的序号
|
* @param seq 需要格式化的序号
|
||||||
* @param start 序号格式化后以什么字符串开头
|
* @param start 序号格式化后以什么字符串开头
|
||||||
* @param minLength 序号最小长度,不足的会补零
|
* @param minLength 序号最小长度,不足的会补零
|
||||||
* @param pattern 格式
|
* @param pattern 序号格式
|
||||||
* @return 格式化后的字符串
|
* @return 格式化后的序号字符串
|
||||||
*/
|
*/
|
||||||
String format(Long seq, String start, Integer minLength, String pattern);
|
String format(Long seq, String start, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将已格式化的序号解析为序号对象
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@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()}可能有值
|
||||||
|
* <p/>
|
||||||
|
* 如果生成序号时,序号的key在年、月、日上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合,进而得到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 序号格式
|
||||||
|
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key需要根据情况手动设置,type为{@link GeneratorConfig#getType()}的默认值
|
||||||
|
*/
|
||||||
|
Sequences parse(String formatted, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -401,6 +477,13 @@ public interface Generator {
|
|||||||
* @param end 结束时间
|
* @param end 结束时间
|
||||||
*/
|
*/
|
||||||
void release(Date begin, Date end);
|
void release(Date begin, Date end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
|
||||||
|
*
|
||||||
|
* @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象
|
||||||
|
*/
|
||||||
|
void release(Sequences sequences);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.yanghuanglin</groupId>
|
<groupId>com.yanghuanglin</groupId>
|
||||||
<artifactId>seq</artifactId>
|
<artifactId>seq</artifactId>
|
||||||
<version>1.1.0</version>
|
<version>1.4.1</version>
|
||||||
<name>seq</name>
|
<name>seq</name>
|
||||||
<description>seq</description>
|
<description>seq</description>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
170
src/main/java/com/yanghuanglin/seq/config/BaseConfig.java
Normal file
170
src/main/java/com/yanghuanglin/seq/config/BaseConfig.java
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
package com.yanghuanglin.seq.config;
|
||||||
|
|
||||||
|
import com.yanghuanglin.seq.dao.SequencesDao;
|
||||||
|
import com.yanghuanglin.seq.dao.SequencesUnlockDao;
|
||||||
|
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 org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成器最后的基础配置信息,单例模式,便于其他地方调用
|
||||||
|
*
|
||||||
|
* @author yanghuanglin
|
||||||
|
* @since 2022/2/14
|
||||||
|
*/
|
||||||
|
public class BaseConfig {
|
||||||
|
private static volatile BaseConfig instance;
|
||||||
|
|
||||||
|
private TransactionTemplate transactionTemplate;
|
||||||
|
private SequencesDao sequencesDao;
|
||||||
|
private SequencesUnusedDao sequencesUnusedDao;
|
||||||
|
private SequencesUnlockDao sequencesUnlockDao;
|
||||||
|
private Integer step;
|
||||||
|
private String type;
|
||||||
|
private Integer minLength;
|
||||||
|
|
||||||
|
private BaseConfig() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseConfig getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
synchronized (BaseConfig.class) {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new BaseConfig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseConfig getInstance(GeneratorConfig generatorConfig) {
|
||||||
|
if (instance == null) {
|
||||||
|
synchronized (BaseConfig.class) {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new BaseConfig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
instance.init(generatorConfig);
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setInstance(BaseConfig instance) {
|
||||||
|
BaseConfig.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionTemplate getTransactionTemplate() {
|
||||||
|
if (transactionTemplate == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return transactionTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
|
||||||
|
this.transactionTemplate = transactionTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SequencesDao getSequencesDao() {
|
||||||
|
if (sequencesDao == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return sequencesDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSequencesDao(SequencesDao sequencesDao) {
|
||||||
|
this.sequencesDao = sequencesDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SequencesUnusedDao getSequencesUnusedDao() {
|
||||||
|
if (sequencesUnusedDao == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return sequencesUnusedDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSequencesUnusedDao(SequencesUnusedDao sequencesUnusedDao) {
|
||||||
|
this.sequencesUnusedDao = sequencesUnusedDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SequencesUnlockDao getSequencesUnlockDao() {
|
||||||
|
if (sequencesUnlockDao == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return sequencesUnlockDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSequencesUnlockDao(SequencesUnlockDao sequencesUnlockDao) {
|
||||||
|
this.sequencesUnlockDao = sequencesUnlockDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getStep() {
|
||||||
|
if (step == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStep(Integer step) {
|
||||||
|
this.step = step;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
if (type == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMinLength() {
|
||||||
|
if (minLength == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return minLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinLength(Integer minLength) {
|
||||||
|
this.minLength = minLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init(GeneratorConfig generatorConfig) {
|
||||||
|
//数据库操作模板
|
||||||
|
JdbcTemplate jdbcTemplate = generatorConfig.getJdbcTemplate();
|
||||||
|
|
||||||
|
if (jdbcTemplate == null) {
|
||||||
|
//数据源
|
||||||
|
DataSource dataSource = generatorConfig.getDataSource();
|
||||||
|
if (dataSource == null)
|
||||||
|
//若数据库操作模板为空,也没有配置数据源,则抛出异常
|
||||||
|
throw new NullPointerException("数据源不能为空");
|
||||||
|
//否则以数据源创建数据库操作模板
|
||||||
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (generatorConfig.getTransactionTemplate() == null) {
|
||||||
|
//若没有配置事务操作模板,则从配置中取事务管理器
|
||||||
|
DataSourceTransactionManager transactionManager = generatorConfig.getTransactionManager();
|
||||||
|
if (transactionManager == null) {
|
||||||
|
//若未配置事务管理器,则通过数据源新建
|
||||||
|
DataSource dataSource = jdbcTemplate.getDataSource();
|
||||||
|
if (dataSource == null)
|
||||||
|
throw new NullPointerException("数据源不能为空");
|
||||||
|
transactionManager = new DataSourceTransactionManager(dataSource);
|
||||||
|
}
|
||||||
|
//通过事务管理器创建事务操作模板
|
||||||
|
transactionTemplate = new TransactionTemplate(transactionManager);
|
||||||
|
} else {
|
||||||
|
//获取事务操作模板
|
||||||
|
transactionTemplate = generatorConfig.getTransactionTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
|
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
|
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
|
this.step = generatorConfig.getStep();
|
||||||
|
this.type = generatorConfig.getType();
|
||||||
|
this.minLength = generatorConfig.getMinLength();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,6 +43,16 @@ public class GeneratorConfig {
|
|||||||
*/
|
*/
|
||||||
private Integer step = 1;
|
private Integer step = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化后序号字符串的最小长度,不足的部分补零
|
||||||
|
*/
|
||||||
|
private Integer minLength = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认序号类型
|
||||||
|
*/
|
||||||
|
private String type="DEFAULT";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 表和字段配置
|
* 表和字段配置
|
||||||
*/
|
*/
|
||||||
@@ -95,6 +105,22 @@ public class GeneratorConfig {
|
|||||||
this.autoCreate = autoCreate;
|
this.autoCreate = autoCreate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getMinLength() {
|
||||||
|
return minLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinLength(Integer minLength) {
|
||||||
|
this.minLength = minLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
public Integer getStep() {
|
public Integer getStep() {
|
||||||
return step;
|
return step;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.yanghuanglin.seq.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式占位符枚举
|
||||||
|
*
|
||||||
|
* @author yanghuanglin
|
||||||
|
* @since 2022/2/14
|
||||||
|
*/
|
||||||
|
public enum FormatPlaceholder {
|
||||||
|
/**
|
||||||
|
* 序号格式字符中的年
|
||||||
|
*/
|
||||||
|
YEAR("#year#"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序号格式字符中的月
|
||||||
|
*/
|
||||||
|
MONTH("#month#"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序号格式字符中的日
|
||||||
|
*/
|
||||||
|
DAY("#day#"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序号格式字符中的格式化后的序号
|
||||||
|
*/
|
||||||
|
SEQ("#seq#");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用toString()后的字符串,这个字符串用于正则匹配(替换格式字符串中的对应占位符)和反向查找(将占位符字符串转换为对应枚举)
|
||||||
|
*/
|
||||||
|
private final String placeholder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造函数
|
||||||
|
*
|
||||||
|
* @param placeholder 格式占位符
|
||||||
|
*/
|
||||||
|
FormatPlaceholder(String placeholder) {
|
||||||
|
this.placeholder = placeholder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlaceholder() {
|
||||||
|
return placeholder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将格式占位符转为对应枚举
|
||||||
|
*
|
||||||
|
* @param placeholder 格式占位符
|
||||||
|
* @return 对应枚举
|
||||||
|
*/
|
||||||
|
public static FormatPlaceholder of(String placeholder) {
|
||||||
|
FormatPlaceholder[] enumConstants = FormatPlaceholder.class.getEnumConstants();
|
||||||
|
for (FormatPlaceholder enumConstant : enumConstants) {
|
||||||
|
if (enumConstant.getPlaceholder().equals(placeholder))
|
||||||
|
return enumConstant;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.yanghuanglin.seq.generator;
|
package com.yanghuanglin.seq.generator;
|
||||||
|
|
||||||
|
import com.yanghuanglin.seq.config.BaseConfig;
|
||||||
|
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||||
|
import com.yanghuanglin.seq.enums.FormatPlaceholder;
|
||||||
import com.yanghuanglin.seq.po.Sequences;
|
import com.yanghuanglin.seq.po.Sequences;
|
||||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||||
@@ -8,21 +11,17 @@ import java.util.Date;
|
|||||||
|
|
||||||
public interface Generator {
|
public interface Generator {
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的年
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
|
* <p/>
|
||||||
|
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找不到记录,说明该组合的序号对象还未初次生成,返回的是seq为step的序号对象,该对象数据会写入到{@link SequencesUnlock}中。
|
||||||
|
* <p/>
|
||||||
|
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除,然后写入到{@link SequencesUnlock}中。
|
||||||
|
* <p/>
|
||||||
|
*
|
||||||
|
* @param key 数据字典中的编码
|
||||||
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
String YEAR = "#year#";
|
Sequences generate(String key);
|
||||||
/**
|
|
||||||
* 序号格式字符中的月
|
|
||||||
*/
|
|
||||||
String MONTH = "#month#";
|
|
||||||
/**
|
|
||||||
* 序号格式字符中的日
|
|
||||||
*/
|
|
||||||
String DAY = "#day#";
|
|
||||||
/**
|
|
||||||
* 序号格式字符中的格式化后的序号
|
|
||||||
*/
|
|
||||||
String SEQ = "#seq#";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据传入的key和type生成可用的序号对象。
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
@@ -50,8 +49,21 @@ public interface Generator {
|
|||||||
*/
|
*/
|
||||||
String generate(String key, String type, Integer minLength);
|
String generate(String key, String type, Integer minLength);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
*
|
||||||
|
* @param sequences 生成的序号对象
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的字符串
|
||||||
|
*/
|
||||||
|
String format(Sequences sequences, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
* @param sequences 生成的序号对象
|
* @param sequences 生成的序号对象
|
||||||
* @param minLength 序号数字最小长度
|
* @param minLength 序号数字最小长度
|
||||||
@@ -60,10 +72,25 @@ public interface Generator {
|
|||||||
*/
|
*/
|
||||||
String format(Sequences sequences, Integer minLength, String pattern);
|
String format(Sequences sequences, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR#getPlaceholder()}(当前年份)、{@link FormatPlaceholder#MONTH#getPlaceholder()}(当前月份)、{@link FormatPlaceholder#DAY#getPlaceholder()}(当前日期)、{@link FormatPlaceholder#SEQ#getPlaceholder()}(生成的字符串序号)四个变量
|
||||||
|
* <p/>
|
||||||
|
* seq为1,pattern为#year##month##day#6#seq#,则会格式化为2022013061。此序号含义如下:
|
||||||
|
* <p/>
|
||||||
|
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为{@link BaseConfig#getMinLength()}设置,默认为1,不足则补零]
|
||||||
|
*
|
||||||
|
* @param seq 需要格式化的序号
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的序号字符串
|
||||||
|
*/
|
||||||
|
String format(Long seq, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将生成的序号对象格式化为指定格式
|
* 将生成的序号对象格式化为指定格式
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link #YEAR}(当前年份)、{@link #MONTH}(当前月份)、{@link #DAY}(当前日期)、{@link #SEQ}(生成的字符串序号)四个变量
|
* pattern支持:{@link FormatPlaceholder#YEAR}}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
* <p/>
|
* <p/>
|
||||||
* seq为1,minLength为4,pattern为#year##month##day#6#seq#,则会格式化为2022013060001。此序号含义如下:
|
* seq为1,minLength为4,pattern为#year##month##day#6#seq#,则会格式化为2022013060001。此序号含义如下:
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -72,14 +99,30 @@ public interface Generator {
|
|||||||
* @param seq 需要格式化的序号
|
* @param seq 需要格式化的序号
|
||||||
* @param minLength 序号最小长度,不足的会补零
|
* @param minLength 序号最小长度,不足的会补零
|
||||||
* @param pattern 格式
|
* @param pattern 格式
|
||||||
* @return 格式化后的字符串
|
* @return 格式化后的序号字符串
|
||||||
*/
|
*/
|
||||||
String format(Long seq, Integer minLength, String pattern);
|
String format(Long seq, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将生成的序号对象格式化为指定格式,格式化后字符串最小长度为{@link BaseConfig#getMinLength()},不足则补零
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
* <p/>
|
||||||
|
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013061。此序号含义如下:
|
||||||
|
* <p/>
|
||||||
|
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为{@link BaseConfig#getMinLength()}设置,默认为1,不足则补零]
|
||||||
|
*
|
||||||
|
* @param seq 需要格式化的序号
|
||||||
|
* @param start 序号格式化后以什么字符串开头
|
||||||
|
* @param pattern 序号格式
|
||||||
|
* @return 格式化后的序号字符串
|
||||||
|
*/
|
||||||
|
String format(Long seq, String start, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将生成的序号对象格式化为指定格式
|
* 将生成的序号对象格式化为指定格式
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link #YEAR}(当前年份)、{@link #MONTH}(当前月份)、{@link #DAY}(当前日期)、{@link #SEQ}(生成的字符串序号)四个变量
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
* <p/>
|
* <p/>
|
||||||
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -88,11 +131,30 @@ public interface Generator {
|
|||||||
* @param seq 需要格式化的序号
|
* @param seq 需要格式化的序号
|
||||||
* @param start 序号格式化后以什么字符串开头
|
* @param start 序号格式化后以什么字符串开头
|
||||||
* @param minLength 序号最小长度,不足的会补零
|
* @param minLength 序号最小长度,不足的会补零
|
||||||
* @param pattern 格式
|
* @param pattern 序号格式
|
||||||
* @return 格式化后的字符串
|
* @return 格式化后的序号字符串
|
||||||
*/
|
*/
|
||||||
String format(Long seq, String start, Integer minLength, String pattern);
|
String format(Long seq, String start, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将已格式化的序号解析为序号对象
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@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()}可能有值
|
||||||
|
* <p/>
|
||||||
|
* 如果生成序号时,序号的key在年、月、日上有关联(如每年每月的序号要从1开始),则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合,进而得到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 序号格式
|
||||||
|
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key需要根据情况手动设置,type为{@link GeneratorConfig#getType()}的默认值
|
||||||
|
*/
|
||||||
|
Sequences parse(String formatted, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
package com.yanghuanglin.seq.generator.impl;
|
package com.yanghuanglin.seq.generator.impl;
|
||||||
|
|
||||||
|
import com.yanghuanglin.seq.config.BaseConfig;
|
||||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||||
import com.yanghuanglin.seq.dao.SequencesDao;
|
import com.yanghuanglin.seq.dao.SequencesDao;
|
||||||
import com.yanghuanglin.seq.dao.SequencesUnlockDao;
|
import com.yanghuanglin.seq.dao.SequencesUnlockDao;
|
||||||
import com.yanghuanglin.seq.dao.SequencesUnusedDao;
|
import com.yanghuanglin.seq.dao.SequencesUnusedDao;
|
||||||
import com.yanghuanglin.seq.dao.impl.SequencesDaoImpl;
|
import com.yanghuanglin.seq.enums.FormatPlaceholder;
|
||||||
import com.yanghuanglin.seq.dao.impl.SequencesUnlockDaoImpl;
|
|
||||||
import com.yanghuanglin.seq.dao.impl.SequencesUnusedDaoImpl;
|
|
||||||
import com.yanghuanglin.seq.generator.Generator;
|
import com.yanghuanglin.seq.generator.Generator;
|
||||||
import com.yanghuanglin.seq.po.Sequences;
|
import com.yanghuanglin.seq.po.Sequences;
|
||||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
||||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
|
||||||
import org.springframework.transaction.support.TransactionTemplate;
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import static com.yanghuanglin.seq.enums.FormatPlaceholder.*;
|
||||||
|
|
||||||
public class SequencesGenerator implements Generator {
|
public class SequencesGenerator implements Generator {
|
||||||
private final TransactionTemplate transactionTemplate;
|
private final TransactionTemplate transactionTemplate;
|
||||||
@@ -28,50 +28,29 @@ public class SequencesGenerator implements Generator {
|
|||||||
private final SequencesUnusedDao sequencesUnusedDao;
|
private final SequencesUnusedDao sequencesUnusedDao;
|
||||||
private final SequencesUnlockDao sequencesUnlockDao;
|
private final SequencesUnlockDao sequencesUnlockDao;
|
||||||
private final Integer step;
|
private final Integer step;
|
||||||
|
private final String type;
|
||||||
|
private final Integer minLength;
|
||||||
|
|
||||||
public SequencesGenerator(GeneratorConfig generatorConfig) {
|
public SequencesGenerator(GeneratorConfig generatorConfig) {
|
||||||
//数据库操作模板
|
BaseConfig baseConfig = BaseConfig.getInstance(generatorConfig);
|
||||||
JdbcTemplate jdbcTemplate = generatorConfig.getJdbcTemplate();
|
|
||||||
|
|
||||||
if (jdbcTemplate == null) {
|
this.transactionTemplate = baseConfig.getTransactionTemplate();
|
||||||
//数据源
|
this.sequencesDao = baseConfig.getSequencesDao();
|
||||||
DataSource dataSource = generatorConfig.getDataSource();
|
this.sequencesUnusedDao = baseConfig.getSequencesUnusedDao();
|
||||||
if (dataSource == null)
|
this.sequencesUnlockDao = baseConfig.getSequencesUnlockDao();
|
||||||
//若数据库操作模板为空,也没有配置数据源,则抛出异常
|
this.step = baseConfig.getStep();
|
||||||
throw new NullPointerException("数据源不能为空");
|
this.type = baseConfig.getType();
|
||||||
//否则以数据源创建数据库操作模板
|
this.minLength = baseConfig.getMinLength();
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (generatorConfig.getTransactionTemplate() == null) {
|
createTable(generatorConfig.getAutoCreate());
|
||||||
//若没有配置事务操作模板,则从配置中取事务管理器
|
|
||||||
DataSourceTransactionManager transactionManager = generatorConfig.getTransactionManager();
|
|
||||||
if (transactionManager == null) {
|
|
||||||
//若未配置事务管理器,则通过数据源新建
|
|
||||||
DataSource dataSource = jdbcTemplate.getDataSource();
|
|
||||||
if (dataSource == null)
|
|
||||||
throw new NullPointerException("数据源不能为空");
|
|
||||||
transactionManager = new JdbcTransactionManager(dataSource);
|
|
||||||
}
|
|
||||||
//通过事务管理器创建事务操作模板
|
|
||||||
transactionTemplate = new TransactionTemplate(transactionManager);
|
|
||||||
} else {
|
|
||||||
//获取事务操作模板
|
|
||||||
transactionTemplate = generatorConfig.getTransactionTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
|
||||||
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
|
||||||
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
|
||||||
this.step = generatorConfig.getStep();
|
|
||||||
|
|
||||||
autoCreateTable(generatorConfig.getAutoCreate());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自动创建需要的表
|
* 创建需要的表
|
||||||
|
*
|
||||||
|
* @param autoCreate 是否自动创建
|
||||||
*/
|
*/
|
||||||
private void autoCreateTable(Boolean autoCreate) {
|
private void createTable(Boolean autoCreate) {
|
||||||
if (!autoCreate)
|
if (!autoCreate)
|
||||||
return;
|
return;
|
||||||
this.sequencesDao.createTable();
|
this.sequencesDao.createTable();
|
||||||
@@ -79,6 +58,12 @@ public class SequencesGenerator implements Generator {
|
|||||||
this.sequencesUnlockDao.createTable();
|
this.sequencesUnlockDao.createTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized Sequences generate(String key) {
|
||||||
|
return generate(key, type);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized Sequences generate(String key, String type) {
|
public synchronized Sequences generate(String key, String type) {
|
||||||
return transactionTemplate.execute(status -> {
|
return transactionTemplate.execute(status -> {
|
||||||
@@ -132,38 +117,133 @@ public class SequencesGenerator implements Generator {
|
|||||||
return sequences.format(minLength);
|
return sequences.format(minLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String format(Sequences sequences, String pattern) {
|
||||||
|
return format(sequences, minLength, pattern);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(Sequences sequences, Integer minLength, String pattern) {
|
public String format(Sequences sequences, Integer minLength, String pattern) {
|
||||||
return format(sequences.getSeq(), minLength, pattern);
|
return format(sequences.getSeq(), minLength, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String format(Long seq, String pattern) {
|
||||||
|
return format(seq, minLength, pattern);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(Long seq, Integer minLength, String pattern) {
|
public String format(Long seq, Integer minLength, String pattern) {
|
||||||
return format(seq, null, minLength, pattern);
|
return format(seq, null, minLength, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String format(Long seq, String start, String pattern) {
|
||||||
|
return format(seq, start, minLength, pattern);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(Long seq, String start, Integer minLength, String pattern) {
|
public String format(Long seq, String start, Integer minLength, String pattern) {
|
||||||
if (start == null)
|
if (start == null)
|
||||||
start = "";
|
start = "";
|
||||||
String seqString = start + new Sequences(seq).format(minLength);
|
String seqString = start + new Sequences(seq).format(minLength);
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
pattern = pattern.replaceAll(Generator.YEAR, String.valueOf(calendar.get(Calendar.YEAR)));
|
pattern = pattern.replaceAll(YEAR.getPlaceholder(), String.valueOf(calendar.get(Calendar.YEAR)));
|
||||||
pattern = pattern.replaceAll(Generator.MONTH, String.format("%02d", calendar.get(Calendar.MONTH) + 1));
|
pattern = pattern.replaceAll(MONTH.getPlaceholder(), String.format("%02d", calendar.get(Calendar.MONTH) + 1));
|
||||||
pattern = pattern.replaceAll(Generator.DAY, String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
|
pattern = pattern.replaceAll(DAY.getPlaceholder(), String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
|
||||||
pattern = pattern.replaceAll(Generator.SEQ, seqString);
|
pattern = pattern.replaceAll(SEQ.getPlaceholder(), seqString);
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Sequences parse(String formatted, String pattern) {
|
||||||
|
//年、月、日、序号分隔特殊符号正则规则
|
||||||
|
String splitRegString = "(" + YEAR.getPlaceholder() + "|" + MONTH.getPlaceholder() + "|" + DAY.getPlaceholder() + "|" + SEQ.getPlaceholder() + ")";
|
||||||
|
//根据年、月、日、序号的特殊符号,对格式进行分隔,得到排除特殊符号后的字符串数组
|
||||||
|
String[] split = pattern.split(splitRegString);
|
||||||
|
|
||||||
|
for (String splitString : split) {
|
||||||
|
//用排除特殊符号后的字符串依次替换序号格式字符串和格式化后的序号字符串,得到纯净的序号字符串和序号格式对应关系
|
||||||
|
pattern = pattern.replace(splitString, "");
|
||||||
|
formatted = formatted.replace(splitString, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
//年、月、日的数字匹配正则规则
|
||||||
|
String yearRegStr = "\\d{4}";
|
||||||
|
String monthRegStr = "\\d{2}";
|
||||||
|
String dayRegStr = "\\d{2}";
|
||||||
|
|
||||||
|
//将序号格式分隔特殊符号字符串转为正则匹配规则
|
||||||
|
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);
|
||||||
|
|
||||||
|
//默认的年、月、日均为空字符串
|
||||||
|
String year = "", month = "", day = "", seq = "";
|
||||||
|
//根据序号匹配规则字符串查找字符串分组
|
||||||
|
while (matcher.find()) {
|
||||||
|
String group = matcher.group();
|
||||||
|
FormatPlaceholder formatPlaceholder = of(group);
|
||||||
|
if (formatPlaceholder == null)
|
||||||
|
continue;
|
||||||
|
switch (formatPlaceholder) {
|
||||||
|
case YEAR:
|
||||||
|
//若分组为年份分组,则将年份正则匹配到的字符串赋值给year,同时把格式化后的序号字符串中,对应年的字符串替换为空字符串
|
||||||
|
Matcher yearMatcher = yearPattern.matcher(formatted);
|
||||||
|
if (yearMatcher.find()) {
|
||||||
|
year = yearMatcher.group();
|
||||||
|
}
|
||||||
|
formatted = formatted.replaceFirst(yearRegStr, "");
|
||||||
|
break;
|
||||||
|
case MONTH:
|
||||||
|
//若分组为月份分组,则将月份正则匹配到的字符串赋值给month,同时把格式化后的序号字符串中,对应月的字符串替换为空字符串
|
||||||
|
Matcher monthMatcher = monthPattern.matcher(formatted);
|
||||||
|
if (monthMatcher.find()) {
|
||||||
|
month = monthMatcher.group();
|
||||||
|
}
|
||||||
|
formatted = formatted.replaceFirst(monthRegStr, "");
|
||||||
|
break;
|
||||||
|
case DAY:
|
||||||
|
//若分组为日期分组,则将日期正则匹配到的字符串赋值给day,同时把格式化后的序号字符串中,对应日期的字符串替换为空字符串
|
||||||
|
Matcher dayMatcher = dayPattern.matcher(formatted);
|
||||||
|
if (dayMatcher.find()) {
|
||||||
|
day = dayMatcher.group();
|
||||||
|
}
|
||||||
|
formatted = formatted.replaceFirst(dayRegStr, "");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//经过以上替换后,最后剩下的为序号,这个序号可能是补零了的,需要调用Long.parseLong来去零
|
||||||
|
seq = formatted;
|
||||||
|
|
||||||
|
//构建一个新的序号对象
|
||||||
|
Sequences sequences = new Sequences();
|
||||||
|
//用获取到的年、月、日、序号给新构建的序号对象设置对应的值
|
||||||
|
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.setSeq(StringUtils.hasLength(seq) ? Long.parseLong(seq) : 0L);
|
||||||
|
sequences.setType(type);
|
||||||
|
|
||||||
|
return sequences;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean lock(Sequences sequences) {
|
public synchronized boolean lock(Sequences sequences) {
|
||||||
|
if (sequences == null)
|
||||||
|
return true;
|
||||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||||
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
||||||
return sequencesUnlockDao.delete(condition);
|
return sequencesUnlockDao.delete(condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release() {
|
public synchronized void release() {
|
||||||
//列出所有使用中表存在的序号
|
//列出所有使用中表存在的序号
|
||||||
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll();
|
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll();
|
||||||
|
|
||||||
@@ -179,7 +259,7 @@ public class SequencesGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release(Date begin, Date end) {
|
public synchronized void release(Date begin, Date end) {
|
||||||
//列出指定时间段内使用中表存在的序号
|
//列出指定时间段内使用中表存在的序号
|
||||||
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end);
|
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end);
|
||||||
|
|
||||||
@@ -195,7 +275,9 @@ public class SequencesGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release(Sequences sequences) {
|
public synchronized void release(Sequences sequences) {
|
||||||
|
if (sequences == null)
|
||||||
|
return;
|
||||||
sequencesUnlockDao.delete(new SequencesUnlock(sequences));
|
sequencesUnlockDao.delete(new SequencesUnlock(sequences));
|
||||||
sequencesUnusedDao.save(new SequencesUnused(sequences));
|
sequencesUnusedDao.save(new SequencesUnused(sequences));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.yanghuanglin.seq.po;
|
package com.yanghuanglin.seq.po;
|
||||||
|
|
||||||
|
import com.yanghuanglin.seq.config.BaseConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前序号
|
* 当前序号
|
||||||
@@ -23,6 +24,24 @@ public class Sequences {
|
|||||||
*/
|
*/
|
||||||
protected Long seq = 0L;
|
protected Long seq = 0L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时字段,序号对应的年份(如2022年)(如果能获取到的话)。
|
||||||
|
* 该字段仅用于解析序号字符串时,解析出对应年份,用于合成key(如:序号对应的key为SNT+年份,返回的为其年份)
|
||||||
|
*/
|
||||||
|
private transient Integer year;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时字段,序号对应的月份(如2月)(如果能获取到的话)。
|
||||||
|
* 该字段仅用于解析序号字符串时,解析出对应月份,用于合成key(如:序号对应的key为SNT+年份+月份,返回的为其月份)
|
||||||
|
*/
|
||||||
|
private transient Integer month;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时字段,序号对应的日期(如1日)(如果能获取到的话)。
|
||||||
|
* 该字段仅用于解析序号字符串时,解析出对应日期,用于合成key(如:序号对应的key为SNT+年份+月份+日期,返回的为其日期)
|
||||||
|
*/
|
||||||
|
private transient Integer day;
|
||||||
|
|
||||||
public Sequences() {
|
public Sequences() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +90,30 @@ public class Sequences {
|
|||||||
this.seq = seq;
|
this.seq = seq;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer getYear() {
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setYear(Integer year) {
|
||||||
|
this.year = year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMonth() {
|
||||||
|
return month;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMonth(Integer month) {
|
||||||
|
this.month = month;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getDay() {
|
||||||
|
return day;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDay(Integer day) {
|
||||||
|
this.day = day;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将序号增加指定步长
|
* 将序号增加指定步长
|
||||||
*
|
*
|
||||||
@@ -83,7 +126,19 @@ public class Sequences {
|
|||||||
/**
|
/**
|
||||||
* 序号补零
|
* 序号补零
|
||||||
*
|
*
|
||||||
* @param minLength 最小长度,低于此长度,会填充零
|
* @return 补零后的序号,若未单独设置序号的长度,则最小长度为{@link BaseConfig#getMinLength()}长度;否则为修改后的长度,不足部分补零
|
||||||
|
*/
|
||||||
|
public String format() {
|
||||||
|
BaseConfig baseConfig = BaseConfig.getInstance();
|
||||||
|
if (baseConfig.getMinLength() != null)
|
||||||
|
return String.format("%0" + baseConfig.getMinLength() + "d", this.seq);
|
||||||
|
return String.valueOf(this.seq);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序号补零
|
||||||
|
*
|
||||||
|
* @param minLength 最小长度,低于此长度,会补零
|
||||||
* @return 补零后的序号
|
* @return 补零后的序号
|
||||||
*/
|
*/
|
||||||
public String format(Integer minLength) {
|
public String format(Integer minLength) {
|
||||||
@@ -91,4 +146,16 @@ public class Sequences {
|
|||||||
return String.format("%0" + minLength + "d", this.seq);
|
return String.format("%0" + minLength + "d", this.seq);
|
||||||
return String.valueOf(this.seq);
|
return String.valueOf(this.seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Sequences{" +
|
||||||
|
"key='" + key + '\'' +
|
||||||
|
", type='" + type + '\'' +
|
||||||
|
", seq=" + seq +
|
||||||
|
", year=" + year +
|
||||||
|
", month=" + month +
|
||||||
|
", day=" + day +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,4 +34,14 @@ public class SequencesUnlock extends Sequences {
|
|||||||
public void setCreateTime(Date createTime) {
|
public void setCreateTime(Date createTime) {
|
||||||
this.createTime = createTime;
|
this.createTime = createTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "SequencesUnlock{" +
|
||||||
|
"key='" + key + '\'' +
|
||||||
|
", type='" + type + '\'' +
|
||||||
|
", seq=" + seq +
|
||||||
|
", createTime=" + createTime +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,4 +16,13 @@ public class SequencesUnused extends Sequences {
|
|||||||
this.type = sequences.getType();
|
this.type = sequences.getType();
|
||||||
this.seq = sequences.getSeq();
|
this.seq = sequences.getSeq();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "SequencesUnused{" +
|
||||||
|
"key='" + key + '\'' +
|
||||||
|
", type='" + type + '\'' +
|
||||||
|
", seq=" + seq +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
|
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||||
import com.yanghuanglin.seq.config.TableConfig;
|
import com.yanghuanglin.seq.config.TableConfig;
|
||||||
import com.yanghuanglin.seq.po.Sequences;
|
|
||||||
import com.yanghuanglin.seq.generator.Generator;
|
import com.yanghuanglin.seq.generator.Generator;
|
||||||
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
||||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
import com.yanghuanglin.seq.po.Sequences;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -27,10 +27,10 @@ public class SeqTest {
|
|||||||
|
|
||||||
GeneratorConfig generatorConfig = new GeneratorConfig(dataSource);
|
GeneratorConfig generatorConfig = new GeneratorConfig(dataSource);
|
||||||
TableConfig tableConfig = new TableConfig();
|
TableConfig tableConfig = new TableConfig();
|
||||||
// tableConfig.setTable("sequences");
|
tableConfig.setTable("sequences");
|
||||||
// tableConfig.setKeyColumn("SEQUENCE_KEY");
|
tableConfig.setKeyColumn("SEQUENCE_KEY");
|
||||||
// tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
||||||
// tableConfig.setSeqColumn("SEQUENCE_NEXT_ID");
|
tableConfig.setSeqColumn("NEXT_ID");
|
||||||
generatorConfig.setTableConfig(tableConfig);
|
generatorConfig.setTableConfig(tableConfig);
|
||||||
|
|
||||||
generator = new SequencesGenerator(generatorConfig);
|
generator = new SequencesGenerator(generatorConfig);
|
||||||
@@ -47,7 +47,7 @@ public class SeqTest {
|
|||||||
int finalI = i;
|
int finalI = i;
|
||||||
threadPoolExecutor.execute(() -> {
|
threadPoolExecutor.execute(() -> {
|
||||||
Sequences sequences = generator.generate("SNT", "MISSION");
|
Sequences sequences = generator.generate("SNT", "MISSION");
|
||||||
String formattedSeq = generator.format(sequences.getSeq(), 5, "处〔#year#〕10801#seq#");
|
String formattedSeq = generator.format(sequences.getSeq(), "处〔#year#〕10801#seq#");
|
||||||
// if (finalI % 2 == 0)
|
// if (finalI % 2 == 0)
|
||||||
// System.out.println(3 / 0);
|
// System.out.println(3 / 0);
|
||||||
generator.lock(sequences);
|
generator.lock(sequences);
|
||||||
@@ -73,4 +73,18 @@ public class SeqTest {
|
|||||||
String s = "select * from sequences where `%s`=? and `%s`=?";
|
String s = "select * from sequences where `%s`=? and `%s`=?";
|
||||||
System.out.println(String.format(s, "key", "value"));
|
System.out.println(String.format(s, "key", "value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parseTest() {
|
||||||
|
String seqPattern = "ZZF#year##month##seq#";
|
||||||
|
String formatted = "ZZF20220200008";
|
||||||
|
|
||||||
|
Sequences sequences = generator.parse(formatted, seqPattern);
|
||||||
|
|
||||||
|
String key = "zzfCode" + sequences.getYear();
|
||||||
|
|
||||||
|
sequences.setKey(key);
|
||||||
|
sequences.setType("MISSION");
|
||||||
|
System.out.println(sequences);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user