Compare commits
10 Commits
1.5.1
...
3615ed9eec
| Author | SHA1 | Date | |
|---|---|---|---|
| 3615ed9eec | |||
| 53ac87f0e8 | |||
| 6dc6498e6c | |||
|
|
4cbf9d72d3 | ||
|
|
f039ed30c9 | ||
|
|
f02fdf5a53 | ||
|
|
464b99a09b | ||
|
|
6a5a8111ee | ||
|
|
1accb4b066 | ||
|
|
c865be0918 |
191
README.md
191
README.md
@@ -4,11 +4,13 @@
|
|||||||
|
|
||||||
用于生成全局自增序号,跳过的序号可以回收使用。
|
用于生成全局自增序号,跳过的序号可以回收使用。
|
||||||
|
|
||||||
|
***本生成器内部使用transactionTemplate进行事务管理,如果你项目中给使用此生成器的方法或类加了`@Transactional`注解,则需将新建一个Service来专门负责调用此生成器的方法,同时要给新建的类增加`@Transactional`注解,并设置`propagation = Propagation.NOT_SUPPORTED`***
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
使用方法:
|
使用方法:
|
||||||
|
|
||||||
+ 在项目中放置jar包的地方把seq-1.5.1.jar、seq-1.5.1-sources.jar、seq-1.5.1-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 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.yanghuanglin</groupId>
|
<groupId>com.yanghuanglin</groupId>
|
||||||
<artifactId>seq</artifactId>
|
<artifactId>seq</artifactId>
|
||||||
<version>1.5.1</version>
|
<version>1.8.2</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<!-- 如若你项目中有引用spring-jdbc,则需要排除seq的jdbc依赖 -->
|
<!-- 如若你项目中有引用spring-jdbc,则需要排除seq的jdbc依赖 -->
|
||||||
<exclusion>
|
<exclusion>
|
||||||
@@ -50,13 +52,13 @@
|
|||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<!-- ${project.basedir}表示当前项目的根目录 -->
|
<!-- ${project.basedir}表示当前项目的根目录 -->
|
||||||
<file>${project.basedir}/lib/seq-1.5.1.jar</file>
|
<file>${project.basedir}/lib/seq-1.8.2.jar</file>
|
||||||
<pomFile>${pom.basedir}/lib/seq-1.5.1-pom.xml</pomFile>
|
<pomFile>${project.basedir}/lib/seq-1.8.2-pom.xml</pomFile>
|
||||||
<sources>${project.basedir}/lib/seq-1.5.1-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.5.1</version>
|
<version>1.8.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<generatePom>true</generatePom>
|
<generatePom>true</generatePom>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -68,78 +70,7 @@
|
|||||||
</project>
|
</project>
|
||||||
```
|
```
|
||||||
|
|
||||||
+ springboot中配置方式一(优先):直接注入已有jdbcTemplate和transactionTemplate
|
+ springboot中配置方式:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
||||||
|
|
||||||
```java
|
|
||||||
package com.yanghuanglin.springseq.baseConfig;
|
|
||||||
|
|
||||||
import com.yanghuanglin.seq.baseConfig.GeneratorConfig;
|
|
||||||
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("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
|
```java
|
||||||
package com.yanghuanglin.springseq.baseConfig;
|
package com.yanghuanglin.springseq.baseConfig;
|
||||||
@@ -160,7 +91,7 @@ import javax.sql.DataSource;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class SeqGeneratorConfig {
|
public class SeqGeneratorConfig {
|
||||||
/**
|
/**
|
||||||
* 注入已有的数据源,果没有,也可以自行构建
|
* 注入已有的数据源,如果没有,也可以自行构建
|
||||||
*/
|
*/
|
||||||
@Resource
|
@Resource
|
||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
@@ -276,18 +207,14 @@ 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 | 默认序号类型 |
|
| type | String | DEFAULT | 默认序号类型 |
|
||||||
| minLength | Integer | 1 | 默认序号格式化后长度,不足的补零 |
|
| minLength | Integer | 1 | 默认序号格式化后长度,不足的补零 |
|
||||||
| tableConfig | com.yanghuanglin.seq.baseConfig.TableConfig | TableConfig的默认配置 | 表配置 |
|
| tableConfig | com.yanghuanglin.seq.baseConfig.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||||
|
|
||||||
以上配置中,jdbcTemplate和transactionTemplate优先级最高,如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置,则dataSource和transactionManager无效;
|
|
||||||
可进行这几种组合:dataSource+autoCreate+step+minLength+tableConfig,jdbcTemplate+transactionTemplate+autoCreate+step+minLength+tableConfig,jdbcTemplate+transactionManager+autoCreate+step+minLength+tableConfig
|
|
||||||
|
|
||||||
---
|
---
|
||||||
Generator方法如下:
|
Generator方法如下:
|
||||||
|
|
||||||
@@ -304,6 +231,14 @@ import com.yanghuanglin.seq.po.SequencesUnused;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public interface Generator {
|
public interface Generator {
|
||||||
|
/**
|
||||||
|
* 判断格式定义中是否有序号
|
||||||
|
*
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 是否包含序号
|
||||||
|
*/
|
||||||
|
boolean containSeq(String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据传入的key和type生成可用的序号对象。
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -313,8 +248,14 @@ public interface Generator {
|
|||||||
* <p/>
|
* <p/>
|
||||||
*
|
*
|
||||||
* @param key 数据字典中的编码
|
* @param key 数据字典中的编码
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
* @return 可用的序号对象
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
|
Sequences generate(String key, Boolean withOutSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 {@link #generate(String, Boolean)},第二个参数默认为false
|
||||||
|
*/
|
||||||
Sequences generate(String key);
|
Sequences generate(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -327,24 +268,36 @@ public interface Generator {
|
|||||||
*
|
*
|
||||||
* @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 BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
@@ -355,7 +308,7 @@ public interface Generator {
|
|||||||
String format(Sequences sequences, String pattern);
|
String format(Sequences sequences, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
@@ -452,7 +405,7 @@ public interface Generator {
|
|||||||
/**
|
/**
|
||||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||||
* <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相同的记录视为锁定。因此此处实际是把这两个表中的记录均删除了
|
||||||
*
|
*
|
||||||
@@ -461,12 +414,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();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 释放指定时间段内未使用的序号
|
* 释放指定时间段内未使用的序号
|
||||||
@@ -476,23 +440,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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清空所有闲置序号和未锁定序号
|
* 清空所有闲置序号和未锁定序号
|
||||||
*/
|
*/
|
||||||
void clear();
|
boolean clear();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清空指定时间段内闲置序号和未锁定序号
|
* 清空指定时间段内闲置序号和未锁定序号
|
||||||
*/
|
*/
|
||||||
void clear(Date begin, Date end);
|
boolean clear(Date begin, Date end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
|
||||||
|
*
|
||||||
|
* @param begin 开始时间
|
||||||
|
*/
|
||||||
|
void clearAfter(Date begin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空结束时间之前的限制序号和未锁定序号
|
||||||
|
*
|
||||||
|
* @param end 结束时间
|
||||||
|
*/
|
||||||
|
void clearBefore(Date end);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.yanghuanglin</groupId>
|
<groupId>com.yanghuanglin</groupId>
|
||||||
<artifactId>seq</artifactId>
|
<artifactId>seq</artifactId>
|
||||||
<version>1.5.1</version>
|
<version>1.8.2</version>
|
||||||
<name>seq</name>
|
<name>seq</name>
|
||||||
<description>seq</description>
|
<description>seq</description>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ public class BaseConfig {
|
|||||||
private Integer step;
|
private Integer step;
|
||||||
private String type;
|
private String type;
|
||||||
private Integer minLength;
|
private Integer minLength;
|
||||||
|
private Boolean monthZeroFilling;
|
||||||
|
private Boolean dayZeroFilling;
|
||||||
|
|
||||||
private BaseConfig() {
|
private BaseConfig() {
|
||||||
}
|
}
|
||||||
@@ -48,10 +50,10 @@ public class BaseConfig {
|
|||||||
synchronized (BaseConfig.class) {
|
synchronized (BaseConfig.class) {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new BaseConfig();
|
instance = new BaseConfig();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
instance.init(generatorConfig);
|
instance.init(generatorConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,46 +127,47 @@ public class BaseConfig {
|
|||||||
return minLength;
|
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) {
|
public void setMinLength(Integer minLength) {
|
||||||
this.minLength = minLength;
|
this.minLength = minLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init(GeneratorConfig generatorConfig) {
|
private void init(GeneratorConfig generatorConfig) {
|
||||||
//数据库操作模板
|
|
||||||
JdbcTemplate jdbcTemplate = generatorConfig.getJdbcTemplate();
|
|
||||||
|
|
||||||
if (jdbcTemplate == null) {
|
|
||||||
//数据源
|
//数据源
|
||||||
DataSource dataSource = generatorConfig.getDataSource();
|
DataSource dataSource = generatorConfig.getDataSource();
|
||||||
if (dataSource == null)
|
if (dataSource == null)
|
||||||
//若数据库操作模板为空,也没有配置数据源,则抛出异常
|
//若数据库操作模板为空,也没有配置数据源,则抛出异常
|
||||||
throw new NullPointerException("数据源不能为空");
|
throw new NullPointerException("数据源不能为空");
|
||||||
//否则以数据源创建数据库操作模板
|
//否则以数据源创建数据库操作模板
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
|
||||||
|
this.transactionTemplate = new TransactionTemplate(transactionManager);
|
||||||
if (generatorConfig.getTransactionTemplate() == null) {
|
|
||||||
//若没有配置事务操作模板,则从配置中取事务管理器
|
|
||||||
DataSourceTransactionManager transactionManager = generatorConfig.getTransactionManager();
|
|
||||||
if (transactionManager == null) {
|
|
||||||
//若未配置事务管理器,则通过数据源新建
|
|
||||||
DataSource dataSource = jdbcTemplate.getDataSource();
|
|
||||||
if (dataSource == null)
|
|
||||||
throw new NullPointerException("数据源不能为空");
|
|
||||||
transactionManager = new DataSourceTransactionManager(dataSource);
|
|
||||||
}
|
|
||||||
//通过事务管理器创建事务操作模板
|
|
||||||
transactionTemplate = new TransactionTemplate(transactionManager);
|
|
||||||
} else {
|
|
||||||
//获取事务操作模板
|
|
||||||
transactionTemplate = generatorConfig.getTransactionTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
this.sequencesDao = new SequencesDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
|
||||||
this.step = generatorConfig.getStep();
|
this.step = generatorConfig.getStep();
|
||||||
this.type = generatorConfig.getType();
|
this.type = generatorConfig.getType();
|
||||||
this.minLength = generatorConfig.getMinLength();
|
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自动创建表
|
* 自动创建表
|
||||||
*/
|
*/
|
||||||
@@ -53,6 +34,16 @@ public class GeneratorConfig {
|
|||||||
*/
|
*/
|
||||||
private String type = "DEFAULT";
|
private String type = "DEFAULT";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 月份是否补零。为false时,1月显示为1,为true时,1月显示为01
|
||||||
|
*/
|
||||||
|
private Boolean monthZeroFilling = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期是否补零。为false时,1日显示为1,为true时,1日显示为01
|
||||||
|
*/
|
||||||
|
private Boolean dayZeroFilling = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 表和字段配置
|
* 表和字段配置
|
||||||
*/
|
*/
|
||||||
@@ -73,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;
|
||||||
}
|
}
|
||||||
@@ -131,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";
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
if (begin != null && end != null) {
|
||||||
|
sql = "select * from `%s_unlock` where `%s`>=? and `%s`<=?";
|
||||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
||||||
return this.jdbcTemplate.query(sql, rowMapper(), begin, end);
|
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;
|
||||||
|
if (begin != null && end != null) {
|
||||||
|
sql = "delete from `%s_unlock` where `%s`>=? and `%s`<=?";
|
||||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
||||||
int result = this.jdbcTemplate.update(sql, begin, end);
|
return this.jdbcTemplate.update(sql, begin, end) != 0;
|
||||||
return result != 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 '未锁定序号表'";
|
||||||
|
|||||||
@@ -91,9 +91,9 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
|||||||
@Override
|
@Override
|
||||||
public void createTable() {
|
public void createTable() {
|
||||||
String sql = "CREATE TABLE IF NOT EXISTS `%s_unused` ( " +
|
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 '设为闲置序号的时间'," +
|
" `%s` DATETIME NOT NULL COMMENT '设为闲置序号的时间'," +
|
||||||
" PRIMARY KEY ( `%s`, `%s`, `%s` ) " +
|
" PRIMARY KEY ( `%s`, `%s`, `%s` ) " +
|
||||||
" ) COMMENT '闲置序号表'";
|
" ) COMMENT '闲置序号表'";
|
||||||
@@ -118,10 +118,22 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteByDate(Date begin, Date end) {
|
public boolean deleteByDate(Date begin, Date end) {
|
||||||
String sql = "delete from `%s_unused` where `%s`>=? and `%s`<=?";
|
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());
|
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
|
||||||
int result = this.jdbcTemplate.update(sql, begin, end);
|
return this.jdbcTemplate.update(sql, begin, end) != 0;
|
||||||
return result != 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() {
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ import com.yanghuanglin.seq.po.SequencesUnused;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public interface Generator {
|
public interface Generator {
|
||||||
|
/**
|
||||||
|
* 判断格式定义中是否有序号
|
||||||
|
*
|
||||||
|
* @param pattern 格式
|
||||||
|
* @return 是否包含序号
|
||||||
|
*/
|
||||||
|
boolean containSeq(String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据传入的key和type生成可用的序号对象。
|
* 根据传入的key和type生成可用的序号对象。
|
||||||
* <p/>
|
* <p/>
|
||||||
@@ -19,8 +27,14 @@ public interface Generator {
|
|||||||
* <p/>
|
* <p/>
|
||||||
*
|
*
|
||||||
* @param key 数据字典中的编码
|
* @param key 数据字典中的编码
|
||||||
|
* @param withOutSeq 序号对象不包含序号,其值通过{@link #containSeq(String)}来判断
|
||||||
* @return 可用的序号对象
|
* @return 可用的序号对象
|
||||||
*/
|
*/
|
||||||
|
Sequences generate(String key, Boolean withOutSeq);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 {@link #generate(String, Boolean)},第二个参数默认为false
|
||||||
|
*/
|
||||||
Sequences generate(String key);
|
Sequences generate(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,24 +47,36 @@ public interface Generator {
|
|||||||
*
|
*
|
||||||
* @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 BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串,其最小长度通过{@link BaseConfig#getMinLength()}设定。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
@@ -61,7 +87,7 @@ public interface Generator {
|
|||||||
String format(Sequences sequences, String pattern);
|
String format(Sequences sequences, String pattern);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
* 将{@link #generate(String, String, Boolean)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
|
||||||
* <p/>
|
* <p/>
|
||||||
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
*
|
*
|
||||||
@@ -158,7 +184,7 @@ public interface Generator {
|
|||||||
/**
|
/**
|
||||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||||
* <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相同的记录视为锁定。因此此处实际是把这两个表中的记录均删除了
|
||||||
*
|
*
|
||||||
@@ -167,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();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 释放指定时间段内未使用的序号
|
* 释放指定时间段内未使用的序号
|
||||||
@@ -182,22 +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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清空所有闲置序号和未锁定序号
|
* 清空所有闲置序号和未锁定序号
|
||||||
*/
|
*/
|
||||||
void clear();
|
boolean clear();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清空指定时间段内闲置序号和未锁定序号
|
* 清空指定时间段内闲置序号和未锁定序号
|
||||||
*/
|
*/
|
||||||
void clear(Date begin, Date end);
|
boolean clear(Date begin, Date end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
|
||||||
|
*
|
||||||
|
* @param begin 开始时间
|
||||||
|
*/
|
||||||
|
void clearAfter(Date begin);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空结束时间之前的限制序号和未锁定序号
|
||||||
|
*
|
||||||
|
* @param end 结束时间
|
||||||
|
*/
|
||||||
|
void clearBefore(Date end);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ public class SequencesGenerator implements Generator {
|
|||||||
private final Integer step;
|
private final Integer step;
|
||||||
private final String type;
|
private final String type;
|
||||||
private final Integer minLength;
|
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);
|
BaseConfig baseConfig = BaseConfig.getInstance(generatorConfig);
|
||||||
@@ -41,6 +43,8 @@ public class SequencesGenerator implements Generator {
|
|||||||
this.step = baseConfig.getStep();
|
this.step = baseConfig.getStep();
|
||||||
this.type = baseConfig.getType();
|
this.type = baseConfig.getType();
|
||||||
this.minLength = baseConfig.getMinLength();
|
this.minLength = baseConfig.getMinLength();
|
||||||
|
this.monthZeroFilling = baseConfig.getMonthZeroFilling();
|
||||||
|
this.dayZeroFilling = baseConfig.getDayZeroFilling();
|
||||||
|
|
||||||
createTable(generatorConfig.getAutoCreate());
|
createTable(generatorConfig.getAutoCreate());
|
||||||
}
|
}
|
||||||
@@ -50,7 +54,7 @@ public class SequencesGenerator implements Generator {
|
|||||||
*
|
*
|
||||||
* @param autoCreate 是否自动创建
|
* @param autoCreate 是否自动创建
|
||||||
*/
|
*/
|
||||||
private void createTable(Boolean autoCreate) {
|
private synchronized void createTable(Boolean autoCreate) {
|
||||||
if (!autoCreate)
|
if (!autoCreate)
|
||||||
return;
|
return;
|
||||||
this.sequencesDao.createTable();
|
this.sequencesDao.createTable();
|
||||||
@@ -60,23 +64,36 @@ public class SequencesGenerator implements Generator {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized Sequences generate(String key) {
|
public boolean containSeq(String pattern) {
|
||||||
return generate(key, type);
|
return StringUtils.hasLength(pattern) && pattern.contains(SEQ.getPlaceholder());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized Sequences generate(String key, String type) {
|
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) {
|
||||||
|
//不是不包含序号的序号对象(有可能格式中只配置了#year##month##day#,序号就不用自增,也不用入库,直接返回即可)
|
||||||
//找到正在使用的最大序号
|
//找到正在使用的最大序号
|
||||||
Sequences sequences = sequencesDao.find(condition);
|
Sequences sequences = sequencesDao.find(condition);
|
||||||
|
boolean result = true;
|
||||||
if (sequences == null) {
|
if (sequences == null) {
|
||||||
//不存在,说明还没生成,将新生成的入库,此时序号为默认的0
|
//不存在,说明还没生成,将新生成的入库,此时序号为默认的0
|
||||||
sequences = condition;
|
sequences = condition;
|
||||||
sequencesDao.save(sequences);
|
result = sequencesDao.save(sequences);
|
||||||
}
|
}
|
||||||
|
|
||||||
//根据传入的key和type查找空闲编号最小的一个
|
//根据传入的key和type查找空闲编号最小的一个
|
||||||
@@ -89,7 +106,7 @@ public class SequencesGenerator implements Generator {
|
|||||||
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 {
|
} else {
|
||||||
//空闲编号存在,说明已经生成过,序号不需要增加,直接使用。同时将该空闲编号移动到使用中表
|
//空闲编号存在,说明已经生成过,序号不需要增加,直接使用。同时将该空闲编号移动到使用中表
|
||||||
@@ -97,10 +114,15 @@ public class SequencesGenerator implements Generator {
|
|||||||
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequencesUnused);
|
SequencesUnlock sequencesUnlock = new SequencesUnlock(sequencesUnused);
|
||||||
sequencesUnlock.setCreateTime(new Date());
|
sequencesUnlock.setCreateTime(new Date());
|
||||||
|
|
||||||
sequencesUnlockDao.save(sequencesUnlock);
|
result = result && sequencesUnlockDao.save(sequencesUnlock) &&
|
||||||
sequencesUnusedDao.delete(sequencesUnused);
|
sequencesUnusedDao.delete(sequencesUnused);
|
||||||
}
|
}
|
||||||
return sequences;
|
sequences.setWithOutSeq(false);
|
||||||
|
return result ? sequences : null;
|
||||||
|
} else {
|
||||||
|
condition.setWithOutSeq(true);
|
||||||
|
return condition;
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
status.setRollbackOnly();
|
status.setRollbackOnly();
|
||||||
@@ -110,13 +132,23 @@ 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
|
@Override
|
||||||
public String format(Sequences sequences, String pattern) {
|
public String format(Sequences sequences, String pattern) {
|
||||||
return format(sequences, minLength, pattern);
|
return format(sequences, minLength, pattern);
|
||||||
@@ -148,9 +180,12 @@ public class SequencesGenerator implements Generator {
|
|||||||
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(YEAR.getPlaceholder(), String.valueOf(calendar.get(Calendar.YEAR)));
|
int year = calendar.get(Calendar.YEAR);
|
||||||
pattern = pattern.replaceAll(MONTH.getPlaceholder(), String.format("%02d", calendar.get(Calendar.MONTH) + 1));
|
int month = calendar.get(Calendar.MONTH) + 1;
|
||||||
pattern = pattern.replaceAll(DAY.getPlaceholder(), String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
|
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
|
pattern = pattern.replaceAll(YEAR.getPlaceholder(), String.valueOf(year));
|
||||||
|
pattern = pattern.replaceAll(MONTH.getPlaceholder(), monthZeroFilling ? String.format("%02d", month) : String.valueOf(month));
|
||||||
|
pattern = pattern.replaceAll(DAY.getPlaceholder(), dayZeroFilling ? String.format("%02d", day) : String.valueOf(day));
|
||||||
pattern = pattern.replaceAll(SEQ.getPlaceholder(), seqString);
|
pattern = pattern.replaceAll(SEQ.getPlaceholder(), seqString);
|
||||||
return pattern;
|
return pattern;
|
||||||
}
|
}
|
||||||
@@ -228,6 +263,7 @@ 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);
|
sequences.setType(type);
|
||||||
|
|
||||||
return sequences;
|
return sequences;
|
||||||
@@ -235,15 +271,43 @@ public class SequencesGenerator implements Generator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean lock(Sequences sequences) {
|
public synchronized boolean lock(Sequences sequences) {
|
||||||
if (sequences == null)
|
if (sequences == null || sequences.getWithOutSeq())
|
||||||
return true;
|
return true;
|
||||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||||
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
//将使用中表的对应数据删除,空闲表中数据在生成时会删除,因此这里不需要处理该表
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
return sequencesUnlockDao.delete(condition);
|
return sequencesUnlockDao.delete(condition);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized 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();
|
||||||
|
|
||||||
@@ -253,13 +317,23 @@ public class SequencesGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//将使用中表的序号放到空闲表中
|
//将使用中表的序号放到空闲表中
|
||||||
sequencesUnusedDao.saveBatch(sequencesUnusedList);
|
|
||||||
//删除所有使用中表的数据
|
//删除所有使用中表的数据
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
//将使用中表的序号放到空闲表中
|
||||||
|
//删除所有使用中表的数据
|
||||||
|
return sequencesUnusedDao.saveBatch(sequencesUnusedList) &&
|
||||||
sequencesUnlockDao.deleteAll();
|
sequencesUnlockDao.deleteAll();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized 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);
|
||||||
|
|
||||||
@@ -269,28 +343,103 @@ public class SequencesGenerator implements Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//将指定时间段内使用中表的序号放到空闲表中
|
//将指定时间段内使用中表的序号放到空闲表中
|
||||||
sequencesUnusedDao.saveBatch(sequencesUnusedList);
|
|
||||||
//删除指定时间段内使用中表的数据
|
//删除指定时间段内使用中表的数据
|
||||||
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
//将指定时间段内使用中表的序号放到空闲表中
|
||||||
|
//删除指定时间段内使用中表的数据
|
||||||
|
return sequencesUnusedDao.saveBatch(sequencesUnusedList) &&
|
||||||
sequencesUnlockDao.deleteByDate(begin, end);
|
sequencesUnlockDao.deleteByDate(begin, end);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void release(Sequences sequences) {
|
public synchronized void releaseAfter(Date begin) {
|
||||||
|
release(begin, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void releaseBefore(Date end) {
|
||||||
|
release(null, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean release(Sequences sequences) {
|
||||||
if (sequences == null)
|
if (sequences == null)
|
||||||
return;
|
return true;
|
||||||
sequencesUnlockDao.delete(new SequencesUnlock(sequences));
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
sequencesUnusedDao.save(new SequencesUnused(sequences, new Date()));
|
try {
|
||||||
|
return sequencesUnlockDao.delete(new SequencesUnlock(sequences))
|
||||||
|
&& sequencesUnusedDao.save(new SequencesUnused(sequences, new Date()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public synchronized boolean release(Sequences sequences, boolean ignoreSeq) {
|
||||||
sequencesUnlockDao.deleteAll();
|
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();
|
sequencesUnusedDao.deleteAll();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
status.setRollbackOnly();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear(Date begin, Date end) {
|
public synchronized boolean clear(Date begin, Date end) {
|
||||||
sequencesUnlockDao.deleteByDate(begin, end);
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
|
try {
|
||||||
|
return sequencesUnlockDao.deleteByDate(begin, end) &&
|
||||||
sequencesUnusedDao.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,11 @@ public class Sequences {
|
|||||||
*/
|
*/
|
||||||
private transient Integer day;
|
private transient Integer day;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时字段,表示序号对象不包含序号
|
||||||
|
*/
|
||||||
|
private transient Boolean withOutSeq;
|
||||||
|
|
||||||
public Sequences() {
|
public Sequences() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将序号增加指定步长
|
* 将序号增加指定步长
|
||||||
*
|
*
|
||||||
@@ -129,9 +142,9 @@ public class Sequences {
|
|||||||
* @return 补零后的序号,若未单独设置序号的长度,则最小长度为{@link BaseConfig#getMinLength()}长度;否则为修改后的长度,不足部分补零
|
* @return 补零后的序号,若未单独设置序号的长度,则最小长度为{@link BaseConfig#getMinLength()}长度;否则为修改后的长度,不足部分补零
|
||||||
*/
|
*/
|
||||||
public String format() {
|
public String format() {
|
||||||
BaseConfig baseConfig = BaseConfig.getInstance();
|
Integer minLength = BaseConfig.getInstance().getMinLength();
|
||||||
if (baseConfig.getMinLength() != null)
|
if (minLength != null)
|
||||||
return String.format("%0" + baseConfig.getMinLength() + "d", this.seq);
|
return String.format("%0" + minLength + "d", this.seq);
|
||||||
return String.valueOf(this.seq);
|
return String.valueOf(this.seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,6 +169,7 @@ public class Sequences {
|
|||||||
", year=" + year +
|
", year=" + year +
|
||||||
", month=" + month +
|
", month=" + month +
|
||||||
", day=" + day +
|
", day=" + day +
|
||||||
|
", withOutSeq=" + withOutSeq +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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("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(), "处〔#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