5
5
import org .apache .commons .dbutils .QueryRunner ;
6
6
import org .apache .commons .dbutils .ResultSetHandler ;
7
7
8
+ import javax .sql .DataSource ;
8
9
import java .sql .Connection ;
9
10
import java .sql .SQLException ;
10
11
import java .util .ArrayList ;
@@ -16,8 +17,8 @@ public class IdKeyDao {
16
17
17
18
private final ResultSetHandler <List <IdKey >> handler ;
18
19
19
- public IdKeyDao (String jdbcUrl ) {
20
- this .dbHelper = new DbHelper <>(jdbcUrl );
20
+ public IdKeyDao (DataSource dataSource ) {
21
+ this .dbHelper = new DbHelper <>(dataSource );
21
22
this .handler = rs -> {
22
23
List <IdKey > list = new ArrayList <>();
23
24
while (rs .next ()) {
@@ -32,8 +33,8 @@ public IdKeyDao(String jdbcUrl) {
32
33
}
33
34
34
35
@ SneakyThrows
35
- public boolean save (IdKey generator ) {
36
- return dbHelper .update (new DbHelper .IUpdate () {
36
+ public void save (IdKey generator ) {
37
+ dbHelper .update (new DbHelper .IUpdate () {
37
38
@ Override
38
39
public int update (Connection connection , QueryRunner queryRunner ) throws SQLException {
39
40
List <IdKey > list = queryRunner .query (connection , "SELECT * FROM ID_GENERATOR WHERE TAG = ?" , handler , generator .getKey ());
@@ -44,7 +45,7 @@ public int update(Connection connection, QueryRunner queryRunner) throws SQLExce
44
45
String sql = "INSERT INTO ID_GENERATOR (ID, UPDATE_TIME, TAG) VALUES (?, ?, ?)" ;
45
46
return queryRunner .update (connection , sql , generator .getId (), generator .getUpdateTime (), generator .getKey ());
46
47
}
47
- }) > 0 ;
48
+ });
48
49
}
49
50
50
51
@@ -61,7 +62,7 @@ public List<IdKey> query(Connection connection, QueryRunner queryRunner) throws
61
62
62
63
@ SneakyThrows
63
64
public IdKey updateMaxId (IdKey generator ) {
64
- return dbHelper .updateAndQuery (new DbHelper .IUpdateAndQuery <List < IdKey > >() {
65
+ return dbHelper .updateAndQuery (new DbHelper .IUpdateAndQuery <>() {
65
66
@ Override
66
67
public List <IdKey > updateAndQuery (Connection connection , QueryRunner queryRunner ) throws SQLException {
67
68
queryRunner .update (connection , "UPDATE ID_GENERATOR SET ID = ID + 1 WHERE TAG = ?" , generator .getKey ());
@@ -73,7 +74,7 @@ public List<IdKey> updateAndQuery(Connection connection, QueryRunner queryRunner
73
74
74
75
@ SneakyThrows
75
76
public List <IdKey > findAll () throws SQLException {
76
- return dbHelper .query (new DbHelper .IQuery <List < IdKey > >() {
77
+ return dbHelper .query (new DbHelper .IQuery <>() {
77
78
@ Override
78
79
public List <IdKey > query (Connection connection , QueryRunner queryRunner ) throws SQLException {
79
80
return queryRunner .query (connection , "SELECT * FROM ID_GENERATOR" , handler );
@@ -85,8 +86,8 @@ private void init() throws SQLException {
85
86
dbHelper .execute (new DbHelper .IExecute () {
86
87
@ Override
87
88
public void execute (Connection connection , QueryRunner queryRunner ) throws SQLException {
88
- 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))" ;
89
- queryRunner .execute (connection , sql );
89
+ String sql = "CREATE TABLE IF NOT EXISTS ID_GENERATOR (TAG TEXT NOT NULL, ID INTEGER NOT NULL, UPDATE_TIME INTEGER NOT NULL, PRIMARY KEY (TAG))" ;
90
+ queryRunner .update (connection , sql );
90
91
}
91
92
});
92
93
}
0 commit comments