Skip to content

Commit 8419744

Browse files
committed
add leaf starter ..
1 parent 1168a64 commit 8419744

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

springboot-starter-leaf/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<artifactId>reflections</artifactId>
2222
</dependency>
2323

24+
<dependency>
25+
<groupId>commons-dbutils</groupId>
26+
<artifactId>commons-dbutils</artifactId>
27+
</dependency>
28+
2429
<dependency>
2530
<groupId>org.perf4j</groupId>
2631
<artifactId>perf4j</artifactId>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.codingapi.springboot.leaf.h2;
2+
3+
import org.apache.commons.dbutils.QueryRunner;
4+
import org.apache.commons.dbutils.ResultSetHandler;
5+
6+
import javax.sql.DataSource;
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
10+
public class DataHelper {
11+
12+
private DataSource dataSource;
13+
14+
private QueryRunner queryRunner;
15+
16+
public DataHelper(DataSource dataSource) {
17+
this.dataSource = dataSource;
18+
this.queryRunner = new QueryRunner();
19+
}
20+
21+
public void execute(String sql,Object...params) throws DbException {
22+
try {
23+
Connection connection = dataSource.getConnection();
24+
queryRunner.execute(connection,sql,params);
25+
connection.close();
26+
} catch (SQLException e) {
27+
throw new DbException(e);
28+
}
29+
}
30+
31+
public void update(String sql,Object...params) throws DbException {
32+
try {
33+
Connection connection = dataSource.getConnection();
34+
connection.setAutoCommit(false);
35+
queryRunner.update(connection,sql,params);
36+
connection.commit();
37+
connection.close();
38+
} catch (SQLException e) {
39+
throw new DbException(e);
40+
}
41+
}
42+
43+
public <T> void query(String sql, ResultSetHandler<T> handler, Object...params)throws DbException{
44+
try {
45+
Connection connection = dataSource.getConnection();
46+
queryRunner.query(connection,sql,handler,params);
47+
connection.close();
48+
} catch (SQLException e) {
49+
throw new DbException(e);
50+
}
51+
}
52+
53+
54+
55+
56+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codingapi.springboot.leaf.h2;
2+
3+
public class DbException extends RuntimeException{
4+
5+
public DbException() {
6+
}
7+
8+
public DbException(String message) {
9+
super(message);
10+
}
11+
12+
public DbException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
15+
16+
public DbException(Throwable cause) {
17+
super(cause);
18+
}
19+
20+
public DbException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
21+
super(message, cause, enableSuppression, writableStackTrace);
22+
}
23+
24+
}

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/segment/dao/impl/IDAllocDaoImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codingapi.springboot.leaf.segment.dao.impl;
22

3+
import com.codingapi.springboot.leaf.h2.DataHelper;
34
import com.codingapi.springboot.leaf.segment.dao.IDAllocDao;
45
import com.codingapi.springboot.leaf.segment.model.LeafAlloc;
56
import lombok.AllArgsConstructor;
@@ -11,34 +12,50 @@
1112
@AllArgsConstructor
1213
public class IDAllocDaoImpl implements IDAllocDao {
1314

15+
private DataHelper dataHelper;
16+
1417
@Override
1518
public List<LeafAlloc> getAllLeafAllocs() {
1619
//todo leaf
20+
String sql = "select * from leaf_alloc";
1721
return new ArrayList<>();
1822
}
1923

2024
@Override
2125
public LeafAlloc updateMaxIdAndGetLeafAlloc(String tag) {
2226
//todo leaf
27+
//UPDATE leaf_alloc SET maxId = maxId + step WHERE key = :key
28+
29+
//select * from leaf_alloc where key = :key
2330
LeafAlloc leafAlloc = new LeafAlloc();
2431
return leafAlloc;
2532
}
2633

2734
@Override
2835
public LeafAlloc updateMaxIdByCustomStepAndGetLeafAlloc(LeafAlloc leafAlloc) {
36+
//UPDATE leaf_alloc SET maxId = maxId + :step WHERE key = :key
37+
38+
//select * from leaf_alloc where key = :key
2939
//todo leaf
3040
return leafAlloc;
3141
}
3242

3343
@Override
3444
public List<String> getAllTags() {
45+
String sql = "select tag from leaf_alloc";
3546
//todo leaf
3647
return Arrays.asList("tags");
3748
}
3849

3950
@Override
4051
public boolean save(LeafAlloc leafInfo) {
52+
String sql = "insert into leaf_alloc (max_id, step, update_time, tag) values (?, ?, ?, ?)";
4153
//todo leaf
4254
return true;
4355
}
56+
57+
public void init(){
58+
String sql = "create table leaf_alloc (tag varchar(128) not null, max_id bigint, step integer, update_time timestamp, primary key (tag))";
59+
60+
}
4461
}

0 commit comments

Comments
 (0)