Compare commits
3 Commits
1.8.2
...
32d3de7550
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32d3de7550 | ||
|
|
bb2ddfc34a | ||
| 3615ed9eec |
34
README.md
34
README.md
@@ -4,11 +4,13 @@
|
|||||||
|
|
||||||
用于生成全局自增序号,跳过的序号可以回收使用。
|
用于生成全局自增序号,跳过的序号可以回收使用。
|
||||||
|
|
||||||
|
***本生成器内部使用transactionTemplate进行事务管理,如果你项目中给使用此生成器的方法或类加了`@Transactional`注解,则需将新建一个Service来专门负责调用此生成器的方法,同时要给新建的类增加`@Transactional`注解,并设置`propagation = Propagation.NOT_SUPPORTED`***
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
使用方法:
|
使用方法:
|
||||||
|
|
||||||
+ 在项目中放置jar包的地方把seq-1.8.2.jar、seq-1.8.2-sources.jar、seq-1.8.2-pom.xml复制过去
|
+ 在项目中放置jar包的地方把seq-1.9.2.jar、seq-1.9.2-sources.jar、seq-1.9.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.8.2</version>
|
<version>1.9.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.8.2.jar</file>
|
<file>${project.basedir}/lib/seq-1.9.2.jar</file>
|
||||||
<pomFile>${project.basedir}/lib/seq-1.8.2-pom.xml</pomFile>
|
<pomFile>${project.basedir}/lib/seq-1.9.2-pom.xml</pomFile>
|
||||||
<sources>${project.basedir}/lib/seq-1.8.2-sources.jar</sources>
|
<sources>${project.basedir}/lib/seq-1.9.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.8.2</version>
|
<version>1.9.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<generatePom>true</generatePom>
|
<generatePom>true</generatePom>
|
||||||
</configuration>
|
</configuration>
|
||||||
@@ -381,6 +383,26 @@ public interface Generator {
|
|||||||
*/
|
*/
|
||||||
String format(Long seq, String start, Integer minLength, String pattern);
|
String format(Long seq, String start, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将生成的序号对象格式化为指定格式
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
* <p/>
|
||||||
|
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||||
|
* <p/>
|
||||||
|
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为4位,不足4位则补零]
|
||||||
|
*
|
||||||
|
* @param seq 需要格式化的序号
|
||||||
|
* @param start 序号格式化后以什么字符串开头
|
||||||
|
* @param minLength 序号最小长度,不足的会补零
|
||||||
|
* @param pattern 序号格式
|
||||||
|
* @param year 格式化时使用的年
|
||||||
|
* @param month 格式化时使用的月
|
||||||
|
* @param day 格式化时使用的日
|
||||||
|
* @return 格式化后的序号字符串
|
||||||
|
*/
|
||||||
|
String format(Long seq, String start, Integer minLength, String pattern, Integer year, Integer month, Integer day);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将已格式化的序号解析为序号对象
|
* 将已格式化的序号解析为序号对象
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|||||||
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.8.2</version>
|
<version>1.9.2</version>
|
||||||
<name>seq</name>
|
<name>seq</name>
|
||||||
<description>seq</description>
|
<description>seq</description>
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -162,6 +162,26 @@ public interface Generator {
|
|||||||
*/
|
*/
|
||||||
String format(Long seq, String start, Integer minLength, String pattern);
|
String format(Long seq, String start, Integer minLength, String pattern);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将生成的序号对象格式化为指定格式
|
||||||
|
* <p/>
|
||||||
|
* pattern支持:{@link FormatPlaceholder#YEAR}(当前年份)、{@link FormatPlaceholder#MONTH}}(当前月份)、{@link FormatPlaceholder#DAY}}(当前日期)、{@link FormatPlaceholder#SEQ}}(生成的字符串序号)几个枚举值通过{@link FormatPlaceholder#getPlaceholder()}得到的字符串
|
||||||
|
* <p/>
|
||||||
|
* seq为1,start为6,minLength为4,pattern为#year##month##day##seq#,则会格式化为2022013060001。此序号含义如下:
|
||||||
|
* <p/>
|
||||||
|
* 序号格式:[年][月][日][固定6开头][序号1,最小位数为4位,不足4位则补零]
|
||||||
|
*
|
||||||
|
* @param seq 需要格式化的序号
|
||||||
|
* @param start 序号格式化后以什么字符串开头
|
||||||
|
* @param minLength 序号最小长度,不足的会补零
|
||||||
|
* @param pattern 序号格式
|
||||||
|
* @param year 格式化时使用的年
|
||||||
|
* @param month 格式化时使用的月
|
||||||
|
* @param day 格式化时使用的日
|
||||||
|
* @return 格式化后的序号字符串
|
||||||
|
*/
|
||||||
|
String format(Long seq, String start, Integer minLength, String pattern, Integer year, Integer month, Integer day);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将已格式化的序号解析为序号对象
|
* 将已格式化的序号解析为序号对象
|
||||||
* <p/>
|
* <p/>
|
||||||
|
|||||||
@@ -176,13 +176,21 @@ public class SequencesGenerator implements Generator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(Long seq, String start, Integer minLength, String pattern) {
|
public String format(Long seq, String start, Integer minLength, String pattern) {
|
||||||
|
return format(seq, start, minLength, pattern, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String format(Long seq, String start, Integer minLength, String pattern, Integer year, Integer month, Integer day) {
|
||||||
if (start == null)
|
if (start == null)
|
||||||
start = "";
|
start = "";
|
||||||
String seqString = start + new Sequences(seq).format(minLength);
|
String seqString = start + new Sequences(seq).format(minLength);
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
int year = calendar.get(Calendar.YEAR);
|
if (year == null)
|
||||||
int month = calendar.get(Calendar.MONTH) + 1;
|
year = calendar.get(Calendar.YEAR);
|
||||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
if (month == null)
|
||||||
|
month = calendar.get(Calendar.MONTH) + 1;
|
||||||
|
if (day == null)
|
||||||
|
day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
pattern = pattern.replaceAll(YEAR.getPlaceholder(), String.valueOf(year));
|
pattern = pattern.replaceAll(YEAR.getPlaceholder(), String.valueOf(year));
|
||||||
pattern = pattern.replaceAll(MONTH.getPlaceholder(), monthZeroFilling ? String.format("%02d", month) : String.valueOf(month));
|
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(DAY.getPlaceholder(), dayZeroFilling ? String.format("%02d", day) : String.valueOf(day));
|
||||||
|
|||||||
Reference in New Issue
Block a user