增加默认序号类型

This commit is contained in:
yhl452493373
2022-02-11 14:45:36 +08:00
parent 6bfcb1fd51
commit 956e707399
5 changed files with 66 additions and 16 deletions

View File

@@ -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.3.1.jar、seq-1.3.1-sources.jar、seq-1.3.1-pom.xml复制过去
+ 在pom.xml中增加以下内容然后执行maven命令mvn clean
```xml
@@ -18,7 +18,7 @@
<dependency>
<groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId>
<version>1.2.0</version>
<version>1.3.1</version>
<exclusions>
<!-- 如若你项目中有引用spring-jdbc则需要排除seq的jdbc依赖 -->
<exclusion>
@@ -33,7 +33,7 @@
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 每次执行mvn clean时自动安装指定的jar包 -->
@@ -50,13 +50,13 @@
</goals>
<configuration>
<!-- ${project.basedir}表示当前项目的根目录 -->
<file>${project.basedir}/lib/seq-1.2.0.jar</file>
<pomFile>${pom.basedir}/lib/seq-1.2.0-pom.xml</pomFile>
<sources>${project.basedir}/lib/seq-1.2.0-sources.jar</sources>
<file>${project.basedir}/lib/seq-1.3.1.jar</file>
<pomFile>${pom.basedir}/lib/seq-1.3.1-pom.xml</pomFile>
<sources>${project.basedir}/lib/seq-1.3.1-sources.jar</sources>
<repositoryLayout>default</repositoryLayout>
<groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId>
<version>1.2.0</version>
<version>1.3.1</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
@@ -279,7 +279,8 @@ GeneratorConfig配置项通过set方法设置
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
| autoCreate | Boolean | true | 开启自动建表 |
| step | Integer | 1 | 序号增加时的步长 |
| tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 |
| type | String | DEFAULT | 默认序号类型 |
| tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 |
以上配置中jdbcTemplate和transactionTemplate优先级最高如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置则dataSource和transactionManager无效
进行这几种组合dataSource+autoCreate+step+tableConfigjdbcTemplate+transactionTemplate+autoCreate+step+tableConfigjdbcTemplate+transactionManager+autoCreate+step+tableConfig
@@ -290,6 +291,7 @@ Generator方法如下
```java
package com.yanghuanglin.seq.generator;
import com.yanghuanglin.seq.config.GeneratorConfig;
import com.yanghuanglin.seq.po.Sequences;
import com.yanghuanglin.seq.po.SequencesUnlock;
import com.yanghuanglin.seq.po.SequencesUnused;
@@ -314,6 +316,19 @@ public interface Generator {
*/
String SEQ = "#seq#";
/**
* 根据传入的key和type生成可用的序号对象。
* <p/>
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找不到记录说明该组合的序号对象还未初次生成返回的是seq为step的序号对象该对象数据会写入到{@link SequencesUnlock}中。
* <p/>
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除然后写入到{@link SequencesUnlock}中。
* <p/>
*
* @param key 数据字典中的编码
* @return 可用的序号对象
*/
Sequences generate(String key);
/**
* 根据传入的key和type生成可用的序号对象。
* <p/>
@@ -386,7 +401,7 @@ public interface Generator {
/**
* 将已格式化的序号解析为序号对象
* <p/>
* 返回的序号对象{@link Sequences#getKey()}{@link Sequences#getType()}为null,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* 返回的序号对象{@link Sequences#getKey()}为null{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* <p/>
* 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key
* <p/>
@@ -396,7 +411,7 @@ public interface Generator {
*
* @param formatted 格式化后的序号字符串
* @param pattern 序号格式
* @return 包含了序号字符串对应年如果有、月如果有、日如果有、序号的序号对象其key、type需要根据情况手动设置
* @return 包含了序号字符串对应年如果有、月如果有、日如果有、序号的序号对象其key需要根据情况手动设置type为{@link GeneratorConfig#getType()}的默认值
*/
Sequences parse(String formatted, String pattern);

View File

@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId>
<version>1.2.1</version>
<version>1.3.1</version>
<name>seq</name>
<description>seq</description>
<properties>

View File

@@ -43,6 +43,11 @@ public class GeneratorConfig {
*/
private Integer step = 1;
/**
* 默认序号类型
*/
private String type="DEFAULT";
/**
* 表和字段配置
*/
@@ -95,6 +100,14 @@ public class GeneratorConfig {
this.autoCreate = autoCreate;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Integer getStep() {
return step;
}

View File

@@ -1,5 +1,6 @@
package com.yanghuanglin.seq.generator;
import com.yanghuanglin.seq.config.GeneratorConfig;
import com.yanghuanglin.seq.po.Sequences;
import com.yanghuanglin.seq.po.SequencesUnlock;
import com.yanghuanglin.seq.po.SequencesUnused;
@@ -24,6 +25,19 @@ public interface Generator {
*/
String SEQ = "#seq#";
/**
* 根据传入的key和type生成可用的序号对象。
* <p/>
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找不到记录说明该组合的序号对象还未初次生成返回的是seq为step的序号对象该对象数据会写入到{@link SequencesUnlock}中。
* <p/>
* 如果根据key和默认的{@link GeneratorConfig#getType()}在{@link Sequences}中找到了记录,且在{@link SequencesUnused}也找到了记录,说明该组合生成的序号有部分未使用,返回的是{@link SequencesUnused}中找到的seq最小的序号对象。同时会将{@link SequencesUnused}中找到的seq最小的记录删除然后写入到{@link SequencesUnlock}中。
* <p/>
*
* @param key 数据字典中的编码
* @return 可用的序号对象
*/
Sequences generate(String key);
/**
* 根据传入的key和type生成可用的序号对象。
* <p/>
@@ -96,7 +110,7 @@ public interface Generator {
/**
* 将已格式化的序号解析为序号对象
* <p/>
* 返回的序号对象{@link Sequences#getKey()}{@link Sequences#getType()}为null,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* 返回的序号对象{@link Sequences#getKey()}为null{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* <p/>
* 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key
* <p/>
@@ -106,7 +120,7 @@ public interface Generator {
*
* @param formatted 格式化后的序号字符串
* @param pattern 序号格式
* @return 包含了序号字符串对应年如果有、月如果有、日如果有、序号的序号对象其key、type需要根据情况手动设置
* @return 包含了序号字符串对应年如果有、月如果有、日如果有、序号的序号对象其key需要根据情况手动设置type为{@link GeneratorConfig#getType()}的默认值
*/
Sequences parse(String formatted, String pattern);

View File

@@ -30,6 +30,7 @@ public class SequencesGenerator implements Generator {
private final SequencesUnusedDao sequencesUnusedDao;
private final SequencesUnlockDao sequencesUnlockDao;
private final Integer step;
private final String type;
public SequencesGenerator(GeneratorConfig generatorConfig) {
//数据库操作模板
@@ -66,6 +67,7 @@ public class SequencesGenerator implements Generator {
this.sequencesUnusedDao = new SequencesUnusedDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
this.sequencesUnlockDao = new SequencesUnlockDaoImpl(jdbcTemplate, generatorConfig.getTableConfig());
this.step = generatorConfig.getStep();
this.type = generatorConfig.getType();
autoCreateTable(generatorConfig.getAutoCreate());
}
@@ -81,6 +83,11 @@ public class SequencesGenerator implements Generator {
this.sequencesUnlockDao.createTable();
}
@Override
public synchronized Sequences generate(String key) {
return generate(key, type);
}
@Override
public synchronized Sequences generate(String key, String type) {
return transactionTemplate.execute(status -> {
@@ -227,6 +234,7 @@ public class SequencesGenerator implements Generator {
sequences.setMonth(StringUtils.hasLength(month) ? Integer.valueOf(month) : null);
sequences.setDay(StringUtils.hasLength(day) ? Integer.valueOf(day) : null);
sequences.setSeq(StringUtils.hasLength(seq) ? Long.parseLong(seq) : 0L);
sequences.setType(type);
return sequences;
}
@@ -239,7 +247,7 @@ public class SequencesGenerator implements Generator {
}
@Override
public void release() {
public synchronized void release() {
//列出所有使用中表存在的序号
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll();
@@ -255,7 +263,7 @@ public class SequencesGenerator implements Generator {
}
@Override
public void release(Date begin, Date end) {
public synchronized void release(Date begin, Date end) {
//列出指定时间段内使用中表存在的序号
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end);
@@ -271,7 +279,7 @@ public class SequencesGenerator implements Generator {
}
@Override
public void release(Sequences sequences) {
public synchronized void release(Sequences sequences) {
sequencesUnlockDao.delete(new SequencesUnlock(sequences));
sequencesUnusedDao.save(new SequencesUnused(sequences));
}