Compare commits
9 Commits
1.0.0
...
f6750280d8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6750280d8 | ||
|
|
0fa5cc1e7e | ||
|
|
54e2f9dffb | ||
|
|
1a9a158126 | ||
|
|
81c9babb40 | ||
|
|
c77088a7f5 | ||
| c6d828bf0f | |||
|
|
8046474df7 | ||
|
|
e45e75c644 |
148
README.md
148
README.md
@@ -8,17 +8,17 @@
|
||||
|
||||
使用方法:
|
||||
|
||||
+ 在项目中放置jar包的地方把seq-1.0.0.jar、seq-1.0.0-sources.jar、seq-1.0.0-pom.xml复制过去
|
||||
+ 在项目中放置jar包的地方把seq-1.2.0.jar、seq-1.2.0-sources.jar、seq-1.2.0-pom.xml复制过去
|
||||
+ 在pom.xml中增加以下内容,然后执行maven命令:mvn clean
|
||||
|
||||
```xml
|
||||
|
||||
<build>
|
||||
<project>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.cdhncy</groupId>
|
||||
<groupId>com.yanghuanglin</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.2.0</version>
|
||||
<exclusions>
|
||||
<!-- 如若你项目中有引用spring-jdbc,则需要排除seq的jdbc依赖 -->
|
||||
<exclusion>
|
||||
@@ -33,46 +33,50 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- 每次执行mvn clean时,自动安装指定的jar包 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install-external</id>
|
||||
<phase>clean</phase>
|
||||
<goals>
|
||||
<goal>install-file</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<!-- ${project.basedir}表示当前项目的根目录 -->
|
||||
<file>${project.basedir}/lib/seq-1.0.0.jar</file>
|
||||
<pomFile>${pom.basedir}/lib/seq-1.0.0-pom.xml</pomFile>
|
||||
<sources>${project.basedir}/lib/seq-1.0.0-sources.jar</sources>
|
||||
<repositoryLayout>default</repositoryLayout>
|
||||
<groupId>com.cdhncy</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<generatePom>true</generatePom>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</build>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- 每次执行mvn clean时,自动安装指定的jar包 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install-external</id>
|
||||
<phase>clean</phase>
|
||||
<goals>
|
||||
<goal>install-file</goal>
|
||||
</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>
|
||||
<repositoryLayout>default</repositoryLayout>
|
||||
<groupId>com.yanghuanglin</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.2.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<generatePom>true</generatePom>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
```
|
||||
|
||||
+ springboot中配置方式一(优先):直接注入已有jdbcTemplate和transactionTemplate
|
||||
|
||||
```java
|
||||
package com.yang.springseq.config;
|
||||
package com.yanghuanglin.springseq.config;
|
||||
|
||||
import com.cdhncy.seq.config.GeneratorConfig;
|
||||
import com.cdhncy.seq.config.TableConfig;
|
||||
import com.cdhncy.seq.generator.Generator;
|
||||
import com.cdhncy.seq.generator.impl.SequencesGenerator;
|
||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||
import com.yanghuanglin.seq.config.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;
|
||||
@@ -138,16 +142,14 @@ public class SeqGeneratorConfig {
|
||||
+ springboot中配置方式二:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
||||
|
||||
```java
|
||||
package com.yang.springseq.config;
|
||||
package com.yanghuanglin.springseq.config;
|
||||
|
||||
import com.cdhncy.seq.config.GeneratorConfig;
|
||||
import com.cdhncy.seq.config.TableConfig;
|
||||
import com.cdhncy.seq.generator.Generator;
|
||||
import com.cdhncy.seq.generator.impl.SequencesGenerator;
|
||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||
import com.yanghuanglin.seq.config.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;
|
||||
import javax.sql.DataSource;
|
||||
@@ -203,10 +205,10 @@ public class SeqGeneratorConfig {
|
||||
+ 使用:
|
||||
|
||||
```java
|
||||
package com.yang.springseq.config;
|
||||
package com.yanghuanglin.springseq.config;
|
||||
|
||||
import com.cdhncy.seq.generator.Generator;
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.yanghuanglin.seq.generator.Generator;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -277,7 +279,7 @@ GeneratorConfig配置项,通过set方法设置
|
||||
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
|
||||
| autoCreate | Boolean | true | 开启自动建表 |
|
||||
| step | Integer | 1 | 序号增加时的步长 |
|
||||
| tableConfig | com.cdhncy.seq.config.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||
| tableConfig | com.yanghuanglin.seq.config.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||
|
||||
以上配置中,jdbcTemplate和transactionTemplate优先级最高,如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置,则dataSource和transactionManager无效;
|
||||
进行这几种组合:dataSource+autoCreate+step+tableConfig,jdbcTemplate+transactionTemplate+autoCreate+step+tableConfig,jdbcTemplate+transactionManager+autoCreate+step+tableConfig
|
||||
@@ -286,11 +288,11 @@ GeneratorConfig配置项,通过set方法设置
|
||||
Generator方法如下:
|
||||
|
||||
```java
|
||||
package com.cdhncy.seq.generator;
|
||||
package com.yanghuanglin.seq.generator;
|
||||
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.cdhncy.seq.po.SequencesUnlock;
|
||||
import com.cdhncy.seq.po.SequencesUnused;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -338,6 +340,16 @@ public interface Generator {
|
||||
*/
|
||||
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/>
|
||||
@@ -350,7 +362,7 @@ public interface Generator {
|
||||
* @param seq 需要格式化的序号
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 格式
|
||||
* @return 格式化后的字符串
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, Integer minLength, String pattern);
|
||||
|
||||
@@ -366,11 +378,28 @@ public interface Generator {
|
||||
* @param seq 需要格式化的序号
|
||||
* @param start 序号格式化后以什么字符串开头
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 格式
|
||||
* @return 格式化后的字符串
|
||||
* @param pattern 序号格式
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String start, Integer minLength, String pattern);
|
||||
|
||||
/**
|
||||
* 将已格式化的序号解析为序号对象
|
||||
* <p/>
|
||||
* 返回的序号对象{@link Sequences#getKey()}和{@link Sequences#getType()}为null,但是临时字段{@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#号 对应 处〔2022〕0210801第10001号,而不能对应 处〔2022〕021110801第10001号
|
||||
*
|
||||
* @param formatted 格式化后的序号字符串
|
||||
* @param pattern 序号格式
|
||||
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key、type需要根据情况手动设置
|
||||
*/
|
||||
Sequences parse(String formatted, String pattern);
|
||||
|
||||
/**
|
||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||
* <p/>
|
||||
@@ -399,6 +428,13 @@ public interface Generator {
|
||||
* @param 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);
|
||||
}
|
||||
|
||||
```
|
||||
5
pom.xml
5
pom.xml
@@ -2,9 +2,9 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.cdhncy</groupId>
|
||||
<groupId>com.yanghuanglin</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.2.0</version>
|
||||
<name>seq</name>
|
||||
<description>seq</description>
|
||||
<properties>
|
||||
@@ -34,6 +34,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
package com.cdhncy.seq.po;
|
||||
|
||||
|
||||
/**
|
||||
* 当前序号
|
||||
*
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
public class Sequences {
|
||||
/**
|
||||
* 需要生成序号的key,和type组成唯一主键
|
||||
*/
|
||||
protected String key;
|
||||
|
||||
/**
|
||||
* 需要生成序号的类型,和key组成唯一主键
|
||||
*/
|
||||
protected String type;
|
||||
|
||||
/**
|
||||
* 默认序号
|
||||
*/
|
||||
protected Long seq = 0L;
|
||||
|
||||
public Sequences() {
|
||||
}
|
||||
|
||||
public Sequences(Long seq) {
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
public Sequences(String key, String type) {
|
||||
this.key = key;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Sequences(SequencesUnused sequencesUnused) {
|
||||
this.key = sequencesUnused.getKey();
|
||||
this.type = sequencesUnused.getType();
|
||||
this.seq = sequencesUnused.getSeq();
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Long getSeq() {
|
||||
return seq;
|
||||
}
|
||||
|
||||
public void setSeq(Long seq) {
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将序号增加指定步长
|
||||
*
|
||||
* @param step 步长
|
||||
*/
|
||||
public void increase(Integer step) {
|
||||
this.seq += step;
|
||||
}
|
||||
|
||||
/**
|
||||
* 序号补零
|
||||
*
|
||||
* @param minLength 最小长度,低于此长度,会填充零
|
||||
* @return 补零后的序号
|
||||
*/
|
||||
public String format(Integer minLength) {
|
||||
if (minLength != null)
|
||||
return String.format("%0" + minLength + "d", this.seq);
|
||||
return String.valueOf(this.seq);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cdhncy.seq.config;
|
||||
package com.yanghuanglin.seq.config;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cdhncy.seq.config;
|
||||
package com.yanghuanglin.seq.config;
|
||||
|
||||
/**
|
||||
* 生成器对应的数据库表和字段配置
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.cdhncy.seq.dao;
|
||||
package com.yanghuanglin.seq.dao;
|
||||
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
|
||||
/**
|
||||
* @author yanghuanglin
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.cdhncy.seq.dao;
|
||||
package com.yanghuanglin.seq.dao;
|
||||
|
||||
import com.cdhncy.seq.po.SequencesUnlock;
|
||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.cdhncy.seq.dao;
|
||||
package com.yanghuanglin.seq.dao;
|
||||
|
||||
import com.cdhncy.seq.po.SequencesUnused;
|
||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -25,6 +25,11 @@ public interface SequencesUnusedDao {
|
||||
*/
|
||||
boolean delete(SequencesUnused sequencesUnused);
|
||||
|
||||
/**
|
||||
* 保存空闲序号
|
||||
*/
|
||||
boolean save(SequencesUnused sequencesUnused);
|
||||
|
||||
/**
|
||||
* 批量保存空闲序号
|
||||
*/
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.cdhncy.seq.dao.impl;
|
||||
package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.cdhncy.seq.config.TableConfig;
|
||||
import com.cdhncy.seq.dao.SequencesDao;
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesDao;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
@SuppressWarnings("SqlResolve")
|
||||
public class SequencesDaoImpl implements SequencesDao {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final TableConfig tableConfig;
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.cdhncy.seq.dao.impl;
|
||||
package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.cdhncy.seq.config.TableConfig;
|
||||
import com.cdhncy.seq.dao.SequencesUnlockDao;
|
||||
import com.cdhncy.seq.po.SequencesUnlock;
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesUnlockDao;
|
||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.util.List;
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
@SuppressWarnings("SqlResolve")
|
||||
public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final TableConfig tableConfig;
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.cdhncy.seq.dao.impl;
|
||||
package com.yanghuanglin.seq.dao.impl;
|
||||
|
||||
import com.cdhncy.seq.config.TableConfig;
|
||||
import com.cdhncy.seq.dao.SequencesUnusedDao;
|
||||
import com.cdhncy.seq.po.SequencesUnused;
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesUnusedDao;
|
||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -16,6 +16,7 @@ import java.util.List;
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
@SuppressWarnings("SqlResolve")
|
||||
public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final TableConfig tableConfig;
|
||||
@@ -55,6 +56,14 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean save(SequencesUnused sequencesUnused) {
|
||||
String sql = "insert into `%s_ununsed`(`%s`,`%s`,`%s`) values(?,?,?)";
|
||||
sql = String.format(sql, tableConfig.getTable(), tableConfig.getKeyColumn(), tableConfig.getTypeColumn(), tableConfig.getSeqColumn());
|
||||
int result = this.jdbcTemplate.update(sql, sequencesUnused.getKey(), sequencesUnused.getType(), sequencesUnused.getSeq());
|
||||
return result != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveBatch(List<SequencesUnused> sequencesUnusedList) {
|
||||
String sql = "insert into `%s_ununsed`(`%s`,`%s`,`%s`) values(?,?,?)";
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.cdhncy.seq.generator;
|
||||
package com.yanghuanglin.seq.generator;
|
||||
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.cdhncy.seq.po.SequencesUnlock;
|
||||
import com.cdhncy.seq.po.SequencesUnused;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -50,6 +50,16 @@ public interface Generator {
|
||||
*/
|
||||
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/>
|
||||
@@ -62,7 +72,7 @@ public interface Generator {
|
||||
* @param seq 需要格式化的序号
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 格式
|
||||
* @return 格式化后的字符串
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, Integer minLength, String pattern);
|
||||
|
||||
@@ -78,11 +88,28 @@ public interface Generator {
|
||||
* @param seq 需要格式化的序号
|
||||
* @param start 序号格式化后以什么字符串开头
|
||||
* @param minLength 序号最小长度,不足的会补零
|
||||
* @param pattern 格式
|
||||
* @return 格式化后的字符串
|
||||
* @param pattern 序号格式
|
||||
* @return 格式化后的序号字符串
|
||||
*/
|
||||
String format(Long seq, String start, Integer minLength, String pattern);
|
||||
|
||||
/**
|
||||
* 将已格式化的序号解析为序号对象
|
||||
* <p/>
|
||||
* 返回的序号对象{@link Sequences#getKey()}和{@link Sequences#getType()}为null,但是临时字段{@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#号 对应 处〔2022〕0210801第10001号,而不能对应 处〔2022〕021110801第10001号
|
||||
*
|
||||
* @param formatted 格式化后的序号字符串
|
||||
* @param pattern 序号格式
|
||||
* @return 包含了序号字符串对应年(如果有)、月(如果有)、日(如果有)、序号的序号对象,其key、type需要根据情况手动设置
|
||||
*/
|
||||
Sequences parse(String formatted, String pattern);
|
||||
|
||||
/**
|
||||
* 锁定指定序号,在序号生成后,调用该序号的逻辑完成后需要执行此方法
|
||||
* <p/>
|
||||
@@ -111,4 +138,11 @@ public interface Generator {
|
||||
* @param 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);
|
||||
}
|
||||
@@ -1,23 +1,29 @@
|
||||
package com.cdhncy.seq.generator.impl;
|
||||
package com.yanghuanglin.seq.generator.impl;
|
||||
|
||||
import com.cdhncy.seq.config.GeneratorConfig;
|
||||
import com.cdhncy.seq.dao.SequencesDao;
|
||||
import com.cdhncy.seq.dao.SequencesUnusedDao;
|
||||
import com.cdhncy.seq.dao.SequencesUnlockDao;
|
||||
import com.cdhncy.seq.dao.impl.SequencesDaoImpl;
|
||||
import com.cdhncy.seq.dao.impl.SequencesUnusedDaoImpl;
|
||||
import com.cdhncy.seq.dao.impl.SequencesUnlockDaoImpl;
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.cdhncy.seq.po.SequencesUnused;
|
||||
import com.cdhncy.seq.po.SequencesUnlock;
|
||||
import com.cdhncy.seq.generator.Generator;
|
||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||
import com.yanghuanglin.seq.dao.SequencesDao;
|
||||
import com.yanghuanglin.seq.dao.SequencesUnlockDao;
|
||||
import com.yanghuanglin.seq.dao.SequencesUnusedDao;
|
||||
import com.yanghuanglin.seq.dao.impl.SequencesDaoImpl;
|
||||
import com.yanghuanglin.seq.dao.impl.SequencesUnlockDaoImpl;
|
||||
import com.yanghuanglin.seq.dao.impl.SequencesUnusedDaoImpl;
|
||||
import com.yanghuanglin.seq.generator.Generator;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import com.yanghuanglin.seq.po.SequencesUnlock;
|
||||
import com.yanghuanglin.seq.po.SequencesUnused;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SequencesGenerator implements Generator {
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
@@ -129,6 +135,11 @@ public class SequencesGenerator implements Generator {
|
||||
return sequences.format(minLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(Sequences sequences, Integer minLength, String pattern) {
|
||||
return format(sequences.getSeq(), minLength, pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String format(Long seq, Integer minLength, String pattern) {
|
||||
return format(seq, null, minLength, pattern);
|
||||
@@ -147,6 +158,80 @@ public class SequencesGenerator implements Generator {
|
||||
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);
|
||||
|
||||
return sequences;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean lock(Sequences sequences) {
|
||||
SequencesUnlock condition = new SequencesUnlock(sequences);
|
||||
@@ -185,4 +270,10 @@ public class SequencesGenerator implements Generator {
|
||||
//删除指定时间段内使用中表的数据
|
||||
sequencesUnlockDao.deleteByDate(begin, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void release(Sequences sequences) {
|
||||
sequencesUnlockDao.delete(new SequencesUnlock(sequences));
|
||||
sequencesUnusedDao.save(new SequencesUnused(sequences));
|
||||
}
|
||||
}
|
||||
148
src/main/java/com/yanghuanglin/seq/po/Sequences.java
Normal file
148
src/main/java/com/yanghuanglin/seq/po/Sequences.java
Normal file
@@ -0,0 +1,148 @@
|
||||
package com.yanghuanglin.seq.po;
|
||||
|
||||
|
||||
/**
|
||||
* 当前序号
|
||||
*
|
||||
* @author yanghuanglin
|
||||
* @since 2022/1/28
|
||||
*/
|
||||
public class Sequences {
|
||||
/**
|
||||
* 需要生成序号的key,和type组成唯一主键
|
||||
*/
|
||||
protected String key;
|
||||
|
||||
/**
|
||||
* 需要生成序号的类型,和key组成唯一主键
|
||||
*/
|
||||
protected String type;
|
||||
|
||||
/**
|
||||
* 默认序号
|
||||
*/
|
||||
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(Long seq) {
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
public Sequences(String key, String type) {
|
||||
this.key = key;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Sequences(String key, String type, Long seq) {
|
||||
this.key = key;
|
||||
this.type = type;
|
||||
this.seq = seq;
|
||||
}
|
||||
|
||||
public Sequences(SequencesUnused sequencesUnused) {
|
||||
this.key = sequencesUnused.getKey();
|
||||
this.type = sequencesUnused.getType();
|
||||
this.seq = sequencesUnused.getSeq();
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Long getSeq() {
|
||||
return seq;
|
||||
}
|
||||
|
||||
public void setSeq(Long 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将序号增加指定步长
|
||||
*
|
||||
* @param step 步长
|
||||
*/
|
||||
public void increase(Integer step) {
|
||||
this.seq += step;
|
||||
}
|
||||
|
||||
/**
|
||||
* 序号补零
|
||||
*
|
||||
* @param minLength 最小长度,低于此长度,会填充零
|
||||
* @return 补零后的序号
|
||||
*/
|
||||
public String format(Integer minLength) {
|
||||
if (minLength != null)
|
||||
return String.format("%0" + minLength + "d", 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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cdhncy.seq.po;
|
||||
package com.yanghuanglin.seq.po;
|
||||
|
||||
|
||||
import java.util.Date;
|
||||
@@ -34,4 +34,14 @@ public class SequencesUnlock extends Sequences {
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SequencesUnlock{" +
|
||||
"key='" + key + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", seq=" + seq +
|
||||
", createTime=" + createTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.cdhncy.seq.po;
|
||||
package com.yanghuanglin.seq.po;
|
||||
|
||||
|
||||
/**
|
||||
@@ -16,4 +16,13 @@ public class SequencesUnused extends Sequences {
|
||||
this.type = sequences.getType();
|
||||
this.seq = sequences.getSeq();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SequencesUnused{" +
|
||||
"key='" + key + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
", seq=" + seq +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import com.cdhncy.seq.config.GeneratorConfig;
|
||||
import com.cdhncy.seq.config.TableConfig;
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import com.cdhncy.seq.generator.Generator;
|
||||
import com.cdhncy.seq.generator.impl.SequencesGenerator;
|
||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||
import com.yanghuanglin.seq.config.GeneratorConfig;
|
||||
import com.yanghuanglin.seq.config.TableConfig;
|
||||
import com.yanghuanglin.seq.generator.Generator;
|
||||
import com.yanghuanglin.seq.generator.impl.SequencesGenerator;
|
||||
import com.yanghuanglin.seq.po.Sequences;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
@@ -73,4 +73,18 @@ public class SeqTest {
|
||||
String s = "select * from sequences where `%s`=? and `%s`=?";
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user