初次提交
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
# spring-seq
|
||||
# [seq](https://gitea.yanghuanglin.com/yhl452493373/seq) 使用示例
|
||||
|
||||
seq基于spring的使用示例
|
||||
### 具体查看com.yang.springseq.config,其中SeqGeneratorConfig为配置信息,Startup为项目启动后执行的代码
|
||||
|
||||
*项目新建时,执行下mvn clean来安装包到本地maven库,如果其他地方已经执行过这个操作,可以不用,而且pom中也可以直接引用,无需安装*
|
||||
25
lib/seq-1.0.0-pom.xml
Normal file
25
lib/seq-1.0.0-pom.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>seq</name>
|
||||
<description>seq</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>5.3.15</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.28</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
110
pom.xml
Normal file
110
pom.xml
Normal file
@@ -0,0 +1,110 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.yang</groupId>
|
||||
<artifactId>spring-seq</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-seq</name>
|
||||
<description>spring-seq</description>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.cdhncy</groupId>
|
||||
<artifactId>seq</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<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>
|
||||
<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>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
12
src/main/java/com/yang/springseq/SpringSeqApplication.java
Normal file
12
src/main/java/com/yang/springseq/SpringSeqApplication.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.yang.springseq;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringSeqApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringSeqApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.yang.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 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;
|
||||
|
||||
@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("SEQUENCE_NEXT_ID");
|
||||
return tableConfig;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public GeneratorConfig generatorConfig(TableConfig tableConfig) {
|
||||
GeneratorConfig generatorConfig = new GeneratorConfig();
|
||||
generatorConfig.setJdbcTemplate(jdbcTemplate);
|
||||
generatorConfig.setTransactionTemplate(transactionTemplate);
|
||||
generatorConfig.setTableConfig(tableConfig);
|
||||
return generatorConfig;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Generator generator(GeneratorConfig generatorConfig) {
|
||||
return new SequencesGenerator(generatorConfig);
|
||||
}
|
||||
}
|
||||
45
src/main/java/com/yang/springseq/config/Startup.java
Normal file
45
src/main/java/com/yang/springseq/config/Startup.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.yang.springseq.config;
|
||||
|
||||
import com.cdhncy.seq.generator.Generator;
|
||||
import com.cdhncy.seq.po.Sequences;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class Startup implements ApplicationRunner {
|
||||
@Resource
|
||||
private Generator generator;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
//释放未锁定序列号
|
||||
generator.release();
|
||||
|
||||
Set<String> set = new HashSet<>();
|
||||
//开启多线程进行测试
|
||||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(100));
|
||||
for (int i = 0; i < 5; i++) {
|
||||
threadPoolExecutor.execute(() -> {
|
||||
Sequences sequences = generator.generate("SNT", "MISSION");
|
||||
String formattedSeq = generator.format(sequences.getSeq(), 5, "处〔#year#〕10801#seq#");
|
||||
generator.lock(sequences);
|
||||
set.add(formattedSeq);
|
||||
System.out.println(formattedSeq);
|
||||
});
|
||||
}
|
||||
threadPoolExecutor.shutdown();
|
||||
while (true) {
|
||||
if (threadPoolExecutor.isTerminated())
|
||||
break;
|
||||
}
|
||||
System.out.println(set.size());
|
||||
}
|
||||
}
|
||||
6
src/main/resources/application.yml
Normal file
6
src/main/resources/application.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://127.0.0.1:3306/sequence
|
||||
username: root
|
||||
password: root
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.yang.springseq;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringSeqApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user