Merge remote-tracking branch 'gitea/master'
# Conflicts: # README.md
This commit is contained in:
86
README.md
86
README.md
@@ -4,6 +4,8 @@
|
||||
|
||||
用于生成全局自增序号,跳过的序号可以回收使用。
|
||||
|
||||
***本生成器内部使用transactionTemplate进行事务管理,如果你项目中给使用此生成器的方法或类加了`@Transactional`注解,则需将新建一个Service来专门负责调用此生成器的方法,同时要给新建的类增加`@Transactional`注解,并设置`propagation = Propagation.NOT_SUPPORTED`***
|
||||
|
||||
---
|
||||
|
||||
使用方法:
|
||||
@@ -68,78 +70,7 @@
|
||||
</project>
|
||||
```
|
||||
|
||||
+ springboot中配置方式一(优先):直接注入已有jdbcTemplate和transactionTemplate
|
||||
|
||||
```java
|
||||
package com.yanghuanglin.springseq.baseConfig;
|
||||
|
||||
import com.yanghuanglin.seq.baseConfig.GeneratorConfig;
|
||||
import com.yanghuanglin.seq.baseConfig.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;
|
||||
|
||||
/**
|
||||
* 基于已有的jdbcTemplate和transactionTemplate,一般如果引用了spring的数据库操作,如jpa、mybatis,都可以直接注入
|
||||
*/
|
||||
@Configuration
|
||||
public class SeqGeneratorConfig {
|
||||
/**
|
||||
* 注入已有的数据库操作模板
|
||||
*/
|
||||
@Resource
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
/**
|
||||
* 注入已有的事务操作模板
|
||||
*/
|
||||
@Resource
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
/**
|
||||
* 序号表配置类
|
||||
*/
|
||||
@Bean
|
||||
public TableConfig tableConfig() {
|
||||
TableConfig tableConfig = new TableConfig();
|
||||
//自定义表名、字段名
|
||||
tableConfig.setTable("sequences");
|
||||
tableConfig.setKeyColumn("SEQUENCE_KEY");
|
||||
tableConfig.setTypeColumn("SEQUENCE_TYPE");
|
||||
tableConfig.setSeqColumn("NEXT_ID");
|
||||
tableConfig.setCreateTimeColumn("CREATE_TIME");
|
||||
return tableConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 序号生成器配置类
|
||||
* @param tableConfig 序号表配置类
|
||||
*/
|
||||
@Bean
|
||||
public GeneratorConfig generatorConfig(TableConfig tableConfig) {
|
||||
GeneratorConfig generatorConfig = new GeneratorConfig();
|
||||
generatorConfig.setJdbcTemplate(jdbcTemplate);
|
||||
generatorConfig.setTransactionTemplate(transactionTemplate);
|
||||
generatorConfig.setTableConfig(tableConfig);
|
||||
return generatorConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册序号生成器类
|
||||
* @param generatorConfig 序号生成器配置类
|
||||
*/
|
||||
@Bean
|
||||
public Generator generator(GeneratorConfig generatorConfig) {
|
||||
return new SequencesGenerator(generatorConfig);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
+ springboot中配置方式二:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
||||
+ springboot中配置方式:注入已有的dataSource或自行构建dataSource,通过dataSource自动生成jdbcTemplate和transactionTemplate
|
||||
|
||||
```java
|
||||
package com.yanghuanglin.springseq.baseConfig;
|
||||
@@ -160,7 +91,7 @@ import javax.sql.DataSource;
|
||||
@Configuration
|
||||
public class SeqGeneratorConfig {
|
||||
/**
|
||||
* 注入已有的数据源,果没有,也可以自行构建
|
||||
* 注入已有的数据源,如果没有,也可以自行构建
|
||||
*/
|
||||
@Resource
|
||||
private DataSource dataSource;
|
||||
@@ -276,18 +207,14 @@ GeneratorConfig配置项,通过set方法设置
|
||||
| 配置项 | 类型 | 默认值 | 说明 |
|
||||
|---------------------|------------------------------------------------------------------|------------------|------------------|
|
||||
| dataSource | javax.sql.DataSource | null | 数据源 |
|
||||
| jdbcTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 数据库操作模板 |
|
||||
| transactionTemplate | org.springframework.jdbc.core.JdbcTemplate | null | 事务操作模板 |
|
||||
| transactionManager | org.springframework.jdbc.datasource.DataSourceTransactionManager | null | 事务管理器 |
|
||||
| monthZeroFilling | Boolean | true | 月份不足2位时自动补零 |
|
||||
| dayZeroFilling | Boolean | true | 当前日期不足2位时自动补零 |
|
||||
| autoCreate | Boolean | true | 开启自动建表 |
|
||||
| step | Integer | 1 | 序号增加时的步长 |
|
||||
| type | String | DEFAULT | 默认序号类型 |
|
||||
| minLength | Integer | 1 | 默认序号格式化后长度,不足的补零 |
|
||||
| tableConfig | com.yanghuanglin.seq.baseConfig.TableConfig | TableConfig的默认配置 | 表配置 |
|
||||
|
||||
以上配置中,jdbcTemplate和transactionTemplate优先级最高,如果jdbcTemplate、transactionTemplate、dataSource、transactionManager同时配置,则dataSource和transactionManager无效;
|
||||
可进行这几种组合:dataSource+autoCreate+step+minLength+tableConfig,jdbcTemplate+transactionTemplate+autoCreate+step+minLength+tableConfig,jdbcTemplate+transactionManager+autoCreate+step+minLength+tableConfig
|
||||
|
||||
---
|
||||
Generator方法如下:
|
||||
|
||||
@@ -590,4 +517,5 @@ public interface Generator {
|
||||
*/
|
||||
void clearBefore(Date end);
|
||||
}
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user