6 Commits
1.5.1 ... 1.6.2

Author SHA1 Message Date
yhl452493373
f039ed30c9 增加releaseBefore、releaseAfter、clearBefore、clearAfter方法 2022-02-21 10:26:29 +08:00
yhl452493373
f02fdf5a53 更新README 2022-02-16 17:38:36 +08:00
yhl452493373
464b99a09b 调整创建表时key、type、seq的长度,分别为64,64,20,避免出现“Specified key was too long; max key length is 767 bytes”问题 2022-02-16 17:36:50 +08:00
yhl452493373
6a5a8111ee 变更测试使用的字段名 2022-02-16 10:47:57 +08:00
yhl452493373
1accb4b066 变更测试使用的字段名 2022-02-16 10:41:22 +08:00
yhl452493373
c865be0918 更新README 2022-02-15 13:37:54 +08:00
9 changed files with 148 additions and 34 deletions

View File

@@ -8,7 +8,7 @@
使用方法:
+ 在项目中放置jar包的地方把seq-1.5.1.jar、seq-1.5.1-sources.jar、seq-1.5.1-pom.xml复制过去
+ 在项目中放置jar包的地方把seq-1.6.2.jar、seq-1.6.2-sources.jar、seq-1.6.2-pom.xml复制过去
+ 在pom.xml中增加以下内容然后执行maven命令mvn clean
```xml
@@ -18,7 +18,7 @@
<dependency>
<groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId>
<version>1.5.1</version>
<version>1.6.2</version>
<exclusions>
<!-- 如若你项目中有引用spring-jdbc则需要排除seq的jdbc依赖 -->
<exclusion>
@@ -50,13 +50,13 @@
</goals>
<configuration>
<!-- ${project.basedir}表示当前项目的根目录 -->
<file>${project.basedir}/lib/seq-1.5.1.jar</file>
<pomFile>${pom.basedir}/lib/seq-1.5.1-pom.xml</pomFile>
<sources>${project.basedir}/lib/seq-1.5.1-sources.jar</sources>
<file>${project.basedir}/lib/seq-1.6.2.jar</file>
<pomFile>${project.basedir}/lib/seq-1.6.2-pom.xml</pomFile>
<sources>${project.basedir}/lib/seq-1.6.2-sources.jar</sources>
<repositoryLayout>default</repositoryLayout>
<groupId>com.yanghuanglin</groupId>
<artifactId>seq</artifactId>
<version>1.5.1</version>
<version>1.6.2</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
@@ -478,6 +478,20 @@ public interface Generator {
*/
void release(Date begin, Date end);
/**
* 释放从开始时间开始,到现在时间之间未使用的序号。结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void releaseAfter(Date begin);
/**
* 释放结束时间以前的序号
*
* @param end 结束时间
*/
void releaseBefore(Date end);
/**
* 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
*
@@ -494,5 +508,20 @@ public interface Generator {
* 清空指定时间段内闲置序号和未锁定序号
*/
void clear(Date begin, Date end);
/**
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void clearAfter(Date begin);
/**
* 清空结束时间之前的限制序号和未锁定序号
*
* @param end 结束时间
*/
void clearBefore(Date end);
}
```

View File

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

View File

@@ -57,9 +57,9 @@ public class SequencesDaoImpl implements SequencesDao {
@Override
public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS `%s` ( " +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 2 ) NOT NULL COMMENT '已使用到的序号'," +
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 20 ) NOT NULL COMMENT '已使用到的序号'," +
" PRIMARY KEY ( `%s`, `%s` ) " +
" ) COMMENT '当前序号表'";
sql = String.format(sql, tableConfig.getTable(),

View File

@@ -48,9 +48,22 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
@Override
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());
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
@@ -63,18 +76,30 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
@Override
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());
int result = this.jdbcTemplate.update(sql, begin, end);
return result != 0;
return this.jdbcTemplate.update(sql, begin, end) != 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
public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS `%s_unlock` ( " +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 2 ) NOT NULL COMMENT '尚未锁定的序号'," +
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 20 ) NOT NULL COMMENT '尚未锁定的序号'," +
" `%s` DATETIME NOT NULL COMMENT '使用时间'," +
" PRIMARY KEY ( `%s`, `%s` ,`%s` ) " +
" ) COMMENT '未锁定序号表'";

