Skip to content

Commit 7e92e6d

Browse files
committed
add id generator
1 parent 9c45f00 commit 7e92e6d

File tree

11 files changed

+291
-0
lines changed

11 files changed

+291
-0
lines changed

pom.xml

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@
143143
<version>${codingapi.framework.version}</version>
144144
</dependency>
145145

146+
<dependency>
147+
<groupId>com.codingapi.springboot</groupId>
148+
<artifactId>springboot-starter-id-generator</artifactId>
149+
<version>${codingapi.framework.version}</version>
150+
</dependency>
151+
146152
<dependency>
147153
<groupId>com.codingapi.springboot</groupId>
148154
<artifactId>springboot-starter-data-fast</artifactId>
@@ -218,6 +224,7 @@
218224
<module>springboot-starter-security-jwt</module>
219225
<module>springboot-starter-leaf</module>
220226
<module>springboot-starter-data-fast</module>
227+
<module>springboot-starter-id-generator</module>
221228
</modules>
222229
</profile>
223230

@@ -231,6 +238,7 @@
231238
<module>springboot-starter-security-jwt</module>
232239
<module>springboot-starter-leaf</module>
233240
<module>springboot-starter-data-fast</module>
241+
<module>springboot-starter-id-generator</module>
234242
</modules>
235243

236244
<build>
@@ -278,6 +286,7 @@
278286
<module>springboot-starter-security-jwt</module>
279287
<module>springboot-starter-leaf</module>
280288
<module>springboot-starter-data-fast</module>
289+
<module>springboot-starter-id-generator</module>
281290
</modules>
282291

283292

springboot-example/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<artifactId>springboot-starter-data-fast</artifactId>
4747
</dependency>
4848

49+
<dependency>
50+
<groupId>com.codingapi.springboot</groupId>
51+
<artifactId>springboot-starter-id-generator</artifactId>
52+
</dependency>
4953

5054
<dependency>
5155
<groupId>org.springframework.boot</groupId>

springboot-example/src/main/java/com/codingapi/springboot/example/ui/controller/OpenController.java

+26
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
import com.codingapi.springboot.framework.dto.request.PageRequest;
77
import com.codingapi.springboot.framework.dto.response.MultiResponse;
88
import com.codingapi.springboot.framework.dto.response.Response;
9+
import com.codingapi.springboot.generator.dao.IdGeneratorDao;
10+
import com.codingapi.springboot.generator.domain.IdGenerator;
911
import lombok.AllArgsConstructor;
1012
import org.springframework.web.bind.annotation.GetMapping;
1113
import org.springframework.web.bind.annotation.RequestMapping;
1214
import org.springframework.web.bind.annotation.RequestParam;
1315
import org.springframework.web.bind.annotation.RestController;
1416

