1
1
package com .codingapi .springboot .generator .dao ;
2
2
3
- import com .codingapi .springboot .generator .domain .IdGenerator ;
3
+ import com .codingapi .springboot .generator .domain .IdKey ;
4
+ import lombok .SneakyThrows ;
4
5
import org .apache .commons .dbutils .QueryRunner ;
5
6
import org .apache .commons .dbutils .ResultSetHandler ;
6
7
9
10
import java .util .ArrayList ;
10
11
import java .util .List ;
11
12
12
- public class IdGeneratorDao {
13
+ public class IdKeyDao {
13
14
14
- private final DbHelper <List <IdGenerator >> dbHelper ;
15
+ private final DbHelper <List <IdKey >> dbHelper ;
15
16
16
- private final ResultSetHandler <List <IdGenerator >> handler ;
17
+ private final ResultSetHandler <List <IdKey >> handler ;
17
18
18
- public IdGeneratorDao (String jdbcUrl ) {
19
+ public IdKeyDao (String jdbcUrl ) {
19
20
this .dbHelper = new DbHelper <>(jdbcUrl );
20
21
this .handler = rs -> {
21
- List <IdGenerator > list = new ArrayList <>();
22
+ List <IdKey > list = new ArrayList <>();
22
23
while (rs .next ()) {
23
- IdGenerator generator = new IdGenerator ();
24
+ IdKey generator = new IdKey ();
24
25
generator .setKey (rs .getString ("TAG" ));
25
26
generator .setId (rs .getInt ("ID" ));
26
- generator .setUpdateTime (rs .getDate ("UPDATE_TIME" ));
27
+ generator .setUpdateTime (rs .getLong ("UPDATE_TIME" ));
27
28
list .add (generator );
28
29
}
29
30
return list ;
30
31
};
31
32
}
32
33
33
- public boolean save (IdGenerator generator ) throws SQLException {
34
+ @ SneakyThrows
35
+ public boolean save (IdKey generator ) {
34
36
return dbHelper .update (new DbHelper .IUpdate () {
35
37
@ Override
36
38
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 ());
38
40
if (list != null && list .size () > 0 ) {
39
41
return 0 ;
40
42
}
@@ -46,22 +48,36 @@ public int update(Connection connection, QueryRunner queryRunner) throws SQLExce
46
48
}
47
49
48
50
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 >>() {
51
55
@ 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 {
53
69
queryRunner .update (connection , "UPDATE ID_GENERATOR SET ID = ID + 1 WHERE TAG = ?" , generator .getKey ());
54
70
return queryRunner .query (connection , "SELECT * FROM ID_GENERATOR WHERE TAG = ?" , handler , generator .getKey ());
55
71
}
56
72
}).stream ().findFirst ().orElse (null );
57
73
}
58
74
59
75
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 >>() {
63
79
@ Override
64
- public List <IdGenerator > query (Connection connection , QueryRunner queryRunner ) throws SQLException {
80
+ public List <IdKey > query (Connection connection , QueryRunner queryRunner ) throws SQLException {
65
81
return queryRunner .query (connection , "SELECT * FROM ID_GENERATOR" , handler );
66
82
}
67
83
});
@@ -71,7 +87,7 @@ private void init() throws SQLException {
71
87
dbHelper .execute (new DbHelper .IExecute () {
72
88
@ Override
73
89
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))" ;
75
91
queryRunner .execute (connection , sql );
76
92
}
77
93
});
0 commit comments