6 Commits
1.1.0 ... 1.3.1

9 changed files with 301 additions and 22 deletions

View File

@@ -8,7 +8,7 @@
使用方法: 使用方法:
+ 在项目中放置jar包的地方把seq-1.0.0.jar、seq-1.0.0-sources.jar、seq-1.0.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 + 在pom.xml中增加以下内容然后执行maven命令mvn clean
```xml ```xml
@@ -18,7 +18,7 @@
<dependency> <dependency>
<groupId>com.yanghuanglin</groupId> <groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId> <artifactId>seq</artifactId>
<version>1.0.0</version> <version>1.3.1</version>
<exclusions> <exclusions>
<!-- 如若你项目中有引用spring-jdbc则需要排除seq的jdbc依赖 --> <!-- 如若你项目中有引用spring-jdbc则需要排除seq的jdbc依赖 -->
<exclusion> <exclusion>
@@ -50,13 +50,13 @@
</goals> </goals>
<configuration> <configuration>
<!-- ${project.basedir}表示当前项目的根目录 --> <!-- ${project.basedir}表示当前项目的根目录 -->
<file>${project.basedir}/lib/seq-1.0.0.jar</file> <file>${project.basedir}/lib/seq-1.3.1.jar</file>
<pomFile>${pom.basedir}/lib/seq-1.0.0-pom.xml</pomFile> <pomFile>${pom.basedir}/lib/seq-1.3.1-pom.xml</pomFile>
<sources>${project.basedir}/lib/seq-1.0.0-sources.jar</sources> <sources>${project.basedir}/lib/seq-1.3.1-sources.jar</sources>
<repositoryLayout>default</repositoryLayout> <repositoryLayout>default</repositoryLayout>
<groupId>com.yanghuanglin</groupId> <groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId> <artifactId>seq</artifactId>
<version>1.0.0</version> <version>1.3.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<generatePom>true</generatePom> <generatePom>true</generatePom>
</configuration> </configuration>
@@ -279,6 +279,7 @@ GeneratorConfig配置项通过set方法设置
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 | | transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
| autoCreate | Boolean | true | 开启自动建表 | | autoCreate | Boolean | true | 开启自动建表 |
| step | Integer | 1 | 序号增加时的步长 | | step | Integer | 1 | 序号增加时的步长 |
| type | String | DEFAULT | 默认序号类型 |
| tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 | | tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 |
以上配置中jdbcTemplate和transactionTemplate优先级最高如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置则dataSource和transactionManager无效 以上配置中jdbcTemplate和transactionTemplate优先级最高如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置则dataSource和transactionManager无效
@@ -290,6 +291,7 @@ Generator方法如下
```java ```java
package com.yanghuanglin.seq.generator; package com.yanghuanglin.seq.generator;
import com.yanghuanglin.seq.config.GeneratorConfig;
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;
@@ -314,6 +316,19 @@ public interface Generator {
*/ */
String SEQ = "#seq#"; 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生成可用的序号对象。 * 根据传入的key和type生成可用的序号对象。
* <p/> * <p/>
@@ -340,6 +355,16 @@ public interface Generator {
*/ */
String generate(String key, String type, Integer minLength); String generate(String key, String type, Integer minLength);
/**
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
*
* @param sequences 生成的序号对象
* @param minLength 序号数字最小长度
* @param pattern 格式
* @return 格式化后的字符串
*/
String format(Sequences sequences, Integer minLength, String pattern);
/** /**
* 将生成的序号对象格式化为指定格式 * 将生成的序号对象格式化为指定格式
* <p/> * <p/>
@@ -352,7 +377,7 @@ public interface Generator {
* @param seq 需要格式化的序号 * @param seq 需要格式化的序号
* @param minLength 序号最小长度,不足的会补零 * @param minLength 序号最小长度,不足的会补零
* @param pattern 格式 * @param pattern 格式
* @return 格式化后的字符串 * @return 格式化后的序号字符串
*/ */
String format(Long seq, Integer minLength, String pattern); String format(Long seq, Integer minLength, String pattern);
@@ -368,11 +393,28 @@ public interface Generator {
* @param seq 需要格式化的序号 * @param seq 需要格式化的序号
* @param start 序号格式化后以什么字符串开头 * @param start 序号格式化后以什么字符串开头
* @param minLength 序号最小长度,不足的会补零 * @param minLength 序号最小长度,不足的会补零
* @param pattern 格式 * @param pattern 序号格式
* @return 格式化后的字符串 * @return 格式化后的序号字符串
*/ */
String format(Long seq, String start, Integer minLength, String pattern); String format(Long seq, String start, Integer minLength, String pattern);
/**
* 将已格式化的序号解析为序号对象
* <p/>
* 返回的序号对象{@link Sequences#getKey()}为null{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* <p/>
* 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key
* <p/>
* 例如SNT序号每年都从1开始则key应该是类似SNT2021、SNT2022这种格式而在配置中该序号的代码只是SNT但是由于每年都要从1开始所有应该每年有一个key这个key就为SNT+年份,而这个年份就是此处解析后返回的对象中的{@link Sequences#getYear()}
* <p/>
* 注意:序号格式和格式化后的字符串占位一定要匹配。如:处〔#year##month#10801第#seq#号 对应 处20220210801第10001号而不能对应 处2022021110801第10001号
*
* @param formatted 格式化后的序号字符串
* @param pattern 序号格式
* @return 包含了序号字符串对应年如果有、月如果有、日如果有、序号的序号对象其key需要根据情况手动设置type为{@link GeneratorConfig#getType()}的默认值
*/
Sequences parse(String formatted, String pattern);
/** /**
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法 * 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
* <p/> * <p/>
@@ -401,6 +443,13 @@ public interface Generator {
* @param end 结束时间 * @param end 结束时间
*/ */
void release(Date begin, Date end); void release(Date begin, Date end);
/**
* 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
*
* @param sequences 需要释放的序号。一般是一个通过{@link Sequences#setKey(String)}、{@link Sequences#setType(String)}、{@link Sequences#setSeq(Long)}三方法一起手动构建或通过{@link Sequences#Sequences(String, String, Long)}构造方法构建的实例对象
*/
void release(Sequences sequences);
} }
``` ```

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
package com.yanghuanglin.seq.generator; package com.yanghuanglin.seq.generator;
import com.yanghuanglin.seq.config.GeneratorConfig;
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;
@@ -24,6 +25,19 @@ public interface Generator {
*/ */
String SEQ = "#seq#"; 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生成可用的序号对象。 * 根据传入的key和type生成可用的序号对象。
* <p/> * <p/>
@@ -50,6 +64,16 @@ public interface Generator {
*/ */
String generate(String key, String type, Integer minLength); String generate(String key, String type, Integer minLength);
/**
* 将{@link #generate(String, String)}得到的序号对象格式化为补零后的序号字符串。实际上只会用到{@link Sequences#getSeq()}属性
*
* @param sequences 生成的序号对象
* @param minLength 序号数字最小长度
* @param pattern 格式
* @return 格式化后的字符串
*/
String format(Sequences sequences, Integer minLength, String pattern);
/** /**
* 将生成的序号对象格式化为指定格式 * 将生成的序号对象格式化为指定格式
* <p/> * <p/>
@@ -62,7 +86,7 @@ public interface Generator {
* @param seq 需要格式化的序号 * @param seq 需要格式化的序号
* @param minLength 序号最小长度,不足的会补零 * @param minLength 序号最小长度,不足的会补零
* @param pattern 格式 * @param pattern 格式
* @return 格式化后的字符串 * @return 格式化后的序号字符串
*/ */
String format(Long seq, Integer minLength, String pattern); String format(Long seq, Integer minLength, String pattern);
@@ -78,11 +102,28 @@ public interface Generator {
* @param seq 需要格式化的序号 * @param seq 需要格式化的序号
* @param start 序号格式化后以什么字符串开头 * @param start 序号格式化后以什么字符串开头
* @param minLength 序号最小长度,不足的会补零 * @param minLength 序号最小长度,不足的会补零
* @param pattern 格式 * @param pattern 序号格式
* @return 格式化后的字符串 * @return 格式化后的序号字符串
*/ */
String format(Long seq, String start, Integer minLength, String pattern); String format(Long seq, String start, Integer minLength, String pattern);
/**
* 将已格式化的序号解析为序号对象
* <p/>
* 返回的序号对象{@link Sequences#getKey()}为null{@link Sequences#getType()}为{@link GeneratorConfig#getType()}的默认值,但是临时字段{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}可能有值
* <p/>
* 如果生成序号时序号的key在年、月、日上有关联如每年每月的序号要从1开始则需要自行用序号字符串与{@link Sequences#getYear()}、{@link Sequences#getMonth()}、{@link Sequences#getDay()}进行组合进而得到key
* <p/>
* 例如SNT序号每年都从1开始则key应该是类似SNT2021、SNT2022这种格式而在配置中该序号的代码只是SNT但是由于每年都要从1开始所有应该每年有一个key这个key就为SNT+年份,而这个年份就是此处解析后返回的对象中的{@link Sequences#getYear()}
* <p/>
* 注意:序号格式和格式化后的字符串占位一定要匹配。如:处〔#year##month#10801第#seq#号 对应 处20220210801第10001号而不能对应 处2022021110801第10001号
*
* @param formatted 格式化后的序号字符串
* @param pattern 序号格式
* @return 包含了序号字符串对应年如果有、月如果有、日如果有、序号的序号对象其key需要根据情况手动设置type为{@link GeneratorConfig#getType()}的默认值
*/
Sequences parse(String formatted, String pattern);
/** /**
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法 * 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
* <p/> * <p/>

View File

@@ -13,14 +13,16 @@ 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.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.support.TransactionTemplate; import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.StringUtils;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SequencesGenerator implements Generator { public class SequencesGenerator implements Generator {
private final TransactionTemplate transactionTemplate; private final TransactionTemplate transactionTemplate;
@@ -28,6 +30,7 @@ public class SequencesGenerator implements Generator {
private final SequencesUnusedDao sequencesUnusedDao; private final SequencesUnusedDao sequencesUnusedDao;
private final SequencesUnlockDao sequencesUnlockDao; private final SequencesUnlockDao sequencesUnlockDao;
private final Integer step; private final Integer step;
private final String type;
public SequencesGenerator(GeneratorConfig generatorConfig) { public SequencesGenerator(GeneratorConfig generatorConfig) {
//数据库操作模板 //数据库操作模板
@@ -51,7 +54,7 @@ public class SequencesGenerator implements Generator {
DataSource dataSource = jdbcTemplate.getDataSource(); DataSource dataSource = jdbcTemplate.getDataSource();
if (dataSource == null) if (dataSource == null)
throw new NullPointerException("数据源不能为空"); throw new NullPointerException("数据源不能为空");
transactionManager = new JdbcTransactionManager(dataSource); transactionManager = new DataSourceTransactionManager(dataSource);
} }
//通过事务管理器创建事务操作模板 //通过事务管理器创建事务操作模板
transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate = new TransactionTemplate(transactionManager);
@@ -64,6 +67,7 @@ public class SequencesGenerator implements Generator {
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();
autoCreateTable(generatorConfig.getAutoCreate()); autoCreateTable(generatorConfig.getAutoCreate());
} }
@@ -79,6 +83,11 @@ public class SequencesGenerator implements Generator {
this.sequencesUnlockDao.createTable(); this.sequencesUnlockDao.createTable();
} }
@Override
public synchronized Sequences generate(String key) {
return generate(key, type);
}
@Override @Override
public synchronized Sequences generate(String key, String type) { public synchronized Sequences generate(String key, String type) {
return transactionTemplate.execute(status -> { return transactionTemplate.execute(status -> {
@@ -132,6 +141,11 @@ public class SequencesGenerator implements Generator {
return sequences.format(minLength); return sequences.format(minLength);
} }
@Override
public String format(Sequences sequences, Integer minLength, String pattern) {
return format(sequences.getSeq(), 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);
@@ -150,6 +164,81 @@ public class SequencesGenerator implements Generator {
return pattern; return pattern;
} }
@Override
public Sequences parse(String formatted, String pattern) {
//年、月、日、序号分隔特殊符号正则规则
String splitRegString = "(" + YEAR + "|" + MONTH + "|" + DAY + "|" + SEQ + ")";
//根据年、月、日、序号的特殊符号,对格式进行分隔,得到排除特殊符号后的字符串数组
String[] split = pattern.split(splitRegString);
for (String splitString : split) {
//用排除特殊符号后的字符串依次替换序号格式字符串和格式化后的序号字符串,得到纯净的序号字符串和序号格式对应关系
pattern = pattern.replace(splitString, "");
formatted = formatted.replace(splitString, "");
}
//年、月、日的数字匹配正则规则
String yearRegStr = "\\d{4}";
String monthRegStr = "\\d{2}";
String dayRegStr = "\\d{2}";
//将序号格式分隔特殊符号字符串转为正则匹配规则
Pattern seqPattern = Pattern.compile(splitRegString);
//对序号格式进行匹配
Matcher matcher = seqPattern.matcher(pattern);
//将年、月、日匹配规则字符串转为正则匹配规则
Pattern yearPattern = Pattern.compile(yearRegStr);
Pattern monthPattern = Pattern.compile(monthRegStr);
Pattern dayPattern = Pattern.compile(dayRegStr);
//默认的年、月、日均为空字符串
String year = "", month = "", day = "", seq = "";
//根据序号匹配规则字符串查找字符串分组
while (matcher.find()) {
String group = matcher.group();
switch (group) {
case Generator.YEAR:
//若分组为年份分组则将年份正则匹配到的字符串赋值给year同时把格式化后的序号字符串中对应年的字符串替换为空字符串
Matcher yearMatcher = yearPattern.matcher(formatted);
if (yearMatcher.find()) {
year = yearMatcher.group();
}
formatted = formatted.replaceFirst(yearRegStr, "");
break;
case Generator.MONTH:
//若分组为月份分组则将月份正则匹配到的字符串赋值给month同时把格式化后的序号字符串中对应月的字符串替换为空字符串
Matcher monthMatcher = monthPattern.matcher(formatted);
if (monthMatcher.find()) {
month = monthMatcher.group();
}
formatted = formatted.replaceFirst(monthRegStr, "");
break;
case Generator.DAY:
//若分组为日期分组则将日期正则匹配到的字符串赋值给day同时把格式化后的序号字符串中对应日期的字符串替换为空字符串
Matcher dayMatcher = dayPattern.matcher(formatted);
if (dayMatcher.find()) {
day = dayMatcher.group();
}
formatted = formatted.replaceFirst(dayRegStr, "");
break;
}
}
//经过以上替换后最后剩下的为序号这个序号可能是补零了的需要调用Long.parseLong来去零
seq = formatted;
//构建一个新的序号对象
Sequences sequences = new Sequences();
//用获取到的年、月、日、序号给新构建的序号对象设置对应的值
sequences.setYear(StringUtils.hasLength(year) ? Integer.valueOf(year) : null);
sequences.setMonth(StringUtils.hasLength(month) ? Integer.valueOf(month) : null);
sequences.setDay(StringUtils.hasLength(day) ? Integer.valueOf(day) : null);
sequences.setSeq(StringUtils.hasLength(seq) ? Long.parseLong(seq) : 0L);
sequences.setType(type);
return sequences;
}
@Override @Override
public synchronized boolean lock(Sequences sequences) { public synchronized boolean lock(Sequences sequences) {
SequencesUnlock condition = new SequencesUnlock(sequences); SequencesUnlock condition = new SequencesUnlock(sequences);
@@ -158,7 +247,7 @@ public class SequencesGenerator implements Generator {
} }
@Override @Override
public void release() { public synchronized void release() {
//列出所有使用中表存在的序号 //列出所有使用中表存在的序号
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll(); List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listAll();
@@ -174,7 +263,7 @@ public class SequencesGenerator implements Generator {
} }
@Override @Override
public void release(Date begin, Date end) { public synchronized void release(Date begin, Date end) {
//列出指定时间段内使用中表存在的序号 //列出指定时间段内使用中表存在的序号
List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end); List<SequencesUnlock> sequencesUnlockList = sequencesUnlockDao.listByDate(begin, end);
@@ -190,7 +279,7 @@ public class SequencesGenerator implements Generator {
} }
@Override @Override
public void release(Sequences sequences) { public synchronized void release(Sequences sequences) {
sequencesUnlockDao.delete(new SequencesUnlock(sequences)); sequencesUnlockDao.delete(new SequencesUnlock(sequences));
sequencesUnusedDao.save(new SequencesUnused(sequences)); sequencesUnusedDao.save(new SequencesUnused(sequences));
} }

View File

@@ -23,6 +23,24 @@ public class Sequences {
*/ */
protected Long seq = 0L; protected Long seq = 0L;
/**
* 临时字段序号对应的年份如2022年如果能获取到的话
* 该字段仅用于解析序号字符串时解析出对应年份用于合成key序号对应的key为SNT+年份,返回的为其年份)
*/
private transient Integer year;
/**
* 临时字段序号对应的月份如2月如果能获取到的话
* 该字段仅用于解析序号字符串时解析出对应月份用于合成key序号对应的key为SNT+年份+月份,返回的为其月份)
*/
private transient Integer month;
/**
* 临时字段序号对应的日期如1日如果能获取到的话
* 该字段仅用于解析序号字符串时解析出对应日期用于合成key序号对应的key为SNT+年份+月份+日期,返回的为其日期)
*/
private transient Integer day;
public Sequences() { public Sequences() {
} }
@@ -71,6 +89,30 @@ public class Sequences {
this.seq = seq; this.seq = seq;
} }
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getMonth() {
return month;
}
public void setMonth(Integer month) {
this.month = month;
}
public Integer getDay() {
return day;
}
public void setDay(Integer day) {
this.day = day;
}
/** /**
* 将序号增加指定步长 * 将序号增加指定步长
* *
@@ -91,4 +133,16 @@ public class Sequences {
return String.format("%0" + minLength + "d", this.seq); return String.format("%0" + minLength + "d", this.seq);
return String.valueOf(this.seq); return String.valueOf(this.seq);
} }
@Override
public String toString() {
return "Sequences{" +
"key='" + key + '\'' +
", type='" + type + '\'' +
", seq=" + seq +
", year=" + year +
", month=" + month +
", day=" + day +
'}';
}
} }

View File

@@ -34,4 +34,14 @@ public class SequencesUnlock extends Sequences {
public void setCreateTime(Date createTime) { public void setCreateTime(Date createTime) {
this.createTime = createTime; this.createTime = createTime;
} }
@Override
public String toString() {
return "SequencesUnlock{" +
"key='" + key + '\'' +
", type='" + type + '\'' +
", seq=" + seq +
", createTime=" + createTime +
'}';
}
} }

View File

@@ -16,4 +16,13 @@ public class SequencesUnused extends Sequences {
this.type = sequences.getType(); this.type = sequences.getType();
this.seq = sequences.getSeq(); this.seq = sequences.getSeq();
} }
@Override
public String toString() {
return "SequencesUnused{" +
"key='" + key + '\'' +
", type='" + type + '\'' +
", seq=" + seq +
'}';
}
} }

View File

@@ -1,9 +1,9 @@
import com.mysql.cj.jdbc.MysqlDataSource;
import com.yanghuanglin.seq.config.GeneratorConfig; import com.yanghuanglin.seq.config.GeneratorConfig;
import com.yanghuanglin.seq.config.TableConfig; import com.yanghuanglin.seq.config.TableConfig;
import com.yanghuanglin.seq.po.Sequences;
import com.yanghuanglin.seq.generator.Generator; import com.yanghuanglin.seq.generator.Generator;
import com.yanghuanglin.seq.generator.impl.SequencesGenerator; import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
import com.mysql.cj.jdbc.MysqlDataSource; import com.yanghuanglin.seq.po.Sequences;
import org.junit.Test; import org.junit.Test;
import java.util.HashSet; import java.util.HashSet;
@@ -73,4 +73,18 @@ public class SeqTest {
String s = "select * from sequences where `%s`=? and `%s`=?"; String s = "select * from sequences where `%s`=? and `%s`=?";
System.out.println(String.format(s, "key", "value")); System.out.println(String.format(s, "key", "value"));
} }
@Test
public void parseTest() {
String seqPattern = "ZZF#year##month##seq#";
String formatted = "ZZF20220200008";
Sequences sequences = generator.parse(formatted, seqPattern);
String key = "zzfCode" + sequences.getYear();
sequences.setKey(key);
sequences.setType("MISSION");
System.out.println(sequences);
}
} }