Skip to content

Commit 29dcb14

Browse files
committed
add id generator & remove leaf
1 parent 7e92e6d commit 29dcb14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+162
-1408
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@
1212
## maven install
1313

1414
```
15+
<!-- Springboot领域驱动框架 -->
1516
<dependency>
1617
<groupId>com.codingapi.springboot</groupId>
1718
<artifactId>springboot-starter</artifactId>
1819
<version>${last.version}</version>
1920
</dependency>
2021
22+
<!-- 快速数据呈现框架 -->
2123
<dependency>
2224
<groupId>com.codingapi.springboot</groupId>
23-
<artifactId>springboot-starter-leaf</artifactId>
25+
<artifactId>springboot-starter-data-fast</artifactId>
2426
<version>${last.version}</version>
2527
</dependency>
2628
29+
<!-- Id自增策略框架 -->
30+
<dependency>
31+
<groupId>com.codingapi.springboot</groupId>
32+
<artifactId>springboot-starter-id-generator</artifactId>
33+
<version>${last.version}</version>
34+
</dependency>
35+
36+
<!-- security&jwt权限框架 -->
2737
<dependency>
2838
<groupId>com.codingapi.springboot</groupId>
2939
<artifactId>springboot-starter-security-jwt</artifactId>

pom.xml

-9
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,6 @@
137137
<version>${codingapi.framework.version}</version>
138138
</dependency>
139139

140-
<dependency>
141-
<groupId>com.codingapi.springboot</groupId>
142-
<artifactId>springboot-starter-leaf</artifactId>
143-
<version>${codingapi.framework.version}</version>
144-
</dependency>
145-
146140
<dependency>
147141
<groupId>com.codingapi.springboot</groupId>
148142
<artifactId>springboot-starter-id-generator</artifactId>
@@ -222,7 +216,6 @@
222216
<module>springboot-starter</module>
223217
<module>springboot-example</module>
224218
<module>springboot-starter-security-jwt</module>
225-
<module>springboot-starter-leaf</module>
226219
<module>springboot-starter-data-fast</module>
227220
<module>springboot-starter-id-generator</module>
228221
</modules>
@@ -236,7 +229,6 @@
236229
<module>springboot-starter</module>
237230
<module>springboot-example</module>
238231
<module>springboot-starter-security-jwt</module>
239-
<module>springboot-starter-leaf</module>
240232
<module>springboot-starter-data-fast</module>
241233
<module>springboot-starter-id-generator</module>
242234
</modules>
@@ -284,7 +276,6 @@
284276
<modules>
285277
<module>springboot-starter</module>
286278
<module>springboot-starter-security-jwt</module>
287-
<module>springboot-starter-leaf</module>
288279
<module>springboot-starter-data-fast</module>
289280
<module>springboot-starter-id-generator</module>
290281
</modules>

springboot-example/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
<artifactId>springboot-starter-security-jwt</artifactId>
3737
</dependency>
3838

39-
<dependency>
40-
<groupId>com.codingapi.springboot</groupId>
41-
<artifactId>springboot-starter-leaf</artifactId>
42-
</dependency>
43-
4439
<dependency>
4540
<groupId>com.codingapi.springboot</groupId>
4641
<artifactId>springboot-starter-data-fast</artifactId>

springboot-example/src/main/java/com/codingapi/springboot/example/ExampleApplication.java

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codingapi.springboot.example;
22

3-
import com.codingapi.springboot.leaf.EnableLeaf;
43
import org.springframework.boot.SpringApplication;
54
import org.springframework.boot.autoconfigure.SpringBootApplication;
65
import org.springframework.context.annotation.Bean;
@@ -10,7 +9,6 @@
109
import java.util.Locale;
1110

1211
@SpringBootApplication
13-
@EnableLeaf
1412
public class ExampleApplication {
1513

1614
public static void main(String[] args) {

springboot-example/src/main/java/com/codingapi/springboot/example/domain/entity/Demo.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import com.codingapi.springboot.example.domain.event.DemoNameChangeEvent;
44
import com.codingapi.springboot.framework.event.EventPusher;
5-
import com.codingapi.springboot.leaf.LeafIdGenerate;
5+
import com.codingapi.springboot.generator.IdGenerate;
66
import lombok.Getter;
77

88
/**
99
* @author lorne
1010
* @since 1.0.0
1111
*/
12-
public class Demo implements LeafIdGenerate {
12+
public class Demo implements IdGenerate {
1313

1414
@Getter
1515
private Integer id;

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
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;
9+
import com.codingapi.springboot.generator.dao.IdKeyDao;
10+
import com.codingapi.springboot.generator.domain.IdKey;
1111
import lombok.AllArgsConstructor;
1212
import org.springframework.web.bind.annotation.GetMapping;
1313
import org.springframework.web.bind.annotation.RequestMapping;
1414
import org.springframework.web.bind.annotation.RequestParam;
1515
import org.springframework.web.bind.annotation.RestController;
1616

1717
import java.sql.SQLException;
18-
import java.util.Date;
1918
import java.util.List;
2019

2120
@RestController
@@ -27,7 +26,7 @@ public class OpenController {
2726

2827
private final DemoEntityRepository demoEntityRepository;
2928

30-
private final IdGeneratorDao idGeneratorDao;
29+
private final IdKeyDao idKeyDao;
3130

3231
@GetMapping("/save")
3332
public Response save(@RequestParam("name") String name) {
@@ -43,17 +42,14 @@ public MultiResponse<DemoEntity> findAll(PageRequest pageRequest) {
4342

4443

4544
@GetMapping("/test-list")
46-
public List<IdGenerator> test1() throws SQLException{
47-
return idGeneratorDao.findAll();
45+
public List<IdKey> test1() throws SQLException{
46+
return idKeyDao.findAll();
4847
}
4948

5049
@GetMapping("/test-save")
5150
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);
51+
IdKey generator = new IdKey("xxx");
52+
idKeyDao.save(generator);
5753
return Response.buildSuccess();
5854
}
5955

springboot-example/src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ spring.datasource.url=jdbc:h2:file:./demo.db
44
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
55
spring.jpa.hibernate.ddl-auto=create-drop
66
spring.jpa.show-sql=true
7-
codingapi.leaf.jdbc-url=jdbc:h2:mem:leaf;DB_CLOSE_DELAY=-1
7+
codingapi.id.generator.jdbc-url=jdbc:h2:mem:generator;DB_CLOSE_DELAY=-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codingapi.springboot.generator;
22

3-
import com.codingapi.springboot.generator.dao.IdGeneratorDao;
3+
import com.codingapi.springboot.generator.dao.IdKeyDao;
44
import com.codingapi.springboot.generator.properties.GeneratorProperties;
55
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
66
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -11,16 +11,17 @@
1111
public class AutoConfiguration {
1212

1313
@Bean
14-
@ConfigurationProperties(prefix = "codingapi.generator")
14+
@ConfigurationProperties(prefix = "codingapi.id.generator")
1515
public GeneratorProperties generatorProperties() {
1616
return new GeneratorProperties();
1717
}
1818

1919
@Bean(initMethod = "init")
2020
@ConditionalOnMissingBean
21-
public IdGeneratorDao idGeneratorDao(GeneratorProperties generatorProperties){
22-
return new IdGeneratorDao(generatorProperties.getJdbcUrl());
21+
public IdKeyDao idKeyDao(GeneratorProperties generatorProperties){
22+
IdKeyDao keyDao = new IdKeyDao(generatorProperties.getJdbcUrl());
23+
GeneratorContext.getInstance().init(keyDao);
24+
return keyDao;
2325
}
2426

25-
2627
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
package com.codingapi.springboot.generator;
22

3+
import com.codingapi.springboot.generator.dao.IdKeyDao;
4+
35
public class GeneratorContext {
46

7+
private GeneratorContext() {
8+
}
9+
10+
private static GeneratorContext instance;
11+
12+
public static GeneratorContext getInstance() {
13+
if (instance == null) {
14+
synchronized (GeneratorContext.class) {
15+
if (instance == null) {
16+
instance = new GeneratorContext();
17+
}
18+
}
19+
}
20+
return instance;
21+
}
22+
23+
private IdGenerateContext idGenerateContext;
24+
25+
long generateId(Class<?> clazz) {
26+
return idGenerateContext.generateId(clazz);
27+
}
28+
29+
void init(IdKeyDao idKeyDao){
30+
idGenerateContext = new IdGenerateContext(idKeyDao);
31+
}
532

633
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.springboot.generator;
2+
3+
public interface IdGenerate {
4+
5+
default long generateLongId() {
6+
return GeneratorContext.getInstance().generateId(getClass());
7+
}
8+
9+
default int generateIntId() {
10+
return (int) GeneratorContext.getInstance().generateId(getClass());
11+
}
12+
13+
default String generateStringId() {
14+
return String.valueOf(GeneratorContext.getInstance().generateId(getClass()));
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codingapi.springboot.generator;
2+
3+
import com.codingapi.springboot.generator.dao.IdKeyDao;
4+
import com.codingapi.springboot.generator.domain.IdKey;
5+
6+
public class IdGenerateContext {
7+
8+
private final IdKeyDao keyDao;
9+
10+
public IdGenerateContext(IdKeyDao keyDao) {
11+
this.keyDao = keyDao;
12+
}
13+
14+
public synchronized long generateId(Class<?> clazz) {
15+
IdKey idKey = keyDao.getByKey(clazz.getName());
16+
if(idKey==null){
17+
idKey = new IdKey(clazz.getName());
18+
keyDao.save(idKey);
19+
}else{
20+
keyDao.updateMaxId(idKey);
21+
}
22+
return idKey.getId();
23+
}
24+
}

springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/dao/IdGeneratorDao.java springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/dao/IdKeyDao.java

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingapi.springboot.generator.dao;
22

3-
import com.codingapi.springboot.generator.domain.IdGenerator;
3+
import com.codingapi.springboot.generator.domain.IdKey;
4+
import lombok.SneakyThrows;
45
import org.apache.commons.dbutils.QueryRunner;
56
import org.apache.commons.dbutils.ResultSetHandler;
67

@@ -9,32 +10,33 @@
910
import java.util.ArrayList;
1011
import java.util.List;
1112

12-
public class IdGeneratorDao {
13+
public class IdKeyDao {
1314

14-
private final DbHelper<List<IdGenerator>> dbHelper;
15+
private final DbHelper<List<IdKey>> dbHelper;
1516

16-
private final ResultSetHandler<List<IdGenerator>> handler;
17+
private final ResultSetHandler<List<IdKey>> handler;
1718

18-
public IdGeneratorDao(String jdbcUrl) {
19+
public IdKeyDao(String jdbcUrl) {
1920
this.dbHelper = new DbHelper<>(jdbcUrl);
2021
this.handler = rs -> {
21-
List<IdGenerator> list = new ArrayList<>();
22+
List<IdKey> list = new ArrayList<>();
2223
while (rs.next()) {
23-
IdGenerator generator = new IdGenerator();
24+
IdKey generator = new IdKey();
2425
generator.setKey(rs.getString("TAG"));
2526
generator.setId(rs.getInt("ID"));
26-
generator.setUpdateTime(rs.getDate("UPDATE_TIME"));
27+
generator.setUpdateTime(rs.getLong("UPDATE_TIME"));
2728
list.add(generator);
2829
}
2930
return list;
3031
};
3132
}
3233

33-
public boolean save(IdGenerator generator) throws SQLException{
34+
@SneakyThrows
35+
public boolean save(IdKey generator) {
3436
return dbHelper.update(new DbHelper.IUpdate() {
3537
@Override
3638
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());
39+
List<IdKey> list = queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler, generator.getKey());
3840
if (list != null && list.size() > 0) {
3941
return 0;
4042
}
@@ -46,22 +48,36 @@ public int update(Connection connection, QueryRunner queryRunner) throws SQLExce
4648
}
4749

4850

49-
public IdGenerator updateMaxId(IdGenerator generator) throws SQLException {
50-
return dbHelper.updateAndQuery(new DbHelper.IUpdateAndQuery<List<IdGenerator>>() {
51+
52+
@SneakyThrows
53+
public IdKey getByKey(String key) {
54+
return dbHelper.query(new DbHelper.IQuery<List<IdKey>>() {
5155
@Override
52-
public List<IdGenerator> updateAndQuery(Connection connection, QueryRunner queryRunner) throws SQLException {
56+
public List<IdKey> query(Connection connection, QueryRunner queryRunner) throws SQLException {
57+
return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler,key);
58+
}
59+
}).stream().findFirst().orElse(null);
60+
}
61+
62+
63+
64+
@SneakyThrows
65+
public IdKey updateMaxId(IdKey generator) {
66+
return dbHelper.updateAndQuery(new DbHelper.IUpdateAndQuery<List<IdKey>>() {
67+
@Override
68+
public List<IdKey> updateAndQuery(Connection connection, QueryRunner queryRunner) throws SQLException {
5369
queryRunner.update(connection, "UPDATE ID_GENERATOR SET ID = ID + 1 WHERE TAG = ?", generator.getKey());
5470
return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR WHERE TAG = ?", handler, generator.getKey());
5571
}
5672
}).stream().findFirst().orElse(null);
5773
}
5874

5975

60-
61-
public List<IdGenerator> findAll() throws SQLException {
62-
return dbHelper.query(new DbHelper.IQuery<List<IdGenerator>>() {
76+
@SneakyThrows
77+
public List<IdKey> findAll() throws SQLException {
78+
return dbHelper.query(new DbHelper.IQuery<List<IdKey>>() {
6379
@Override
64-
public List<IdGenerator> query(Connection connection, QueryRunner queryRunner) throws SQLException {
80+
public List<IdKey> query(Connection connection, QueryRunner queryRunner) throws SQLException {
6581
return queryRunner.query(connection, "SELECT * FROM ID_GENERATOR", handler);
6682
}
6783
});
@@ -71,7 +87,7 @@ private void init() throws SQLException {
7187
dbHelper.execute(new DbHelper.IExecute() {
7288
@Override
7389
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))";
90+
String sql = "CREATE TABLE IF NOT EXISTS ID_GENERATOR (TAG VARCHAR(128) NOT NULL, ID BIGINT NOT NULL, UPDATE_TIME BIGINT NOT NULL, PRIMARY KEY (TAG))";
7591
queryRunner.execute(connection, sql);
7692
}
7793
});

springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/domain/IdGenerator.java

-15
This file was deleted.

0 commit comments

Comments
 (0)