17+
import java.sql.SQLException;
18+
import java.util.Date;
19+
import java.util.List;
20+
1521
@RestController
1622
@RequestMapping("/open/demo")
1723
@AllArgsConstructor
@@ -21,6 +27,8 @@ public class OpenController {
2127

2228
private final DemoEntityRepository demoEntityRepository;
2329

30+
private final IdGeneratorDao idGeneratorDao;
31+
2432
@GetMapping("/save")
2533
public Response save(@RequestParam("name") String name) {
2634
executor.create(name);
@@ -31,4 +39,22 @@ public Response save(@RequestParam("name") String name) {
3139
public MultiResponse<DemoEntity> findAll(PageRequest pageRequest) {
3240
return MultiResponse.of(demoEntityRepository.findAll(pageRequest));
3341
}
42+
43+
44+
45+
@GetMapping("/test-list")
46+
public List<IdGenerator> test1() throws SQLException{
47+
return idGeneratorDao.findAll();
48+
}
49+
50+
@GetMapping("/test-save")
51+
public Response test2() throws SQLException {
52+
IdGenerator generator = new IdGenerator();
53+
generator.setKey("xxxx");
54+
generator.setId(1L);
55+
generator.setUpdateTime(new Date());
56+
idGeneratorDao.save(generator);
57+
return Response.buildSuccess();
58+
}
59+
3460
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-parent</artifactId>
7+
<groupId>com.codingapi.springboot</groupId>
8+
<version>1.4.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot-starter-id-generator</artifactId>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
</properties>
17+
18+
<dependencies>
19+
20+
21+
<dependency>
22+
<groupId>com.h2database</groupId>
23+
<artifactId>h2</artifactId>
24+
</dependency>
25+
26+
27+
<dependency>
28+
<groupId>commons-dbutils</groupId>
29+
<artifactId>commons-dbutils</artifactId>
30+
</dependency>
31+
32+
</dependencies>
33+
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.codingapi.springboot.generator;
2+
3+
import com.codingapi.springboot.generator.dao.IdGeneratorDao;
4+
import com.codingapi.springboot.generator.properties.GeneratorProperties;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
public class AutoConfiguration {
12+
13+
@Bean
14+
@ConfigurationProperties(prefix = "codingapi.generator")
15+
public GeneratorProperties generatorProperties() {
16+
return new GeneratorProperties();
17+
}
18+
19+
@Bean(initMethod = "init")
20+
@ConditionalOnMissingBean
21+
public IdGeneratorDao idGeneratorDao(GeneratorProperties generatorProperties){
22+
return new IdGeneratorDao(generatorProperties.getJdbcUrl());
23+
}
24+
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.codingapi.springboot.generator;
2+
3+
public class GeneratorContext {
4+
5+
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.codingapi.springboot.generator.dao;
2+
3+
import org.apache.commons.dbutils.QueryRunner;
4+
import org.h2.jdbcx.JdbcDataSource;
5+
6+
import java.sql.Connection;
7+
import java.sql.SQLException;
8+
9+
public class DbHelper<T> {
10+
11+
private final JdbcDataSource dataSource;
12+
private final QueryRunner queryRunner;
13+
14+
15+
interface IExecute {
16+
default void execute(Connection connection, QueryRunner queryRunner) throws SQLException {
17+
}
18+
}
19+
20+
interface IUpdateAndQuery<T> {
21+
default T updateAndQuery(Connection connection, QueryRunner queryRunner) throws SQLException {
22+
return null;
23+
}
24+
}
25+
26+
interface IUpdate {
27+
default int update(Connection connection, QueryRunner queryRunner) throws SQLException {
28+
return 0;
29+
}
30+
}
31+
32+
interface IQuery<T> {
33+
default T query(Connection connection, QueryRunner queryRunner) throws SQLException {
34+
return null;
35+
}
36+
}
37+
38+
public DbHelper(String jdbcUrl) {
39+
this.dataSource = new JdbcDataSource();
40+
this.queryRunner = new QueryRunner();
41+
this.dataSource.setURL(jdbcUrl);
42+
}
43+
44+
45+
public void execute(IExecute execute) throws SQLException {
46+
Connection connection = dataSource.getConnection();
47+
execute.execute(connection, queryRunner);
48+
connection.close();
49+
}
50+
51+
52+
public T updateAndQuery(IUpdateAndQuery<T> execute) throws SQLException {
53+
Connection connection = dataSource.getConnection();
54+
connection.setAutoCommit(false);
55+
T res = execute.updateAndQuery(connection, queryRunner);
56+
connection.commit();
57+
connection.close();
58+
return res;
59+
}
60+
61+
public int update(IUpdate execute) throws SQLException {
62+
Connection connection = dataSource.getConnection();
63+
connection.setAutoCommit(false);
64+
int res = execute.update(connection, queryRunner);
65+
connection.commit();
66+
connection.close();
67+
return res;
68+
}
69+
70+
public T query(IQuery<T> execute) throws SQLException {
71+
Connection connection = dataSource.getConnection();
72+
T res = execute.query(connection, queryRunner);
73+
connection.close();
74+
return res;
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.codingapi.springboot.generator.dao;
2+
3+
import com.codingapi.springboot.generator.domain.IdGenerator;
4+
import org.apache.commons.dbutils.QueryRunner;
5+
import org.apache.commons.dbutils.ResultSetHandler;
6+
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class IdGeneratorDao {
13+
14+
private final DbHelper<List<IdGenerator>> dbHelper;
15+
16+
private final ResultSetHandler<List<IdGenerator>> handler;
17+
18+
public IdGeneratorDao(String jdbcUrl) {
19+
this.dbHelper = new DbHelper<>(jdbcUrl);
20+
this.handler = rs -> {
21+
List<IdGenerator> list = new ArrayList<>();
22+
while (rs.next()) {
23+
IdGenerator generator = new IdGenerator();
24+
generator.setKey(rs.getString("TAG"));
25+
generator.setId(rs.getInt("ID"));
26+
generator.setUpdateTime(rs.getDate("UPDATE_TIME"));
27+
list.add(generator);
28+
}
29+
return list;
30+
};
31+
}
32+
33+
public boolean save(IdGenerator generator) throws SQLException{
34+
return dbHelper.update(new DbHelper.IUpdate() {
35+
@Override
36+
public int update(Connection connection, QueryRunner queryRunner) throws SQLException {
37+
List<IdGenerator> list = queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler, generator.getKey());
38+
if (list != null && list.size() > 0) {
39+
return 0;
40+
}
41+
42+
String sql = "INSERT INTO ID_GENERATOR (ID, UPDATE_TIME, TAG) VALUES (?, ?, ?)";
43+
return queryRunner.update(connection, sql, generator.getId(), generator.getUpdateTime(), generator.getKey());
44+
}
45+
}) > 0;
46+
}
47+
48+
49+
public IdGenerator updateMaxId(IdGenerator generator) throws SQLException {
50+
return dbHelper.updateAndQuery(new DbHelper.IUpdateAndQuery<List<IdGenerator>>() {
51+
@Override
52+
public List<IdGenerator> updateAndQuery(Connection connection, QueryRunner queryRunner) throws SQLException {
53+
queryRunner.update(connection, "UPDATE ID_GENERATOR SET ID = ID + 1 WHERE TAG = ?", generator.getKey());
54+
return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler, generator.getKey());
55+
}
56+
}).stream().findFirst().orElse(null);
57+
}
58+
59+
60+
61+
public List<IdGenerator> findAll() throws SQLException {
62+
return dbHelper.query(new DbHelper.IQuery<List<IdGenerator>>() {
63+
@Override
64+
public List<IdGenerator> query(Connection connection, QueryRunner queryRunner) throws SQLException {
65+
return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR", handler);
66+
}
67+
});
68+
}
69+
70+
private void init() throws SQLException {
71+
dbHelper.execute(new DbHelper.IExecute() {
72+
@Override
73+
public void execute(Connection connection, QueryRunner queryRunner) throws SQLException {
74+
String sql = "CREATE TABLE IF NOT EXISTS ID_GENERATOR (TAG VARCHAR(128) NOT NULL, ID BIGINT NOT NULL, UPDATE_TIME TIMESTAMP NOT NULL, PRIMARY KEY (TAG))";
75+
queryRunner.execute(connection, sql);
76+
}
77+
});
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.springboot.generator.domain;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import java.util.Date;
7+
8+
@Setter
9+
@Getter
10+
public class IdGenerator {
11+
12+
private String key;
13+
private long id;
14+
private Date updateTime;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codingapi.springboot.generator.properties;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Setter
7+
@Getter
8+
public class GeneratorProperties {
9+
10+
private String jdbcUrl = "jdbc:h2:file:./generator.db";
11+
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
com.codingapi.springboot.generator.AutoConfiguration

0 commit comments

Comments
 (0)