Skip to content

Commit ce98bb5

Browse files
author
veasion
committed
modify
1 parent da44458 commit ce98bb5

File tree

8 files changed

+43
-57
lines changed

8 files changed

+43
-57
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
veasion-db 是一个轻量级持久层ORM框架,除slf4j-api外不依赖任何第三方jar,该框架提供丰富灵活的数据库操作,
44
单元测试 query/update 目录下有大量示例及demo。
55

6-
框架无需写任何SQL,基本支持sql能实现的任意查询或更新,如多表关联查询、多表关联更新、子查询、with、window、insert select、replace、不同数据库分页等。
6+
框架无需写SQL,基本支持任意查询或更新,如多表关联查询、多表关联更新、子查询、union、with、window、insert select、replace、不同数据库分页等。
77

88
框架支持自定义拦截器,内置逻辑删除、数据隔离拦截器,可通过SPI或调用InterceptorUtils.addInterceptor方法加入扩展。
99
## maven 依赖

src/main/java/cn/veasion/db/interceptor/EntityDaoInterceptor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public interface EntityDaoInterceptor {
1111

1212
<R> R intercept(EntityDaoInvocation<R> invocation);
1313

14+
/**
15+
* 拦截器执行排序,sortIndex值越大越先执行
16+
*/
1417
default int sortIndex() {
1518
return 0;
1619
}

src/main/java/cn/veasion/db/jdbc/DataSourceProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.veasion.db.jdbc;
22

33
import cn.veasion.db.base.JdbcTypeEnum;
4+
import cn.veasion.db.utils.ISort;
45

56
import javax.sql.DataSource;
67
import java.sql.Connection;
@@ -12,7 +13,7 @@
1213
* @author luozhuowei
1314
* @date 2021/12/5
1415
*/
15-
public interface DataSourceProvider {
16+
public interface DataSourceProvider extends ISort {
1617

1718
/**
1819
* 获取数据源
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package cn.veasion.db.jdbc;
22

33
import cn.veasion.db.AbstractFilter;
4+
import cn.veasion.db.utils.ISort;
45

56
/**
67
* DynamicTableExt
78
*
89
* @author luozhuowei
910
* @date 2022/1/30
1011
*/
11-
public interface DynamicTableExt {
12+
public interface DynamicTableExt extends ISort {
1213

1314
String getTableName(String tableName, Class<?> entityClazz, AbstractFilter<?> filter, Object source);
1415

15-
default int sort() {
16-
return 0;
17-
}
18-
1916
}

src/main/java/cn/veasion/db/query/PageParam.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.veasion.db.query;
22

3+
import cn.veasion.db.utils.ISort;
4+
35
import java.util.List;
46

57
/**
@@ -8,7 +10,7 @@
810
* @author luozhuowei
911
* @date 2021/12/8
1012
*/
11-
public abstract class PageParam {
13+
public abstract class PageParam implements ISort {
1214

1315
protected int page = 1;
1416
protected int size = 10;
@@ -39,8 +41,4 @@ public void setSize(int size) {
3941
this.size = size;
4042
}
4143

42-
public int sort() {
43-
return 0;
44-
}
45-
4644
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.veasion.db.utils;
2+
3+
/**
4+
* ISort
5+
*
6+
* @author luozhuowei
7+
* @date 2022/11/9
8+
*/
9+
public interface ISort {
10+
11+
/**
12+
* 排序,值越大越优先
13+
*/
14+
default int sort() {
15+
return 0;
16+
}
17+
18+
}

src/main/java/cn/veasion/db/utils/ServiceLoaderUtils.java

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import cn.veasion.db.jdbc.DataSourceProvider;
44
import cn.veasion.db.jdbc.DynamicTableExt;
55
import cn.veasion.db.query.PageParam;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
86

97
import java.util.ArrayList;
108
import java.util.List;
@@ -18,28 +16,21 @@
1816
*/
1917
public class ServiceLoaderUtils {
2018

21-
private static Logger logger = LoggerFactory.getLogger(ServiceLoaderUtils.class);
22-
2319
private static DataSourceProvider dataSourceProvider;
2420
private static DynamicTableExt dynamicTableExt;
2521
private static TypeConvert typeConvert;
2622
private static PageParam pageParam;
2723

28-
public synchronized static DataSourceProvider dataSourceProvider() {
24+
public static DataSourceProvider dataSourceProvider() {
2925
if (dataSourceProvider != null) {
3026
return dataSourceProvider;
3127
}
32-
List<DataSourceProvider> list = loadList(DataSourceProvider.class);
33-
if (list.size() > 0) {
34-
dataSourceProvider = list.get(0);
35-
}
36-
if (list.size() > 1) {
37-
logger.warn("发现多个dataSourceProvider");
38-
}
39-
if (dataSourceProvider == null) {
40-
logger.warn("dataSourceProvider未获取到实例");
28+
synchronized (ServiceLoaderUtils.class) {
29+
if (dataSourceProvider != null) {
30+
return dataSourceProvider;
31+
}
32+
return (dataSourceProvider = loadOne(DataSourceProvider.class));
4133
}
42-
return dataSourceProvider;
4334
}
4435

4536
public static DynamicTableExt dynamicTableExt() {
@@ -50,14 +41,7 @@ public static DynamicTableExt dynamicTableExt() {
5041
if (dynamicTableExt != null) {
5142
return dynamicTableExt;
5243
}
53-
List<DynamicTableExt> list = loadList(DynamicTableExt.class);
54-
if (list.size() > 0) {
55-
if (list.size() > 1) {
56-
list.sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
57-
}
58-
dynamicTableExt = list.get(0);
59-
}
60-
return dynamicTableExt;
44+
return (dynamicTableExt = loadOne(DynamicTableExt.class));
6145
}
6246
}
6347

@@ -69,14 +53,7 @@ public static TypeConvert typeConvert() {
6953
if (typeConvert != null) {
7054
return typeConvert;
7155
}
72-
List<TypeConvert> list = loadList(TypeConvert.class);
73-
if (list.size() > 0) {
74-
if (list.size() > 1) {
75-
list.sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
76-
}
77-
typeConvert = list.get(0);
78-
}
79-
return typeConvert;
56+
return (typeConvert = loadOne(TypeConvert.class));
8057
}
8158
}
8259

@@ -88,26 +65,22 @@ public static PageParam pageParam() {
8865
if (pageParam != null) {
8966
return pageParam;
9067
}
91-
List<PageParam> list = loadList(PageParam.class);
92-
if (list.size() > 0) {
93-
if (list.size() > 1) {
94-
list.sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
95-
}
96-
pageParam = list.get(0);
97-
}
98-
return pageParam;
68+
return (pageParam = loadOne(PageParam.class));
9969
}
10070
}
10171

102-
@Deprecated
10372
public static <T> T loadOne(Class<T> clazz) {
10473
List<T> list = loadList(clazz);
10574
return list.size() > 0 ? list.get(0) : null;
10675
}
10776

77+
@SuppressWarnings("unchecked")
10878
public static <T> List<T> loadList(Class<T> clazz) {
10979
List<T> list = new ArrayList<>();
11080
ServiceLoader.load(clazz).forEach(list::add);
81+
if (list.size() > 1 && ISort.class.isAssignableFrom(clazz)) {
82+
((List<ISort>) list).sort((a, b) -> -Integer.compare(a.sort(), b.sort()));
83+
}
11184
return list;
11285
}
11386

src/main/java/cn/veasion/db/utils/TypeConvert.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
* @author luozhuowei
77
* @date 2021/12/8
88
*/
9-
public interface TypeConvert {
9+
public interface TypeConvert extends ISort {
1010

1111
<T> T convert(Object object, Class<T> clazz);
1212

1313
default Object convertValue(Object value) {
1414
return value;
1515
}
1616

17-
default int sort() {
18-
return 0;
19-
}
20-
2117
}

0 commit comments

Comments
 (0)