增加releaseBefore、releaseAfter、clearBefore、clearAfter方法

This commit is contained in:
yhl452493373
2022-02-21 10:26:29 +08:00
parent f02fdf5a53
commit f039ed30c9
6 changed files with 135 additions and 21 deletions

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`<=?";
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
return this.jdbcTemplate.query(sql, rowMapper(), begin, end);
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,10 +76,22 @@ public class SequencesUnlockDaoImpl implements SequencesUnlockDao {
@Override
public boolean deleteByDate(Date begin, Date end) {
String 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;
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());
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

View File

@@ -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`<=?";
sql = String.format(sql, tableConfig.getTable(), tableConfig.getCreateTimeColumn(), tableConfig.getCreateTimeColumn());
int result = this.jdbcTemplate.update(sql, begin, end);
return result != 0;
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());
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);
}
}