View File

@@ -91,9 +91,9 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override
public void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS `%s_unused` ( " +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 255 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 2 ) NOT NULL COMMENT '闲置的的序号'," +
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号英文名称'," +
" `%s` VARCHAR ( 64 ) NOT NULL COMMENT '序号类型'," +
" `%s` BIGINT ( 20 ) NOT NULL COMMENT '闲置的的序号'," +
" `%s` DATETIME NOT NULL COMMENT '设为闲置序号的时间'," +
" PRIMARY KEY ( `%s`, `%s`, `%s` ) " +
" ) COMMENT '闲置序号表'";
@@ -118,10 +118,22 @@ public class SequencesUnusedDaoImpl implements SequencesUnusedDao {
@Override
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());
int result = this.jdbcTemplate.update(sql, begin, end);
return result != 0;
return this.jdbcTemplate.update(sql, begin, end) != 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() {

View File

@@ -184,6 +184,20 @@ public interface Generator {
*/
void release(Date begin, Date end);
/**
* 释放从开始时间开始,到现在时间之间未使用的序号。结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void releaseAfter(Date begin);
/**
* 释放结束时间以前的序号
*
* @param end 结束时间
*/
void releaseBefore(Date end);
/**
* 释放指定序号。一般用于业务对象删除后,对应序号需要回收使用时。
*
@@ -200,4 +214,18 @@ public interface Generator {
* 清空指定时间段内闲置序号和未锁定序号
*/
void clear(Date begin, Date end);
/**
* 清空从开始时间到限制时间之间闲置序号和未锁定序号,结束时间为方法执行时的时间
*
* @param begin 开始时间
*/
void clearAfter(Date begin);
/**
* 清空结束时间之前的限制序号和未锁定序号
*
* @param end 结束时间
*/
void clearBefore(Date end);
}

View File

@@ -50,7 +50,7 @@ public class SequencesGenerator implements Generator {
*
* @param autoCreate 是否自动创建
*/
private void createTable(Boolean autoCreate) {
private synchronized void createTable(Boolean autoCreate) {
if (!autoCreate)
return;
this.sequencesDao.createTable();
@@ -274,6 +274,16 @@ public class SequencesGenerator implements Generator {
sequencesUnlockDao.deleteByDate(begin, end);
}
@Override
public synchronized void releaseAfter(Date begin) {
release(begin, null);
}
@Override
public synchronized void releaseBefore(Date end) {
release(null, end);
}
@Override
public synchronized void release(Sequences sequences) {
if (sequences == null)
@@ -283,14 +293,24 @@ public class SequencesGenerator implements Generator {
}
@Override
public void clear() {
public synchronized void clear() {
sequencesUnlockDao.deleteAll();
sequencesUnusedDao.deleteAll();
}
@Override
public void clear(Date begin, Date end) {
public synchronized void clear(Date begin, Date end) {
sequencesUnlockDao.deleteByDate(begin, end);
sequencesUnusedDao.deleteByDate(begin, end);
}
@Override
public synchronized void clearAfter(Date begin) {
clear(begin, null);
}
@Override
public synchronized void clearBefore(Date end) {
clear(null, end);
}
}

View File

@@ -129,9 +129,9 @@ public class Sequences {
* @return 补零后的序号,若未单独设置序号的长度,则最小长度为{@link BaseConfig#getMinLength()}长度;否则为修改后的长度,不足部分补零
*/
public String format() {
BaseConfig baseConfig = BaseConfig.getInstance();
if (baseConfig.getMinLength() != null)
return String.format("%0" + baseConfig.getMinLength() + "d", this.seq);
Integer minLength = BaseConfig.getInstance().getMinLength();
if (minLength != null)
return String.format("%0" + minLength + "d", this.seq);
return String.valueOf(this.seq);
}

View File

@@ -30,7 +30,7 @@ public class SeqTest {
tableConfig.setTable("sequences");
tableConfig.setKeyColumn("SEQUENCE_KEY");
tableConfig.setTypeColumn("SEQUENCE_TYPE");
tableConfig.setSeqColumn("NEXT_ID");
tableConfig.setSeqColumn("CURRENT");
generatorConfig.setTableConfig(tableConfig);
generator = new SequencesGenerator(generatorConfig);