Compare commits
14 Commits
f6750280d8
...
1.8.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 53ac87f0e8 | |||
| 6dc6498e6c | |||
|
|
4cbf9d72d3 | ||
|
|
f039ed30c9 | ||
|
|
f02fdf5a53 | ||
|
|
464b99a09b | ||
|
|
6a5a8111ee | ||
|
|
1accb4b066 | ||
|
|
c865be0918 | ||
|
|
011e37c1a3 | ||
|
|
a4e5017b60 | ||
|
|
19fb15bd03 | ||
|
|
956e707399 | ||
|
|
6bfcb1fd51 |
309
README.md
309
README.md
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
使用方法:
|
使用方法:
|
||||||
|
|
||||||
+ 在项目中放置jar包的地方把seq-1.2.0.jar、seq-1.2.0-sources.jar、seq-1.2.0-pom.xml复制过去
|
+ 在项目中放置jar包的地方把seq-1.8.2.jar、seq-1.8.2-sources.jar、seq-1.8.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.2.0</version>
|
<version>1.8.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.2.0.jar</file>
|
<file>${project.basedir}/lib/seq-1.8.2.jar</file>
|
||||||
<pomFile>${pom.basedir}/lib/seq-1.2.0-pom.xml</pomFile>
|
<pomFile>${project.basedir}/lib/seq-1.8.2-pom.xml</pomFile>
|
||||||
<sources>${project.basedir}/lib/seq-1.2.0-sources.jar</sources>
|
<sources>${project.basedir}/lib/seq-1.8.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.2.0</version>
|
<version>1.8.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<generatePom>true</generatePom>
|
<generatePom>true</generatePom>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -68,84 +68,13 @@
|
|||||||
</project>
|
</project>
|
||||||
```
|
```
|
||||||
|
|
||||||
+ springboot中配置方式一(优先):直接注入已有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.impl.SequencesGenerator;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.transaction.support.TransactionTemplate;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 基于已有的jdbcTemplate和transactionTemplate,一般如果引用了spring的数据库操作,如jpa、mybatis,都可以直接注入
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class SeqGeneratorConfig {
|
|
||||||
/**
|
|
||||||
* 注入已有的数据库操作模板
|
|
||||||
*/
|
|
||||||
@Resource
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
|
||||||
/**
|
|
||||||
* 注入已有的事务操作模板
|
|
||||||
*/
|
|
||||||
@Resource
|
|
||||||
private TransactionTemplate transactionTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 序号表配置类
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public TableConfig tableConfig() {
|
|
||||||
TableConfig tableConfig = new TableConfig();
|
|
||||||
//自定义表名、字段名
|
|
||||||
//tableConfig.setTable("sequences");
|
|
||||||
//tableConfig.setKeyColumn("SEQUENCE_KEY");
|
|
||||||
//tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
|
||||||
//tableConfig.setSeqColumn("SEQUENCE_NEXT_ID");
|
|
||||||
//tableConfig.setCreateTimeColumn("CREATE_TIME");
|
|
||||||
return tableConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 序号生成器配置类
|
|
||||||
* @param tableConfig 序号表配置类
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public GeneratorConfig generatorConfig(TableConfig tableConfig) {
|
|
||||||
GeneratorConfig generatorConfig = new GeneratorConfig();
|
|
||||||
generatorConfig.setJdbcTemplate(jdbcTemplate);
|
|
||||||
generatorConfig.setTransactionTemplate(transactionTemplate);
|
|
||||||
generatorConfig.setTableConfig(tableConfig);
|
|
||||||
return generatorConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 注册序号生成器类
|
|
||||||
* @param generatorConfig 序号生成器配置类
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public Generator generator(GeneratorConfig generatorConfig) {
|
|
||||||
return new SequencesGenerator(generatorConfig);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
+ springboot中配置方式二:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
|
||||||
|
|
||||||
```java
|
|
||||||
package com.yanghuanglin.springseq.config;
|
|
||||||
|
|
||||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
|
||||||
import com.yanghuanglin.seq.config.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;
|
||||||
@@ -160,7 +89,7 @@ import javax.sql.DataSource;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class SeqGeneratorConfig {
|
public class SeqGeneratorConfig {
|
||||||
/**
|
/**
|
||||||
* 注入已有的数据源,果没有,也可以自行构建
|
* 注入已有的数据源,如果没有,也可以自行构建
|
||||||
*/
|
*/
|
||||||
@Resource
|
@Resource
|
||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
@@ -172,11 +101,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 +113,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 +125,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 +136,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,18 +202,16 @@ TableConfig配置项,通过set方法设置(一般不用改,如果已有相
|
|||||||
|
|
||||||
GeneratorConfig配置项,通过set方法设置
|
GeneratorConfig配置项,通过set方法设置
|
||||||
|
|
||||||
| 配置项 | 类型 | 默认值 | 说明 |
|
| 配置项 | 类型 | 默认值 | 说明 |
|
||||||
|---------------------|------------------------------------------------------------------|------------------|----------|
|
|---------------------|------------------------------------------------------------------|------------------|------------------|
|
||||||
| dataSource | javax.sql.DataSource | null | 数据源 |
|
| dataSource | javax.sql.DataSource | null | 数据源 |
|
||||||
| jdbcTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 数据库操作模板 |
|
| monthZeroFilling | Boolean | true | 月份不足2位时自动补零 |
|
||||||
| transactionTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 事务操作模板 |
|
| dayZeroFilling | Boolean | true | 当前日期不足2位时自动补零 |
|
||||||
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
|
| autoCreate | Boolean | true | 开启自动建表 |
|
||||||
| autoCreate | Boolean | true | 开启自动建表 |
|
| step | Integer | 1 | 序号增加时的步长 |
|
||||||
| step | Integer | 1 | 序号增加时的步长 |
|
| 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无效;
|
|
||||||
进行这几种组合:dataSource+autoCreate+step+tableConfig,jdbcTemplate+transactionTemplate+autoCreate+step+tableConfig,jdbcTemplate+transactionManager+autoCreate+step+tableConfig
|
|
||||||
|
|
||||||
---
|
---
|
||||||
Generator方法如下:
|
Generator方法如下:
|
||||||
@@ -290,6 +219,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 +230,31 @@ import java.util.Date;
|
|||||||
|
|
||||||
public interface Generator {
|
public interface Generator {
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的年
|
* 判断格式定义中是否有序号
|
||||||
|
*
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 是否包含序号
|
||||||
*/
|
*/
|
||||||
String YEAR = "#year#";
|
boolean containSeq(String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的月
|
* 根据传入的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 数据字典中的编码
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
String MONTH = "#month#";
|
Sequences generate(String key, Boolean withOutSeq);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的日
|
* 同 {@link #generate(String, Boolean)},第二个参数默认为false
|
||||||
*/
|
*/
|
||||||
String DAY = "#day#";
|
Sequences generate(String key);
|
||||||
/**
|
|
||||||
* 序号格式字符中的格式化后的序号
|
|
||||||
*/
|
|
||||||
String SEQ = "#seq#";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据传入的key和type生成可用的序号对象。
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
@@ -322,26 +264,51 @@ public interface Generator {
|
|||||||
* 如果根据key和type在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除,然后写入到{@link SequencesUnlock}中。
|
* 如果根据key和type在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除,然后写入到{@link SequencesUnlock}中。
|
||||||
* <p/>
|
* <p/>
|
||||||
*
|
*
|
||||||
* @param key 数据字典中的编码
|
* @param key 数据字典中的编码
|
||||||
* @param type 序号类型
|
* @param type 序号类型
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
* @return 可用的序号对象
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
|
Sequences generate(String key, String type, Boolean withOutSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同{@link #generate(String, String, Boolean)},第三个参数默认为false
|
||||||
|
*/
|
||||||
Sequences generate(String key, String type);
|
Sequences generate(String key, String type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回根据{@link #generate(String, String)}得到的序号对象,补零后的序号字符串
|
* 返回根据{@link #generate(String, String, Boolean)}得到的序号对象,补零后的序号字符串
|
||||||
* <p/>
|
* <p/>
|
||||||
* 如生成的为3,而minLength为5,则返回的是00003
|
* 如生成的为3,而minLength为5,则返回的是00003
|
||||||
*
|
*
|
||||||
* @param key 数据字典中的编码
|
* @param key 数据字典中的编码
|
||||||
* @param type 序号类型
|
* @param type 序号类型
|
||||||
* @param minLength 序号数字最小长度
|
* @param minLength 序号数字最小长度
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
* @return 补零后的字符串
|
* @return 补零后的字符串
|
||||||
*/
|
*/
|
||||||
|
String generate(String key, String type, Integer minLength, Boolean withOutSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同{@link #generate(String, String, Integer, Boolean)},第四个参数默认为false
|
||||||
|
*/
|
||||||
String generate(String key, String type, Integer minLength);
|
String generate(String key, String type, Integer minLength);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
*
|
||||||
|
* @param sequences 生成的序号对象
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的字符串
|
||||||
|
*/
|
||||||
|
String format(Sequences sequences, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
* @param sequences 生成的序号对象
|
* @param sequences 生成的序号对象
|
||||||
* @param minLength 序号数字最小长度
|
* @param minLength 序号数字最小长度
|
||||||
@@ -350,10 +317,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/>
|
||||||
@@ -366,10 +348,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为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/>
|
||||||
@@ -386,7 +384,9 @@ public interface Generator {
|
|||||||
/**
|
/**
|
||||||
* 将已格式化的序号解析为序号对象
|
* 将已格式化的序号解析为序号对象
|
||||||
* <p/>
|
* <p/>
|
||||||
* 返回的序号对象{@link Sequences#getKey()}和{@link Sequences#getType()}为null,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
|
* 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/>
|
* <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
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -396,14 +396,14 @@ public interface Generator {
|
|||||||
*
|
*
|
||||||
* @param formatted 格式化后的序号字符串
|
* @param formatted 格式化后的序号字符串
|
||||||
* @param pattern 序号格式
|
* @param pattern 序号格式
|
||||||
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key、type需要根据情况手动设置
|
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key需要根据情况手动设置,type为{@link GeneratorConfig#getType()}的默认值
|
||||||
*/
|
*/
|
||||||
Sequences parse(String formatted, String pattern);
|
Sequences parse(String formatted, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||||
* <p/>
|
* <p/>
|
||||||
* 如办理案件时,先调用{@link #generate(String, String)}或者{@link #generate(String, String, Integer)}生成了序号,之后对案件进行了入库,如果入库完毕,则将该序号锁定,说明这个序号已被使用
|
* 如办理案件时,先调用{@link #generate(String, String, Boolean)}或者{@link #generate(String, String, Integer, Boolean)}生成了序号,之后对案件进行了入库,如果入库完毕,则将该序号锁定,说明这个序号已被使用
|
||||||
* <p/>
|
* <p/>
|
||||||
* 注意,此处的锁定非数据库中锁定,而是{@link SequencesUnused}和{@link SequencesUnlock}中均不存在key、type、seq相同的记录视为锁定。因此此处实际是把这两个表中的记录均删除了
|
* 注意,此处的锁定非数据库中锁定,而是{@link SequencesUnused}和{@link SequencesUnlock}中均不存在key、type、seq相同的记录视为锁定。因此此处实际是把这两个表中的记录均删除了
|
||||||
*
|
*
|
||||||
@@ -412,12 +412,23 @@ 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/>
|
||||||
* {@link SequencesUnlock}中未通过{@link #lock(Sequences)}方法锁定的序号会一直存在,调用此方法会将里面的所有序号都移动到{@link SequencesUnused}中,下次生成序号时优先从{@link SequencesUnused}获取。
|
* {@link SequencesUnlock}中未通过{@link #lock(Sequences)}方法锁定的序号会一直存在,调用此方法会将里面的所有序号都移动到{@link SequencesUnused}中,下次生成序号时优先从{@link SequencesUnused}获取。
|
||||||
*/
|
*/
|
||||||
void release();
|
boolean release();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 释放指定时间段内未使用的序号
|
* 释放指定时间段内未使用的序号
|
||||||
@@ -427,14 +438,62 @@ public interface Generator {
|
|||||||
* @param begin 开始时间
|
* @param begin 开始时间
|
||||||
* @param end 结束时间
|
* @param end 结束时间
|
||||||
*/
|
*/
|
||||||
void release(Date begin, Date end);
|
boolean 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);
|
boolean 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 是否忽略序号
|
||||||
|
*/
|
||||||
|
boolean release(Sequences sequences, boolean ignoreSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空所有闲置序号和未锁定序号
|
||||||
|
*/
|
||||||
|
boolean clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空指定时间段内闲置序号和未锁定序号
|
||||||
|
*/
|
||||||
|
boolean clear(Date begin, Date end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
|
||||||
|
*
|
||||||
|
* @param begin 开始时间
|
||||||
|
*/
|
||||||
|
void clearAfter(Date begin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空结束时间之前的限制序号和未锁定序号
|
||||||
|
*
|
||||||
|
* @param end 结束时间
|
||||||
|
*/
|
||||||
|
void clearBefore(Date end);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
4
pom.xml
4
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.2.0</version>
|
<version>1.8.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>
|
||||||
|
|||||||
173
src/main/java/com/yanghuanglin/seq/config/BaseConfig.java
Normal file
173
src/main/java/com/yanghuanglin/seq/config/BaseConfig.java
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
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 Boolean monthZeroFilling;
|
||||||
|
private Boolean dayZeroFilling;
|
||||||
|
|
||||||
|
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 Boolean getMonthZeroFilling() {
|
||||||
|
if (monthZeroFilling == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return monthZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMonthZeroFilling(Boolean monthZeroFilling) {
|
||||||
|
this.monthZeroFilling = monthZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getDayZeroFilling() {
|
||||||
|
if (dayZeroFilling == null)
|
||||||
|
throw new NullPointerException("请先初始化BaseConfig");
|
||||||
|
return dayZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDayZeroFilling(Boolean dayZeroFilling) {
|
||||||
|
this.dayZeroFilling = dayZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinLength(Integer minLength) {
|
||||||
|
this.minLength = minLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init(GeneratorConfig generatorConfig) {
|
||||||
|
//数据源
|
||||||
|
DataSource dataSource = generatorConfig.getDataSource();
|
||||||
|
if (dataSource == null)
|
||||||
|
//若数据库操作模板为空,也没有配置数据源,则抛出异常
|
||||||
|
throw new NullPointerException("数据源不能为空");
|
||||||
|
//否则以数据源创建数据库操作模板
|
||||||
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
|
||||||
|
this.transactionTemplate = new TransactionTemplate(transactionManager);
|
||||||
|
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
|
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
|
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
|
this.step = generatorConfig.getStep();
|
||||||
|
this.type = generatorConfig.getType();
|
||||||
|
this.minLength = generatorConfig.getMinLength();
|
||||||
|
this.monthZeroFilling = generatorConfig.getMonthZeroFilling();
|
||||||
|
this.dayZeroFilling = generatorConfig.getDayZeroFilling();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
package com.yanghuanglin.seq.config;
|
package com.yanghuanglin.seq.config;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
||||||
import org.springframework.transaction.support.TransactionTemplate;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,21 +14,6 @@ public class GeneratorConfig {
|
|||||||
*/
|
*/
|
||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据库操作模板
|
|
||||||
*/
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 事务处理模板
|
|
||||||
*/
|
|
||||||
private TransactionTemplate transactionTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 事务管理器
|
|
||||||
*/
|
|
||||||
private DataSourceTransactionManager transactionManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自动创建表
|
* 自动创建表
|
||||||
*/
|
*/
|
||||||
@@ -43,6 +24,26 @@ public class GeneratorConfig {
|
|||||||
*/
|
*/
|
||||||
private Integer step = 1;
|
private Integer step = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化后序号字符串的最小长度,不足的部分补零
|
||||||
|
*/
|
||||||
|
private Integer minLength = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认序号类型
|
||||||
|
*/
|
||||||
|
private String type = "DEFAULT";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 月份是否补零。为false时,1月显示为1,为true时,1月显示为01
|
||||||
|
*/
|
||||||
|
private Boolean monthZeroFilling = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期是否补零。为false时,1日显示为1,为true时,1日显示为01
|
||||||
|
*/
|
||||||
|
private Boolean dayZeroFilling = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 表和字段配置
|
* 表和字段配置
|
||||||
*/
|
*/
|
||||||
@@ -63,30 +64,6 @@ public class GeneratorConfig {
|
|||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JdbcTemplate getJdbcTemplate() {
|
|
||||||
return jdbcTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
|
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TransactionTemplate getTransactionTemplate() {
|
|
||||||
return transactionTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
|
|
||||||
this.transactionTemplate = transactionTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DataSourceTransactionManager getTransactionManager() {
|
|
||||||
return transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTransactionManager(DataSourceTransactionManager transactionManager) {
|
|
||||||
this.transactionManager = transactionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean getAutoCreate() {
|
public Boolean getAutoCreate() {
|
||||||
return autoCreate;
|
return autoCreate;
|
||||||
}
|
}
|
||||||
@@ -95,6 +72,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;
|
||||||
}
|
}
|
||||||
@@ -105,6 +98,22 @@ public class GeneratorConfig {
|
|||||||
this.step = step;
|
this.step = step;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getMonthZeroFilling() {
|
||||||
|
return monthZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMonthZeroFilling(Boolean monthZeroFilling) {
|
||||||
|
this.monthZeroFilling = monthZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getDayZeroFilling() {
|
||||||
|
return dayZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDayZeroFilling(Boolean dayZeroFilling) {
|
||||||
|
this.dayZeroFilling = dayZeroFilling;
|
||||||
|
}
|
||||||
|
|
||||||
public TableConfig getTableConfig() {
|
public TableConfig getTableConfig() {
|
||||||
return tableConfig;
|
return tableConfig;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class TableConfig {
|
|||||||
private String table = "sequences";
|
private String table = "sequences";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序号英文名称,和序号类型组成唯一组件
|
* 序号英文名称,和序号类型组成唯一主键
|
||||||
*/
|
*/
|
||||||
private String keyColumn = "key";
|
private String keyColumn = "key";
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(),
|
||||||
|
|||||||
@@ -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 '未锁定序号表'";
|
||||||
|
|||||||
@@ -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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,31 @@ import java.util.Date;
|
|||||||
|
|
||||||
public interface Generator {
|
public interface Generator {
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的年
|
* 判断格式定义中是否有序号
|
||||||
|
*
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 是否包含序号
|
||||||
*/
|
*/
|
||||||
String YEAR = "#year#";
|
boolean containSeq(String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的月
|
* 根据传入的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 数据字典中的编码
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
String MONTH = "#month#";
|
Sequences generate(String key, Boolean withOutSeq);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 序号格式字符中的日
|
* 同 {@link #generate(String, Boolean)},第二个参数默认为false
|
||||||
*/
|
*/
|
||||||
String DAY = "#day#";
|
Sequences generate(String key);
|
||||||
/**
|
|
||||||
* 序号格式字符中的格式化后的序号
|
|
||||||
*/
|
|
||||||
String SEQ = "#seq#";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据传入的key和type生成可用的序号对象。
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
@@ -32,26 +45,51 @@ public interface Generator {
|
|||||||
* 如果根据key和type在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除,然后写入到{@link SequencesUnlock}中。
|
* 如果根据key和type在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除,然后写入到{@link SequencesUnlock}中。
|
||||||
* <p/>
|
* <p/>
|
||||||
*
|
*
|
||||||
* @param key 数据字典中的编码
|
* @param key 数据字典中的编码
|
||||||
* @param type 序号类型
|
* @param type 序号类型
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
* @return 可用的序号对象
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
|
Sequences generate(String key, String type, Boolean withOutSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同{@link #generate(String, String, Boolean)},第三个参数默认为false
|
||||||
|
*/
|
||||||
Sequences generate(String key, String type);
|
Sequences generate(String key, String type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回根据{@link #generate(String, String)}得到的序号对象,补零后的序号字符串
|
* 返回根据{@link #generate(String, String, Boolean)}得到的序号对象,补零后的序号字符串
|
||||||
* <p/>
|
* <p/>
|
||||||
* 如生成的为3,而minLength为5,则返回的是00003
|
* 如生成的为3,而minLength为5,则返回的是00003
|
||||||
*
|
*
|
||||||
* @param key 数据字典中的编码
|
* @param key 数据字典中的编码
|
||||||
* @param type 序号类型
|
* @param type 序号类型
|
||||||
* @param minLength 序号数字最小长度
|
* @param minLength 序号数字最小长度
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
* @return 补零后的字符串
|
* @return 补零后的字符串
|
||||||
*/
|
*/
|
||||||
|
String generate(String key, String type, Integer minLength, Boolean withOutSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同{@link #generate(String, String, Integer, Boolean)},第四个参数默认为false
|
||||||
|
*/
|
||||||
String generate(String key, String type, Integer minLength);
|
String generate(String key, String type, Integer minLength);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
*
|
||||||
|
* @param sequences 生成的序号对象
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 格式化后的字符串
|
||||||
|
*/
|
||||||
|
String format(Sequences sequences, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
* @param sequences 生成的序号对象
|
* @param sequences 生成的序号对象
|
||||||
* @param minLength 序号数字最小长度
|
* @param minLength 序号数字最小长度
|
||||||
@@ -60,10 +98,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/>
|
||||||
@@ -76,10 +129,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为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/>
|
||||||
@@ -96,7 +165,9 @@ public interface Generator {
|
|||||||
/**
|
/**
|
||||||
* 将已格式化的序号解析为序号对象
|
* 将已格式化的序号解析为序号对象
|
||||||
* <p/>
|
* <p/>
|
||||||
* 返回的序号对象{@link Sequences#getKey()}和{@link Sequences#getType()}为null,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
|
* 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/>
|
* <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
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -106,14 +177,14 @@ public interface Generator {
|
|||||||
*
|
*
|
||||||
* @param formatted 格式化后的序号字符串
|
* @param formatted 格式化后的序号字符串
|
||||||
* @param pattern 序号格式
|
* @param pattern 序号格式
|
||||||
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key、type需要根据情况手动设置
|
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key需要根据情况手动设置,type为{@link GeneratorConfig#getType()}的默认值
|
||||||
*/
|
*/
|
||||||
Sequences parse(String formatted, String pattern);
|
Sequences parse(String formatted, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||||
* <p/>
|
* <p/>
|
||||||
* 如办理案件时,先调用{@link #generate(String, String)}或者{@link #generate(String, String, Integer)}生成了序号,之后对案件进行了入库,如果入库完毕,则将该序号锁定,说明这个序号已被使用
|
* 如办理案件时,先调用{@link #generate(String, String, Boolean)}或者{@link #generate(String, String, Integer, Boolean)}生成了序号,之后对案件进行了入库,如果入库完毕,则将该序号锁定,说明这个序号已被使用
|
||||||
* <p/>
|
* <p/>
|
||||||
* 注意,此处的锁定非数据库中锁定,而是{@link SequencesUnused}和{@link SequencesUnlock}中均不存在key、type、seq相同的记录视为锁定。因此此处实际是把这两个表中的记录均删除了
|
* 注意,此处的锁定非数据库中锁定,而是{@link SequencesUnused}和{@link SequencesUnlock}中均不存在key、type、seq相同的记录视为锁定。因此此处实际是把这两个表中的记录均删除了
|
||||||
*
|
*
|
||||||
@@ -122,12 +193,23 @@ 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/>
|
||||||
* {@link SequencesUnlock}中未通过{@link #lock(Sequences)}方法锁定的序号会一直存在,调用此方法会将里面的所有序号都移动到{@link SequencesUnused}中,下次生成序号时优先从{@link SequencesUnused}获取。
|
* {@link SequencesUnlock}中未通过{@link #lock(Sequences)}方法锁定的序号会一直存在,调用此方法会将里面的所有序号都移动到{@link SequencesUnused}中,下次生成序号时优先从{@link SequencesUnused}获取。
|
||||||
*/
|
*/
|
||||||
void release();
|
boolean release();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 释放指定时间段内未使用的序号
|
* 释放指定时间段内未使用的序号
|
||||||
@@ -137,12 +219,60 @@ public interface Generator {
|
|||||||
* @param begin 开始时间
|
* @param begin 开始时间
|
||||||
* @param end 结束时间
|
* @param end 结束时间
|
||||||
*/
|
*/
|
||||||
void release(Date begin, Date end);
|
boolean 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);
|
boolean 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 是否忽略序号
|
||||||
|
*/
|
||||||
|
boolean release(Sequences sequences, boolean ignoreSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空所有闲置序号和未锁定序号
|
||||||
|
*/
|
||||||
|
boolean clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空指定时间段内闲置序号和未锁定序号
|
||||||
|
*/
|
||||||
|
boolean clear(Date begin, Date end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
|
||||||
|
*
|
||||||
|
* @param begin 开始时间
|
||||||
|
*/
|
||||||
|
void clearAfter(Date begin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空结束时间之前的限制序号和未锁定序号
|
||||||
|
*
|
||||||
|
* @param end 结束时间
|
||||||
|
*/
|
||||||
|
void clearBefore(Date end);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +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.jdbc.support.JdbcTransactionManager;
|
|
||||||
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;
|
||||||
@@ -25,56 +20,41 @@ 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;
|
||||||
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;
|
||||||
|
private final Boolean monthZeroFilling;
|
||||||
|
private final Boolean dayZeroFilling;
|
||||||
|
|
||||||
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);
|
this.monthZeroFilling = baseConfig.getMonthZeroFilling();
|
||||||
}
|
this.dayZeroFilling = baseConfig.getDayZeroFilling();
|
||||||
|
|
||||||
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 synchronized void createTable(Boolean autoCreate) {
|
||||||
if (!autoCreate)
|
if (!autoCreate)
|
||||||
return;
|
return;
|
||||||
this.sequencesDao.createTable();
|
this.sequencesDao.createTable();
|
||||||
@@ -82,43 +62,67 @@ public class SequencesGenerator implements Generator {
|
|||||||
this.sequencesUnlockDao.createTable();
|
this.sequencesUnlockDao.createTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized Sequences generate(String key, String type) {
|
public boolean containSeq(String pattern) {
|
||||||
|
return StringUtils.hasLength(pattern) && pattern.contains(SEQ.getPlaceholder());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized Sequences generate(String key, Boolean withOutSeq) {
|
||||||
|
return generate(key, type, withOutSeq);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Sequences generate(String key) {
|
||||||
|
return generate(key, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized Sequences generate(String key, String type, Boolean withOutSeq) {
|
||||||
return transactionTemplate.execute(status -> {
|
return transactionTemplate.execute(status -> {
|
||||||
try {
|
try {
|
||||||
//根据传入的key和type新生成查询条件对象
|
//根据传入的key和type新生成查询条件对象
|
||||||
Sequences condition = new Sequences(key, type);
|
Sequences condition = new Sequences(key, type);
|
||||||
|
|
||||||
//找到正在使用的最大序号
|
if (!withOutSeq) {
|
||||||
Sequences sequences = sequencesDao.find(condition);
|
//不是不包含序号的序号对象(有可能格式中只配置了#year##month##day#,序号就不用自增,也不用入库,直接返回即可)
|
||||||
if (sequences == null) {
|
//找到正在使用的最大序号
|
||||||
//不存在,说明还没生成,将新生成的入库,此时序号为默认的0
|
Sequences sequences = sequencesDao.find(condition);
|
||||||
sequences = condition;
|
boolean result = true;
|
||||||
sequencesDao.save(sequences);
|
if (sequences == null) {
|
||||||
}
|
//不存在,说明还没生成,将新生成的入库,此时序号为默认的0
|
||||||
|
sequences = condition;
|
||||||
|
result = sequencesDao.save(sequences);
|
||||||
|
}
|
||||||
|
|
||||||
//根据传入的key和type查找空闲编号最小的一个
|
//根据传入的key和type查找空闲编号最小的一个
|
||||||
SequencesUnused conditionIdle = new SequencesUnused(condition);
|
SequencesUnused conditionIdle = new SequencesUnused(condition);
|
||||||
SequencesUnused sequencesUnused = sequencesUnusedDao.findMinSeq(conditionIdle);
|
SequencesUnused sequencesUnused = sequencesUnusedDao.findMinSeq(conditionIdle);
|
||||||
|
|
||||||
if (sequencesUnused == null) {
|
if (sequencesUnused == null) {
|
||||||
//空闲编号不存在,说明是未生成过,序号需要增加后直接使用,同时将新生成的写入到使用中表
|
//空闲编号不存在,说明是未生成过,序号需要增加后直接使用,同时将新生成的写入到使用中表
|
||||||
sequences.increase(step);
|
sequences.increase(step);
|
||||||
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequences);
|
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequences);
|
||||||
sequencesUnlock.setCreateTime(new Date());
|
sequencesUnlock.setCreateTime(new Date());
|
||||||
|
|
||||||
sequencesDao.update(sequences);
|
result = result && sequencesDao.update(sequences) &&
|
||||||
sequencesUnlockDao.save(sequencesUnlock);
|
sequencesUnlockDao.save(sequencesUnlock);
|
||||||
|
} else {
|
||||||
|
//空闲编号存在,说明已经生成过,序号不需要增加,直接使用。同时将该空闲编号移动到使用中表
|
||||||
|
sequences = new Sequences(sequencesUnused);
|
||||||
|
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequencesUnused);
|
||||||
|
sequencesUnlock.setCreateTime(new Date());
|
||||||
|
|
||||||
|
result = result && sequencesUnlockDao.save(sequencesUnlock) &&
|
||||||
|
sequencesUnusedDao.delete(sequencesUnused);
|
||||||
|
}
|
||||||
|
sequences.setWithOutSeq(false);
|
||||||
|
return result ? sequences : null;
|
||||||
} else {
|
} else {
|
||||||
//空闲编号存在,说明已经生成过,序号不需要增加,直接使用。同时将该空闲编号移动到使用中表
|
condition.setWithOutSeq(true);
|
||||||
sequences = new Sequences(sequencesUnused);
|
return condition;
|
||||||
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequencesUnused);
|
|
||||||
sequencesUnlock.setCreateTime(new Date());
|
|
||||||
|
|
||||||
sequencesUnlockDao.save(sequencesUnlock);
|
|
||||||
sequencesUnusedDao.delete(sequencesUnused);
|
|
||||||
}
|
}
|
||||||
return sequences;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
status.setRollbackOnly();
|
status.setRollbackOnly();
|
||||||
@@ -128,40 +132,68 @@ public class SequencesGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized String generate(String key, String type, Integer minLength) {
|
public Sequences generate(String key, String type) {
|
||||||
Sequences sequences = this.generate(key, type);
|
return generate(key, type, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized String generate(String key, String type, Integer minLength, Boolean withOutSeq) {
|
||||||
|
Sequences sequences = generate(key, type, withOutSeq);
|
||||||
if (sequences == null)
|
if (sequences == null)
|
||||||
return null;
|
return null;
|
||||||
return sequences.format(minLength);
|
return sequences.format(minLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String generate(String key, String type, Integer minLength) {
|
||||||
|
return generate(key, type, minLength, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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)));
|
int year = calendar.get(Calendar.YEAR);
|
||||||
pattern = pattern.replaceAll(Generator.MONTH, String.format("%02d", calendar.get(Calendar.MONTH) + 1));
|
int month = calendar.get(Calendar.MONTH) + 1;
|
||||||
pattern = pattern.replaceAll(Generator.DAY, String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
|
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
pattern = pattern.replaceAll(Generator.SEQ, seqString);
|
pattern = pattern.replaceAll(YEAR.getPlaceholder(), String.valueOf(year));
|
||||||
|
pattern = pattern.replaceAll(MONTH.getPlaceholder(), monthZeroFilling ? String.format("%02d", month) : String.valueOf(month));
|
||||||
|
pattern = pattern.replaceAll(DAY.getPlaceholder(), dayZeroFilling ? String.format("%02d", day) : String.valueOf(day));
|
||||||
|
pattern = pattern.replaceAll(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);
|
||||||
|
|
||||||
@@ -191,8 +223,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()) {
|
||||||
@@ -200,7 +235,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()) {
|
||||||
@@ -208,7 +243,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()) {
|
||||||
@@ -228,52 +263,183 @@ public class SequencesGenerator implements Generator {
|
|||||||
sequences.setMonth(StringUtils.hasLength(month) ? Integer.valueOf(month) : null);
|
sequences.setMonth(StringUtils.hasLength(month) ? Integer.valueOf(month) : null);
|
||||||
sequences.setDay(StringUtils.hasLength(day) ? Integer.valueOf(day) : null);
|
sequences.setDay(StringUtils.hasLength(day) ? Integer.valueOf(day) : null);
|
||||||
sequences.setSeq(StringUtils.hasLength(seq) ? Long.parseLong(seq) : 0L);
|
sequences.setSeq(StringUtils.hasLength(seq) ? Long.parseLong(seq) : 0L);
|
||||||
|
sequences.setWithOutSeq(!StringUtils.hasLength(seq));
|
||||||
|
sequences.setType(type);
|
||||||
|
|
||||||
return sequences;
|
return sequences;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean lock(Sequences sequences) {
|
public synchronized boolean lock(Sequences sequences) {
|
||||||
|
if (sequences == null || sequences.getWithOutSeq())
|
||||||
|
return true;
|
||||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||||
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
||||||
return sequencesUnlockDao.delete(condition);
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
return sequencesUnlockDao.delete(condition);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release() {
|
public synchronized boolean lock(Sequences sequences, boolean ignoreSeq) {
|
||||||
|
if (!ignoreSeq)
|
||||||
|
return lock(sequences);
|
||||||
|
if (sequences == null || sequences.getWithOutSeq())
|
||||||
|
return true;
|
||||||
|
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||||
|
condition.setSeq(null);
|
||||||
|
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
return sequencesUnlockDao.delete(condition);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean release() {
|
||||||
//列出所有使用中表存在的序号
|
//列出所有使用中表存在的序号
|
||||||
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll();
|
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll();
|
||||||
|
|
||||||
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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
//将使用中表的序号放到空闲表中
|
//将使用中表的序号放到空闲表中
|
||||||
sequencesUnusedDao.saveBatch(sequencesUnusedList);
|
|
||||||
//删除所有使用中表的数据
|
//删除所有使用中表的数据
|
||||||
sequencesUnlockDao.deleteAll();
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
//将使用中表的序号放到空闲表中
|
||||||
|
//删除所有使用中表的数据
|
||||||
|
return sequencesUnusedDao.saveBatch(sequencesUnusedList) &&
|
||||||
|
sequencesUnlockDao.deleteAll();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release(Date begin, Date end) {
|
public synchronized boolean release(Date begin, Date end) {
|
||||||
//列出指定时间段内使用中表存在的序号
|
//列出指定时间段内使用中表存在的序号
|
||||||
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end);
|
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end);
|
||||||
|
|
||||||
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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
//将指定时间段内使用中表的序号放到空闲表中
|
//将指定时间段内使用中表的序号放到空闲表中
|
||||||
sequencesUnusedDao.saveBatch(sequencesUnusedList);
|
|
||||||
//删除指定时间段内使用中表的数据
|
//删除指定时间段内使用中表的数据
|
||||||
sequencesUnlockDao.deleteByDate(begin, end);
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
//将指定时间段内使用中表的序号放到空闲表中
|
||||||
|
//删除指定时间段内使用中表的数据
|
||||||
|
return sequencesUnusedDao.saveBatch(sequencesUnusedList) &&
|
||||||
|
sequencesUnlockDao.deleteByDate(begin, end);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release(Sequences sequences) {
|
public synchronized void releaseAfter(Date begin) {
|
||||||
sequencesUnlockDao.delete(new SequencesUnlock(sequences));
|
release(begin, null);
|
||||||
sequencesUnusedDao.save(new SequencesUnused(sequences));
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void releaseBefore(Date end) {
|
||||||
|
release(null, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean release(Sequences sequences) {
|
||||||
|
if (sequences == null)
|
||||||
|
return true;
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
return sequencesUnlockDao.delete(new SequencesUnlock(sequences))
|
||||||
|
&& sequencesUnusedDao.save(new SequencesUnused(sequences, new Date()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean release(Sequences sequences, boolean ignoreSeq) {
|
||||||
|
if (!ignoreSeq) {
|
||||||
|
return release(sequences);
|
||||||
|
}
|
||||||
|
if (sequences == null)
|
||||||
|
return true;
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequences);
|
||||||
|
sequencesUnlock.setSeq(null);
|
||||||
|
//由于忽略了序号,因此不需要将未使用序号放到SequencesUnused里面
|
||||||
|
return sequencesUnlockDao.delete(sequencesUnlock);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean clear() {
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
return sequencesUnlockDao.deleteAll() &&
|
||||||
|
sequencesUnusedDao.deleteAll();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean clear(Date begin, Date end) {
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
return sequencesUnlockDao.deleteByDate(begin, end) &&
|
||||||
|
sequencesUnusedDao.deleteByDate(begin, end);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void clearAfter(Date begin) {
|
||||||
|
clear(begin, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void clearBefore(Date end) {
|
||||||
|
clear(null, end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.yanghuanglin.seq.po;
|
package com.yanghuanglin.seq.po;
|
||||||
|
|
||||||
|
import com.yanghuanglin.seq.config.BaseConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前序号
|
* 当前序号
|
||||||
@@ -41,6 +42,11 @@ public class Sequences {
|
|||||||
*/
|
*/
|
||||||
private transient Integer day;
|
private transient Integer day;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时字段,表示序号对象不包含序号
|
||||||
|
*/
|
||||||
|
private transient Boolean withOutSeq;
|
||||||
|
|
||||||
public Sequences() {
|
public Sequences() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +119,14 @@ public class Sequences {
|
|||||||
this.day = day;
|
this.day = day;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean getWithOutSeq() {
|
||||||
|
return withOutSeq;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWithOutSeq(Boolean withOutSeq) {
|
||||||
|
this.withOutSeq = withOutSeq;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将序号增加指定步长
|
* 将序号增加指定步长
|
||||||
*
|
*
|
||||||
@@ -125,7 +139,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) {
|
||||||
@@ -143,6 +169,7 @@ public class Sequences {
|
|||||||
", year=" + year +
|
", year=" + year +
|
||||||
", month=" + month +
|
", month=" + month +
|
||||||
", day=" + day +
|
", day=" + day +
|
||||||
|
", withOutSeq=" + withOutSeq +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||||
|
import com.yanghuanglin.seq.config.BaseConfig;
|
||||||
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.generator.Generator;
|
import com.yanghuanglin.seq.generator.Generator;
|
||||||
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
||||||
import com.yanghuanglin.seq.po.Sequences;
|
import com.yanghuanglin.seq.po.Sequences;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -26,12 +30,15 @@ public class SeqTest {
|
|||||||
dataSource.setPassword("root");
|
dataSource.setPassword("root");
|
||||||
|
|
||||||
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);
|
||||||
|
generatorConfig.setDayZeroFilling(false);
|
||||||
|
generatorConfig.setMonthZeroFilling(false);
|
||||||
|
|
||||||
generator = new SequencesGenerator(generatorConfig);
|
generator = new SequencesGenerator(generatorConfig);
|
||||||
}
|
}
|
||||||
@@ -44,12 +51,9 @@ public class SeqTest {
|
|||||||
Set<String> set = new HashSet<>();
|
Set<String> set = new HashSet<>();
|
||||||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(100));
|
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(100));
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; 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)
|
|
||||||
// System.out.println(3 / 0);
|
|
||||||
generator.lock(sequences);
|
generator.lock(sequences);
|
||||||
set.add(formattedSeq);
|
set.add(formattedSeq);
|
||||||
System.out.println(formattedSeq);
|
System.out.println(formattedSeq);
|
||||||
|
|||||||
Reference in New Issue
Block a user