10 Commits
1.3.1 ... 1.7.2

Author SHA1 Message Date
yhl452493373
4cbf9d72d3 增加lock(Sequences sequences, boolean ignoreSeq)、release(Sequences sequences, boolean ignoreSeq)方法 2022-02-21 16:03:41 +08:00
yhl452493373
f039ed30c9 增加releaseBefore、releaseAfter、clearBefore、clearAfter方法 2022-02-21 10:26:29 +08:00
yhl452493373
f02fdf5a53 更新README 2022-02-16 17:38:36 +08:00
yhl452493373
464b99a09b 调整创建表时key、type、seq的长度,分别为64,64,20,避免出现“Specified key was too long; max key length is 767 bytes”问题 2022-02-16 17:36:50 +08:00
yhl452493373
6a5a8111ee 变更测试使用的字段名 2022-02-16 10:47:57 +08:00
yhl452493373
1accb4b066 变更测试使用的字段名 2022-02-16 10:41:22 +08:00
yhl452493373
c865be0918 更新README 2022-02-15 13:37:54 +08:00
yhl452493373
011e37c1a3 1、增加 clear()和clear(begin,end)方法,用于在某些情况下清理unlock和unused表,避免他们越来越大 2022-02-15 09:47:37 +08:00
yhl452493373
a4e5017b60 升级junit 2022-02-14 17:20:23 +08:00
yhl452493373
19fb15bd03 1、Generator中占位符部分转到FormatPlaceholder枚举中,规范其代码
2、SequencesGenerator中,基础配置转到BaseConfig,该类为单例,便于其他地方调用
3、Generator中,format方法重构,增加不需要minLength参数的方法,minLength参数从BaseConfig单例中或
4、Sequences中,format方法重构,增加不需要minLength参数的方法,minLength参数从BaseConfig单例中或
2022-02-14 16:23:24 +08:00
14 changed files with 765 additions and 172 deletions

202
README.md
View File

@@ -8,7 +8,7 @@
使用方法: 使用方法:
+ 在项目中放置jar包的地方把seq-1.3.1.jar、seq-1.3.1-sources.jar、seq-1.3.1-pom.xml复制过去 + 在项目中放置jar包的地方把seq-1.7.2.jar、seq-1.7.2-sources.jar、seq-1.7.2-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.3.1</version> <version>1.7.2</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.3.1.jar</file> <file>${project.basedir}/lib/seq-1.7.2.jar</file>
<pomFile>${pom.basedir}/lib/seq-1.3.1-pom.xml</pomFile> <pomFile>${project.basedir}/lib/seq-1.7.2-pom.xml</pomFile>
<sources>${project.basedir}/lib/seq-1.3.1-sources.jar</sources> <sources>${project.basedir}/lib/seq-1.7.2-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.3.1</version> <version>1.7.2</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;
@@ -271,19 +273,20 @@ 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 | 序号增加时的步长 |
| type | String | DEFAULT | 默认序号类型 | | type | String | DEFAULT | 默认序号类型 |
| tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 | | 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+tableConfigjdbcTemplate+transactionTemplate+autoCreate+step+tableConfigjdbcTemplate+transactionManager+autoCreate+step+tableConfig 进行这几种组合dataSource+autoCreate+step+minLength+tableConfigjdbcTemplate+transactionTemplate+autoCreate+step+minLength+tableConfigjdbcTemplate+transactionManager+autoCreate+step+minLength+tableConfig
--- ---
Generator方法如下 Generator方法如下
@@ -291,7 +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.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;
@@ -299,23 +304,6 @@ import com.yanghuanglin.seq.po.SequencesUnused;
import java.util.Date; import java.util.Date;
public interface Generator { public interface Generator {
/**
* 序号格式字符中的年
*/
String YEAR = "#year#";
/**
* 序号格式字符中的月
*/
String MONTH = "#month#";
/**
* 序号格式字符中的日
*/
String DAY = "#day#";
/**
* 序号格式字符中的格式化后的序号
*/
String SEQ = "#seq#";
/** /**
* 根据传入的key和type生成可用的序号对象。 * 根据传入的key和type生成可用的序号对象。
* <p/> * <p/>
@@ -324,7 +312,7 @@ public interface Generator {
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除然后写入到{@link SequencesUnlock}中。 * 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除然后写入到{@link SequencesUnlock}中。
* <p/> * <p/>
* *
* @param key 数据字典中的编码 * @param key 数据字典中的编码
* @return 可用的序号对象 * @return 可用的序号对象
*/ */
Sequences generate(String key); Sequences generate(String key);
@@ -355,8 +343,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 序号数字最小长度
@@ -365,10 +366,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为1pattern为#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为1minLength为4pattern为#year##month##day#6#seq#则会格式化为2022013060001。此序号含义如下 * seq为1minLength为4pattern为#year##month##day#6#seq#则会格式化为2022013060001。此序号含义如下
* <p/> * <p/>
@@ -381,10 +397,26 @@ public interface Generator {
*/ */
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为6minLength为4pattern为#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为6minLength为4pattern为#year##month##day##seq#则会格式化为2022013060001。此序号含义如下 * seq为1,start为6minLength为4pattern为#year##month##day##seq#则会格式化为2022013060001。此序号含义如下
* <p/> * <p/>
@@ -401,6 +433,8 @@ public interface Generator {
/** /**
* 将已格式化的序号解析为序号对象 * 将已格式化的序号解析为序号对象
* <p/> * <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()}可能有值 * 返回的序号对象{@link Sequences#getKey()}为null{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* <p/> * <p/>
* 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key * 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key
@@ -427,6 +461,17 @@ public interface Generator {
*/ */
boolean lock(Sequences sequences); boolean lock(Sequences sequences);
/**
* 忽略{@link Sequences#getSeq()} ,仅通过{@link Sequences#getKey()}和{@link Sequences#getType()}来锁定序号。
* <p/>
* 如果ignoreSeq为false则等价于{@link #lock(Sequences)}
*
* @param sequences 需要锁定的序号
* @param ignoreSeq 是否忽略序号
* @return 锁定结果
*/
boolean lock(Sequences sequences, boolean ignoreSeq);
/** /**
* 释放所有未使用的序号 * 释放所有未使用的序号
* <p/> * <p/>
@@ -444,12 +489,61 @@ public interface Generator {
*/ */
void release(Date begin, Date end); void release(Date begin, Date end);
/**
* 释放从开始时间开始,到现在时间之间未使用的序号。结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void releaseAfter(Date begin);
/**
* 释放结束时间以前的序号
*
* @param end 结束时间
*/
void releaseBefore(Date end);
/** /**
* 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。 * 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
* *
* @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象 * @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象
*/ */
void release(Sequences sequences); void release(Sequences sequences);
/**
* 忽略{@link Sequences#getSeq()}来释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
* <p/>
* 如果ignoreSeq为false则等价于{@link #release(Sequences)}
*
* @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象
* @param ignoreSeq 是否忽略序号
*/
void release(Sequences sequences, boolean ignoreSeq);
/**
* 清空所有闲置序号和未锁定序号
*/
void clear();
/**
* 清空指定时间段内闲置序号和未锁定序号
*/
void clear(Date begin, Date end);
/**
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void clearAfter(Date begin);
/**
* 清空结束时间之前的限制序号和未锁定序号
*
* @param end 结束时间
*/
void clearBefore(Date end);
} }
``` ```

View File

@@ -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.3.1</version> <version>1.7.2</version>
<name>seq</name> <name>seq</name>
<description>seq</description> <description>seq</description>
<properties> <properties>
@@ -24,7 +24,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.13.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View 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();
}
}

View File

@@ -43,6 +43,11 @@ public class GeneratorConfig {
*/ */
private Integer step = 1; private Integer step = 1;
/**
* 格式化后序号字符串的最小长度,不足的部分补零
*/
private Integer minLength = 1;
/** /**
* 默认序号类型 * 默认序号类型
*/ */
@@ -100,6 +105,14 @@ 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() { public String getType() {
return type; return type;
} }

View File

@@ -2,6 +2,7 @@ package com.yanghuanglin.seq.dao;
import com.yanghuanglin.seq.po.SequencesUnused; import com.yanghuanglin.seq.po.SequencesUnused;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@@ -36,4 +37,14 @@ public interface SequencesUnusedDao {
boolean saveBatch(List<SequencesUnused> sequencesUnusedList); boolean saveBatch(List<SequencesUnused> sequencesUnusedList);
void createTable(); void createTable();
/**
* 删除所有闲置的序号
*/
boolean deleteAll();
/**
* 删除指定时间段内使用中的序号
*/
boolean deleteByDate(Date begin, Date end);
} }

View File

@@ -57,9 +57,9 @@ public class SequencesDaoImpl implements SequencesDao {
@Override @Override
public void createTable() { public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS `%s` ( " + String sql = "CREATE TABLE IF NOT EXISTS `%s` ( " +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号英文名称'," + " `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号类型'," + " `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 2 ) NOT NULL COMMENT '已使用到的序号'," + " `%s` BIGINT ( 20 ) NOT NULL COMMENT '已使用到的序号'," +
" PRIMARY KEY ( `%s`, `%s` ) " + " PRIMARY KEY ( `%s`, `%s` ) " +
" ) COMMENT '当前序号表'"; " ) COMMENT '当前序号表'";
sql = String.format(sql, tableConfig.getTable(), sql = String.format(sql, tableConfig.getTable(),

View File

@@ -33,10 +33,15 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
@Override @Override
public boolean delete(SequencesUnlock sequencesUnlock) { public boolean delete(SequencesUnlock sequencesUnlock) {
String sql = "delete from `%s_unlock` where `%s`=? and `%s`=? and `%s`=?"; String sql = "delete from `%s_unlock` where `%s`=? and `%s`=?";
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn()); sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn());
int result = this.jdbcTemplate.update(sql, sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq()); if (sequencesUnlock.getSeq() != null) {
return result != 0; sql += " and `%s`=?";
sql = String.format(sql, tableConfig.getSeqColumn());
return this.jdbcTemplate.update(sql, sequencesUnlock.getKey(), sequencesUnlock.getType(), sequencesUnlock.getSeq()) != 0;
} else {
return this.jdbcTemplate.update(sql, sequencesUnlock.getKey(), sequencesUnlock.getType()) != 0;
}
} }
@Override @Override
@@ -48,9 +53,22 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
@Override @Override
public List<SequencesUnlock> listByDate(Date begin, Date end) { public List<SequencesUnlock> listByDate(Date begin, Date end) {
String sql = "select * from `%s_unlock` where `%s`>=? and `%s`<=?"; String sql;
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn()); if (begin != null && end != null) {
return this.jdbcTemplate.query(sql, rowMapper(), begin, end); 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);
} 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);
} 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);
} else {
return listAll();
}
} }
@Override @Override
@@ -63,18 +81,30 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
@Override @Override
public boolean deleteByDate(Date begin, Date end) { public boolean deleteByDate(Date begin, Date end) {
String sql = "delete from `%s_unlock` where `%s`>=? and `%s`<=?"; String sql;
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn()); if (begin != null && end != null) {
int result = this.jdbcTemplate.update(sql, begin, end); sql = "delete from `%s_unlock` where `%s`>=? and `%s`<=?";
return result != 0; sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
return this.jdbcTemplate.update(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;
} 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;
} else {
return deleteAll();
}
} }
@Override @Override
public void createTable() { public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS `%s_unlock` ( " + String sql = "CREATE TABLE IF NOT EXISTS `%s_unlock` ( " +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号英文名称'," + " `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号类型'," + " `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 2 ) NOT NULL COMMENT '尚未锁定的序号'," + " `%s` BIGINT ( 20 ) NOT NULL COMMENT '尚未锁定的序号'," +
" `%s` DATETIME NOT NULL COMMENT '使用时间'," + " `%s` DATETIME NOT NULL COMMENT '使用时间'," +
" PRIMARY KEY ( `%s`, `%s` ,`%s` ) " + " PRIMARY KEY ( `%s`, `%s` ,`%s` ) " +
" ) COMMENT '未锁定序号表'"; " ) COMMENT '未锁定序号表'";

View File

@@ -10,6 +10,8 @@ import org.springframework.jdbc.core.RowMapper;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@@ -28,7 +30,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override @Override
public SequencesUnused findMinSeq(SequencesUnused sequencesUnused) { public SequencesUnused findMinSeq(SequencesUnused sequencesUnused) {
String sql = "select * from `%s_ununsed` where `%s`=? and `%s`=? order by `%s` asc limit 0,1"; 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()); sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
try { try {
return this.jdbcTemplate.queryForObject(sql, rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType()); return this.jdbcTemplate.queryForObject(sql, rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType());
@@ -40,7 +42,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override @Override
public SequencesUnused findMaxSeq(SequencesUnused sequencesUnused) { public SequencesUnused findMaxSeq(SequencesUnused sequencesUnused) {
try { try {
String sql = "select * from `%s_ununsed` where `%s`=? and `%s`=? order by `%s` desc limit 0,1"; 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()); 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(sql, rowMapper(), sequencesUnused.getKey(), sequencesUnused.getType());
} catch (EmptyResultDataAccessException ignored) { } catch (EmptyResultDataAccessException ignored) {
@@ -50,7 +52,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override @Override
public boolean delete(SequencesUnused sequencesUnused) { public boolean delete(SequencesUnused sequencesUnused) {
String sql = "delete from `%s_ununsed` where `%s`=? and `%s`=? and `%s`=?"; String sql = "delete from `%s_unused` where `%s`=? and `%s`=? and `%s`=?";
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn()); 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(sql, sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq());
return result != 0; return result != 0;
@@ -58,16 +60,16 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override @Override
public boolean save(SequencesUnused sequencesUnused) { public boolean save(SequencesUnused sequencesUnused) {
String sql = "insert into `%s_ununsed`(`%s`,`%s`,`%s`) values(?,?,?)"; String sql = "insert into `%s_unused`(`%s`,`%s`,`%s`,`%s`) values(?,?,?,?)";
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn()); 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()); int result = this.jdbcTemplate.update(sql, sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq(), sequencesUnused.getCreateTime());
return result != 0; return result != 0;
} }
@Override @Override
public boolean saveBatch(List<SequencesUnused> sequencesUnusedList) { public boolean saveBatch(List<SequencesUnused> sequencesUnusedList) {
String sql = "insert into `%s_ununsed`(`%s`,`%s`,`%s`) values(?,?,?)"; String sql = "insert into `%s_unused`(`%s`,`%s`,`%s`,`%s`) values(?,?,?,?)";
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn()); 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(sql, new BatchPreparedStatementSetter() {
@Override @Override
public void setValues(PreparedStatement ps, int i) throws SQLException { public void setValues(PreparedStatement ps, int i) throws SQLException {
@@ -75,6 +77,7 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
ps.setString(1, sequencesUnused.getKey()); ps.setString(1, sequencesUnused.getKey());
ps.setString(2, sequencesUnused.getType()); ps.setString(2, sequencesUnused.getType());
ps.setLong(3, sequencesUnused.getSeq()); ps.setLong(3, sequencesUnused.getSeq());
ps.setTimestamp(4, new Timestamp(sequencesUnused.getCreateTime().getTime()));
} }
@Override @Override
@@ -87,29 +90,60 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override @Override
public void createTable() { public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS `%s_ununsed` ( " + String sql = "CREATE TABLE IF NOT EXISTS `%s_unused` ( " +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号英文名称'," + " `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号类型'," + " `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 2 ) NOT NULL COMMENT '闲置的的序号'," + " `%s` BIGINT ( 20 ) NOT NULL COMMENT '闲置的的序号'," +
" `%s` DATETIME NOT NULL COMMENT '设为闲置序号的时间'," +
" PRIMARY KEY ( `%s`, `%s`, `%s` ) " + " PRIMARY KEY ( `%s`, `%s`, `%s` ) " +
" ) COMMENT '闲置序号表'"; " ) COMMENT '闲置序号表'";
sql = String.format(sql, tableConfig.getTable(), sql = String.format(sql, tableConfig.getTable(),
tableConfig.getKeyColumn(), tableConfig.getKeyColumn(),
tableConfig.getTypeColumn(), tableConfig.getTypeColumn(),
tableConfig.getSeqColumn(), tableConfig.getSeqColumn(),
tableConfig.getCreateTimeColumn(),
tableConfig.getKeyColumn(), tableConfig.getKeyColumn(),
tableConfig.getTypeColumn(), tableConfig.getTypeColumn(),
tableConfig.getSeqColumn()); tableConfig.getSeqColumn());
this.jdbcTemplate.execute(sql); this.jdbcTemplate.execute(sql);
} }
@Override
public boolean deleteAll() {
String sql = "delete from `%s_unused`";
sql = String.format(sql, tableConfig.getTable());
int result = this.jdbcTemplate.update(sql);
return result != 0;
}
@Override
public boolean deleteByDate(Date begin, Date end) {
String sql;
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;
} 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;
} 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;
} else {
return deleteAll();
}
}
private RowMapper<SequencesUnused> rowMapper() { private RowMapper<SequencesUnused> rowMapper() {
return (rs, rowNum) -> { return (rs, rowNum) -> {
SequencesUnused result = new SequencesUnused(); SequencesUnused sequencesUnused = new SequencesUnused();
result.setKey(rs.getString(tableConfig.getKeyColumn())); sequencesUnused.setKey(rs.getString(tableConfig.getKeyColumn()));
result.setType(rs.getString(tableConfig.getTypeColumn())); sequencesUnused.setType(rs.getString(tableConfig.getTypeColumn()));
result.setSeq(rs.getLong(tableConfig.getSeqColumn())); sequencesUnused.setSeq(rs.getLong(tableConfig.getSeqColumn()));
return result; sequencesUnused.setCreateTime(rs.getDate(tableConfig.getCreateTimeColumn()));
return sequencesUnused;
}; };
} }
} }

View File

@@ -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;
}
}

View File

@@ -1,6 +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.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,23 +10,6 @@ import com.yanghuanglin.seq.po.SequencesUnused;
import java.util.Date; import java.util.Date;
public interface Generator { public interface Generator {
/**
* 序号格式字符中的年
*/
String YEAR = "#year#";
/**
* 序号格式字符中的月
*/
String MONTH = "#month#";
/**
* 序号格式字符中的日
*/
String DAY = "#day#";
/**
* 序号格式字符中的格式化后的序号
*/
String SEQ = "#seq#";
/** /**
* 根据传入的key和type生成可用的序号对象。 * 根据传入的key和type生成可用的序号对象。
* <p/> * <p/>
@@ -33,7 +18,7 @@ public interface Generator {
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除然后写入到{@link SequencesUnlock}中。 * 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除然后写入到{@link SequencesUnlock}中。
* <p/> * <p/>
* *
* @param key 数据字典中的编码 * @param key 数据字典中的编码
* @return 可用的序号对象 * @return 可用的序号对象
*/ */
Sequences generate(String key); Sequences generate(String key);
@@ -64,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 序号数字最小长度
@@ -74,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为1pattern为#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为1minLength为4pattern为#year##month##day#6#seq#则会格式化为2022013060001。此序号含义如下 * seq为1minLength为4pattern为#year##month##day#6#seq#则会格式化为2022013060001。此序号含义如下
* <p/> * <p/>
@@ -90,10 +103,26 @@ public interface Generator {
*/ */
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为6minLength为4pattern为#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为6minLength为4pattern为#year##month##day##seq#则会格式化为2022013060001。此序号含义如下 * seq为1,start为6minLength为4pattern为#year##month##day##seq#则会格式化为2022013060001。此序号含义如下
* <p/> * <p/>
@@ -110,6 +139,8 @@ public interface Generator {
/** /**
* 将已格式化的序号解析为序号对象 * 将已格式化的序号解析为序号对象
* <p/> * <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()}可能有值 * 返回的序号对象{@link Sequences#getKey()}为null{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* <p/> * <p/>
* 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key * 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key
@@ -136,6 +167,17 @@ public interface Generator {
*/ */
boolean lock(Sequences sequences); boolean lock(Sequences sequences);
/**
* 忽略{@link Sequences#getSeq()} ,仅通过{@link Sequences#getKey()}和{@link Sequences#getType()}来锁定序号。
* <p/>
* 如果ignoreSeq为false则等价于{@link #lock(Sequences)}
*
* @param sequences 需要锁定的序号
* @param ignoreSeq 是否忽略序号
* @return 锁定结果
*/
boolean lock(Sequences sequences, boolean ignoreSeq);
/** /**
* 释放所有未使用的序号 * 释放所有未使用的序号
* <p/> * <p/>
@@ -153,10 +195,58 @@ public interface Generator {
*/ */
void release(Date begin, Date end); void release(Date begin, Date end);
/**
* 释放从开始时间开始,到现在时间之间未使用的序号。结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void releaseAfter(Date begin);
/**
* 释放结束时间以前的序号
*
* @param end 结束时间
*/
void releaseBefore(Date end);
/** /**
* 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。 * 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
* *
* @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象 * @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象
*/ */
void release(Sequences sequences); void release(Sequences sequences);
/**
* 忽略{@link Sequences#getSeq()}来释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
* <p/>
* 如果ignoreSeq为false则等价于{@link #release(Sequences)}
*
* @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象
* @param ignoreSeq 是否忽略序号
*/
void release(Sequences sequences, boolean ignoreSeq);
/**
* 清空所有闲置序号和未锁定序号
*/
void clear();
/**
* 清空指定时间段内闲置序号和未锁定序号
*/
void clear(Date begin, Date end);
/**
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void clearAfter(Date begin);
/**
* 清空结束时间之前的限制序号和未锁定序号
*
* @param end 结束时间
*/
void clearBefore(Date end);
} }

View File

@@ -1,22 +1,18 @@
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.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils; 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;
@@ -24,6 +20,8 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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;
private final SequencesDao sequencesDao; private final SequencesDao sequencesDao;
@@ -31,51 +29,28 @@ public class SequencesGenerator implements Generator {
private final SequencesUnlockDao sequencesUnlockDao; private final SequencesUnlockDao sequencesUnlockDao;
private final Integer step; private final Integer step;
private final String type; 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 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();
autoCreateTable(generatorConfig.getAutoCreate());
} }
/** /**
* 自动创建需要的表 * 创建需要的表
*
* @param autoCreate 是否自动创建
*/ */
private void autoCreateTable(Boolean autoCreate) { private synchronized void createTable(Boolean autoCreate) {
if (!autoCreate) if (!autoCreate)
return; return;
this.sequencesDao.createTable(); this.sequencesDao.createTable();
@@ -83,6 +58,7 @@ public class SequencesGenerator implements Generator {
this.sequencesUnlockDao.createTable(); this.sequencesUnlockDao.createTable();
} }
@Override @Override
public synchronized Sequences generate(String key) { public synchronized Sequences generate(String key) {
return generate(key, type); return generate(key, type);
@@ -135,39 +111,54 @@ public class SequencesGenerator implements Generator {
@Override @Override
public synchronized String generate(String key, String type, Integer minLength) { public synchronized String generate(String key, String type, Integer minLength) {
Sequences sequences = this.generate(key, type); Sequences sequences = generate(key, type);
if (sequences == null) if (sequences == null)
return null; return null;
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 @Override
public Sequences parse(String formatted, String pattern) { public Sequences parse(String formatted, String pattern) {
//年、月、日、序号分隔特殊符号正则规则 //年、月、日、序号分隔特殊符号正则规则
String splitRegString = "(" + YEAR + "|" + MONTH + "|" + DAY + "|" + SEQ + ")"; String splitRegString = "(" + YEAR.getPlaceholder() + "|" + MONTH.getPlaceholder() + "|" + DAY.getPlaceholder() + "|" + SEQ.getPlaceholder() + ")";
//根据年、月、日、序号的特殊符号,对格式进行分隔,得到排除特殊符号后的字符串数组 //根据年、月、日、序号的特殊符号,对格式进行分隔,得到排除特殊符号后的字符串数组
String[] split = pattern.split(splitRegString); String[] split = pattern.split(splitRegString);
@@ -197,8 +188,11 @@ public class SequencesGenerator implements Generator {
//根据序号匹配规则字符串查找字符串分组 //根据序号匹配规则字符串查找字符串分组
while (matcher.find()) { while (matcher.find()) {
String group = matcher.group(); String group = matcher.group();
switch (group) { FormatPlaceholder formatPlaceholder = of(group);
case Generator.YEAR: if (formatPlaceholder == null)
continue;
switch (formatPlaceholder) {
case YEAR:
//若分组为年份分组则将年份正则匹配到的字符串赋值给year同时把格式化后的序号字符串中对应年的字符串替换为空字符串 //若分组为年份分组则将年份正则匹配到的字符串赋值给year同时把格式化后的序号字符串中对应年的字符串替换为空字符串
Matcher yearMatcher = yearPattern.matcher(formatted); Matcher yearMatcher = yearPattern.matcher(formatted);
if (yearMatcher.find()) { if (yearMatcher.find()) {
@@ -206,7 +200,7 @@ public class SequencesGenerator implements Generator {
} }
formatted = formatted.replaceFirst(yearRegStr, ""); formatted = formatted.replaceFirst(yearRegStr, "");
break; break;
case Generator.MONTH: case MONTH:
//若分组为月份分组则将月份正则匹配到的字符串赋值给month同时把格式化后的序号字符串中对应月的字符串替换为空字符串 //若分组为月份分组则将月份正则匹配到的字符串赋值给month同时把格式化后的序号字符串中对应月的字符串替换为空字符串
Matcher monthMatcher = monthPattern.matcher(formatted); Matcher monthMatcher = monthPattern.matcher(formatted);
if (monthMatcher.find()) { if (monthMatcher.find()) {
@@ -214,7 +208,7 @@ public class SequencesGenerator implements Generator {
} }
formatted = formatted.replaceFirst(monthRegStr, ""); formatted = formatted.replaceFirst(monthRegStr, "");
break; break;
case Generator.DAY: case DAY:
//若分组为日期分组则将日期正则匹配到的字符串赋值给day同时把格式化后的序号字符串中对应日期的字符串替换为空字符串 //若分组为日期分组则将日期正则匹配到的字符串赋值给day同时把格式化后的序号字符串中对应日期的字符串替换为空字符串
Matcher dayMatcher = dayPattern.matcher(formatted); Matcher dayMatcher = dayPattern.matcher(formatted);
if (dayMatcher.find()) { if (dayMatcher.find()) {
@@ -241,11 +235,25 @@ public class SequencesGenerator implements Generator {
@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
public synchronized boolean lock(Sequences sequences, boolean ignoreSeq) {
if (!ignoreSeq)
return lock(sequences);
if (sequences == null)
return true;
SequencesUnlock condition = new SequencesUnlock(sequences);
condition.setSeq(null);
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
return sequencesUnlockDao.delete(condition);
}
@Override @Override
public synchronized void release() { public synchronized void release() {
//列出所有使用中表存在的序号 //列出所有使用中表存在的序号
@@ -253,7 +261,7 @@ public class SequencesGenerator implements Generator {
List<SequencesUnused> sequencesUnusedList = new ArrayList<>(); List<SequencesUnused> sequencesUnusedList = new ArrayList<>();
for (SequencesUnlock sequencesUnlock : sequencesUnlockList) { for (SequencesUnlock sequencesUnlock : sequencesUnlockList) {
sequencesUnusedList.add(new SequencesUnused(sequencesUnlock)); sequencesUnusedList.add(new SequencesUnused(sequencesUnlock, new Date()));
} }
//将使用中表的序号放到空闲表中 //将使用中表的序号放到空闲表中
@@ -269,7 +277,7 @@ public class SequencesGenerator implements Generator {
List<SequencesUnused> sequencesUnusedList = new ArrayList<>(); List<SequencesUnused> sequencesUnusedList = new ArrayList<>();
for (SequencesUnlock sequencesUnlock : sequencesUnlockList) { for (SequencesUnlock sequencesUnlock : sequencesUnlockList) {
sequencesUnusedList.add(new SequencesUnused(sequencesUnlock)); sequencesUnusedList.add(new SequencesUnused(sequencesUnlock, new Date()));
} }
//将指定时间段内使用中表的序号放到空闲表中 //将指定时间段内使用中表的序号放到空闲表中
@@ -278,9 +286,57 @@ public class SequencesGenerator implements Generator {
sequencesUnlockDao.deleteByDate(begin, end); sequencesUnlockDao.deleteByDate(begin, end);
} }
@Override
public synchronized void releaseAfter(Date begin) {
release(begin, null);
}
@Override
public synchronized void releaseBefore(Date end) {
release(null, end);
}
@Override @Override
public synchronized 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, new Date()));
}
@Override
public synchronized void release(Sequences sequences, boolean ignoreSeq) {
if (!ignoreSeq) {
release(sequences);
return;
}
if (sequences == null)
return;
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequences);
sequencesUnlock.setSeq(null);
sequencesUnlockDao.delete(sequencesUnlock);
//由于忽略了序号因此不需要将未使用序号放到SequencesUnused里面
}
@Override
public synchronized void clear() {
sequencesUnlockDao.deleteAll();
sequencesUnusedDao.deleteAll();
}
@Override
public synchronized void clear(Date begin, Date end) {
sequencesUnlockDao.deleteByDate(begin, end);
sequencesUnusedDao.deleteByDate(begin, end);
}
@Override
public synchronized void clearAfter(Date begin) {
clear(begin, null);
}
@Override
public synchronized void clearBefore(Date end) {
clear(null, end);
} }
} }

View File

@@ -1,5 +1,6 @@
package com.yanghuanglin.seq.po; package com.yanghuanglin.seq.po;
import com.yanghuanglin.seq.config.BaseConfig;
/** /**
* 当前序号 * 当前序号
@@ -125,7 +126,19 @@ public class Sequences {
/** /**
* 序号补零 * 序号补零
* *
* @param minLength 最小长度,低于此长度,会填充 * @return 补零后的序号,若未单独设置序号的长度,则最小长度为{@link BaseConfig#getMinLength()}长度;否则为修改后的长度,不足部分补
*/
public String format() {
Integer minLength = BaseConfig.getInstance().getMinLength();
if (minLength != null)
return String.format("%0" + minLength + "d", this.seq);
return String.valueOf(this.seq);
}
/**
* 序号补零
*
* @param minLength 最小长度,低于此长度,会补零
* @return 补零后的序号 * @return 补零后的序号
*/ */
public String format(Integer minLength) { public String format(Integer minLength) {

View File

@@ -1,6 +1,8 @@
package com.yanghuanglin.seq.po; package com.yanghuanglin.seq.po;
import java.util.Date;
/** /**
* 闲置序号 * 闲置序号
* *
@@ -8,6 +10,8 @@ package com.yanghuanglin.seq.po;
* @since 2022/1/28 * @since 2022/1/28
*/ */
public class SequencesUnused extends Sequences { public class SequencesUnused extends Sequences {
private Date createTime;
public SequencesUnused() { public SequencesUnused() {
} }
@@ -17,12 +21,28 @@ public class SequencesUnused extends Sequences {
this.seq = sequences.getSeq(); this.seq = sequences.getSeq();
} }
public SequencesUnused(Sequences sequences, Date createTime) {
this.key = sequences.getKey();
this.type = sequences.getType();
this.seq = sequences.getSeq();
this.createTime = createTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override @Override
public String toString() { public String toString() {
return "SequencesUnused{" + return "SequencesUnused{" +
"key='" + key + '\'' + "key='" + key + '\'' +
", type='" + type + '\'' + ", type='" + type + '\'' +
", seq=" + seq + ", seq=" + seq +
", createTime=" + createTime +
'}'; '}';
} }
} }

View File

@@ -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("CURRENT");
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);