Skip to content

Commit d020bb5

Browse files
author
veasion
committed
对象支持序列化 & 单元测试多租户拦截器
1 parent c557f9d commit d020bb5

Some content is hidden

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

48 files changed

+778
-110
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ veasion-db 是一个轻量级持久层ORM框架,除slf4j-api外不依赖任何
1212
<dependency>
1313
<groupId>cn.veasion</groupId>
1414
<artifactId>veasion-db</artifactId>
15-
<version>1.2.3</version>
15+
<version>1.2.4</version>
1616
</dependency>
1717
```
1818
支持sql解析生成veasion-db代码
@@ -61,6 +61,10 @@ public class SimpleQueryTest extends BaseTest {
6161
// select * from t_student where id = 1
6262
studentDao.getById(1L);
6363

64+
// 根据id查询学生性别(性别转枚举)
65+
// select sex from t_student where id = 1
66+
SexEnum sexEnum = studentDao.queryForType(new Q("sex").eq("id", 1), SexEnum.class);
67+
6468
// 查询学号为s001的学生名称
6569
// select name from t_student where sno = 's001'
6670
studentDao.queryForType(new Q("name").eq("sno", "s001"), String.class);
@@ -379,7 +383,7 @@ public class InsertTest extends BaseTest {
379383
studentPO.setName("学生_" + s);
380384
studentPO.setSno("s" + s);
381385
studentPO.setAge(18);
382-
studentPO.setSex(1);
386+
studentPO.setSex(SexEnum.MALE);
383387
studentPO.setClassId(1L);
384388
studentPO.setIsDeleted(0L);
385389
studentPO.setVersion(0);
@@ -410,9 +414,33 @@ public class InsertTest extends BaseTest {
410414
前端传 { id: 1 } 自动映射成 select * from t_student where id = 1 <br>
411415
前端传 { id: 1, className: '三年二班' } 自动映射成 select s.* from t_student s join t_classes c on s.class_id = c.id where s.id = 1 and c.name = '三年二班' <br>
412416

413-
具体参考单元测试 QueryCriteriaTest
417+
具体参考单元测试 cn.veasion.db.criteria.QueryCriteriaTest
418+
419+
### 拦截器
420+
自定义拦截器可继承抽象类 cn.veasion.db.interceptor.AbstractInterceptor<br>
421+
422+
内置:逻辑删除拦截器 cn.veasion.db.interceptor.LogicDeleteInterceptor <br>
423+
内置:拒绝无条件修改删除拦截器 cn.veasion.db.interceptor.UpdateDeleteNoFilterInterceptor <br>
424+
425+
其他如租户SaaS数据隔离拦截器实现见单元测试:cn.veasion.db.interceptor.TenantInterceptor <br>
426+
427+
需要使用上面拦截器功能可在项目 resources/META-INF/services 目录下新建 SPI 文件 cn.veasion.db.interceptor.EntityDaoInterceptor 中加入指定拦截器类,或显性调用InterceptorUtils.addInterceptor方法添加<br>
428+
429+
具体参考单元测试代码示例。
430+
431+
### 类型转换
432+
框架默认支持基本数据类型转换,其他类型可自定义扩展,SPI 实现 cn.veasion.db.utils.TypeConvert 接口<br>
433+
434+
示例:枚举转换扩展见单元测试 cn.veasion.db.interceptor.ExtTypeConvert
435+
436+
### 动态表名
437+
见 cn.veasion.db.jdbc.DefaultDynamicTableExt 加入SPI支持。<br>
438+
439+
具体参考单元测试:cn.veasion.db.table.DynamicTableTest
414440

415441
### spring 项目接入 veasion-db
442+
推荐使用基础框架 [veasion-project-base](https://github.com/veasion/veasion-project-base) 接入
443+
416444
SPI 实现 cn.veasion.db.jdbc.DataSourceProvider 接口
417445
```java
418446
public class DefaultDataSourceProvider implements DataSourceProvider {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>cn.veasion</groupId>
99
<artifactId>veasion-db</artifactId>
10-
<version>1.2.3</version>
10+
<version>1.2.4</version>
1111

1212
<name>veasion-db</name>
1313
<url>https://github.com/veasion/veasion-db</url>

src/main/java/cn/veasion/db/AbstractFilter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Collections;
1212
import java.util.List;
1313
import java.util.Objects;
14+
import java.util.function.Consumer;
1415
import java.util.stream.Collectors;
1516

1617
/**
@@ -103,6 +104,10 @@ public T between(String field, Object value1, Object value2) {
103104
return addFilter(Filter.between(field, value1, value2));
104105
}
105106

107+
public T notBetween(String field, Object value1, Object value2) {
108+
return addFilter(Filter.notBetween(field, value1, value2));
109+
}
110+
106111
public T andBracket(Filter... filters) {
107112
addFilter(Filter.leftBracket());
108113
addFilters(filters);
@@ -148,6 +153,11 @@ public T sqlFilter(Filter.SqlFilterHandler sqlFilterHandler, Object... values) {
148153
return addFilter(Filter.sqlFilter(sqlFilterHandler, values));
149154
}
150155

156+
public T exec(Consumer<T> consumer) {
157+
consumer.accept(getSelf());
158+
return getSelf();
159+
}
160+
151161
public List<Filter> getFilters() {
152162
return filters;
153163
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package cn.veasion.db;
22

33
import cn.veasion.db.base.Filter;
4+
import cn.veasion.db.utils.TypeUtils;
5+
6+
import java.io.Serializable;
47

58
/**
69
* IFilter
710
*
811
* @author luozhuowei
912
* @date 2022/11/30
1013
*/
11-
public interface IFilter<T> {
14+
public interface IFilter<T> extends Serializable {
1215

1316
T addFilter(Filter filter);
1417

18+
@SuppressWarnings("unchecked")
19+
default T copy() {
20+
try {
21+
return (T) TypeUtils.serializableCopy(this);
22+
} catch (Exception e) {
23+
throw new RuntimeException(e);
24+
}
25+
}
26+
1527
}

src/main/java/cn/veasion/db/TableEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cn.veasion.db;
22

3+
import java.io.Serializable;
4+
35
/**
46
* TableEntity
57
*
68
* @author luozhuowei
79
* @date 2022/11/8
810
*/
9-
public final class TableEntity {
11+
public final class TableEntity implements Serializable {
1012

1113
private String table;
1214

src/main/java/cn/veasion/db/base/Expression.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import cn.veasion.db.DbException;
44

5+
import java.io.Serializable;
6+
57
/**
68
* Expression
79
*
810
* @author luozhuowei
911
* @date 2021/12/2
1012
*/
11-
public class Expression {
13+
public class Expression implements Serializable {
1214

1315
private String alias;
1416
private String expression;

src/main/java/cn/veasion/db/base/Filter.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import cn.veasion.db.utils.FieldUtils;
77
import cn.veasion.db.utils.FilterUtils;
88

9+
import java.io.Serializable;
910
import java.util.Arrays;
1011
import java.util.Collection;
1112
import java.util.Objects;
@@ -16,7 +17,7 @@
1617
* @author luozhuowei
1718
* @date 2021/12/1
1819
*/
19-
public class Filter {
20+
public class Filter implements Serializable {
2021

2122
public static Filter eq(String field, Object value) {
2223
return build(field, Operator.EQ, value);
@@ -170,6 +171,14 @@ public static <T, R> Filter between(LambdaFunction<T, R> fieldLambda, Object val
170171
return build(fieldLambda, Operator.BETWEEN, new Object[]{value1, value2}, Operator.BETWEEN.getOpt().concat(" ? AND ?"));
171172
}
172173

174+
public static Filter notBetween(String field, Object value1, Object value2) {
175+
return build(field, Operator.NOT_BETWEEN, new Object[]{value1, value2}, Operator.NOT_BETWEEN.getOpt().concat(" ? AND ?"));
176+
}
177+
178+
public static <T, R> Filter notBetween(LambdaFunction<T, R> fieldLambda, Object value1, Object value2) {
179+
return build(fieldLambda, Operator.NOT_BETWEEN, new Object[]{value1, value2}, Operator.NOT_BETWEEN.getOpt().concat(" ? AND ?"));
180+
}
181+
173182
public static Filter subQuery(Operator operator, SubQueryParam subQueryParam) {
174183
return build((String) null, operator, Objects.requireNonNull(subQueryParam), null).special();
175184
}
@@ -345,6 +354,23 @@ public boolean isSpecial() {
345354
return special;
346355
}
347356

357+
@Override
358+
public boolean equals(Object object) {
359+
if (this == object) return true;
360+
if (object == null || getClass() != object.getClass()) return false;
361+
Filter filter = (Filter) object;
362+
return Objects.equals(sql, filter.sql) &&
363+
Objects.equals(field, filter.field) &&
364+
operator == filter.operator &&
365+
special == filter.special &&
366+
Objects.equals(value, filter.value);
367+
}
368+
369+
@Override
370+
public int hashCode() {
371+
return Objects.hash(field, operator, sql, value, special);
372+
}
373+
348374
@Override
349375
public String toString() {
350376
if (operator != null && value != null) {

src/main/java/cn/veasion/db/base/IBaseId.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cn.veasion.db.base;
22

3+
import java.io.Serializable;
4+
35
/**
46
* IBaseId
57
*
68
* @author luozhuowei
79
* @date 2021/12/2
810
*/
9-
public interface IBaseId<ID> {
11+
public interface IBaseId<ID> extends Serializable {
1012

1113
ID getId();
1214

src/main/java/cn/veasion/db/base/JdbcTypeEnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cn.veasion.db.base;
22

3+
import java.io.Serializable;
4+
35
/**
46
* JdbcTypeEnum
57
*
68
* @author luozhuowei
79
* @date 2021/12/9
810
*/
9-
public enum JdbcTypeEnum {
11+
public enum JdbcTypeEnum implements Serializable {
1012

1113
INSERT,
1214

src/main/java/cn/veasion/db/base/JoinType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cn.veasion.db.base;
22

3+
import java.io.Serializable;
4+
35
/**
46
* JoinType
57
*
68
* @author luozhuowei
79
* @date 2021/12/10
810
*/
9-
public interface JoinType {
11+
public interface JoinType extends Serializable {
1012

1113
String getJoin();
1214

src/main/java/cn/veasion/db/base/Operator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cn.veasion.db.base;
22

3+
import java.io.Serializable;
4+
35
/**
46
* Operator
57
*
68
* @author luozhuowei
79
* @date 2021/12/13
810
*/
9-
public enum Operator {
11+
public enum Operator implements Serializable {
1012

1113
EQ("="),
1214
NEQ("<>"),
@@ -19,6 +21,7 @@ public enum Operator {
1921
LIKE("LIKE"),
2022
NOT_LIKE("NOT LIKE"),
2123
BETWEEN("BETWEEN"),
24+
NOT_BETWEEN("NOT BETWEEN"),
2225
EXISTS("EXISTS"),
2326
NOT_EXISTS("NOT EXISTS"),
2427
NULL("IS NULL"),

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,21 @@ public <R> R intercept(EntityDaoInvocation<R> invocation) {
4646
Object[] args = invocation.getArgs();
4747
if (args != null && !skip()) {
4848
for (Object arg : args) {
49-
if (arg instanceof AbstractQuery && handleQuery) {
49+
if (handleQuery && arg instanceof AbstractQuery) {
5050
handleQuery((AbstractQuery<?>) arg);
51-
} else if (arg instanceof AbstractUpdate && handleUpdate) {
51+
} else if (handleUpdate && arg instanceof AbstractUpdate) {
5252
handleUpdate((AbstractUpdate<?>) arg);
53-
} else if (arg instanceof Delete && handleDelete) {
53+
} else if (handleDelete && arg instanceof Delete) {
5454
if (!containSkipClass(((Delete) arg).getEntityClass())) {
5555
handleDelete((Delete) arg);
5656
}
57-
} else if (arg instanceof AbstractFilter) {
58-
if (handleAbstractFilter) {
59-
handleAbstractFilter((AbstractFilter<?>) arg);
60-
}
57+
} else if (handleAbstractFilter && arg instanceof AbstractFilter) {
58+
handleAbstractFilter((AbstractFilter<?>) arg);
6159
} else if (arg instanceof BatchEntityInsert) {
6260
if (!containSkipClass(((BatchEntityInsert) arg).getEntityClass())) {
6361
handleBatchInsert((BatchEntityInsert) arg);
6462
}
65-
} else if (arg instanceof EntityInsert && handleInsert) {
63+
} else if (handleInsert && arg instanceof EntityInsert) {
6664
EntityInsert insert = (EntityInsert) arg;
6765
if (!containSkipClass(insert.getEntityClass())) {
6866
handleInsert(insert.getEntityClass(), Collections.singletonList(insert.getEntity()), Collections.singletonList(insert.getFieldValueMap()));
@@ -147,11 +145,10 @@ protected void handleUpdate(AbstractUpdate<?> update) {
147145

148146
protected void handleBatchInsert(BatchEntityInsert insert) {
149147
AbstractQuery<?> insertSelectQuery = insert.getInsertSelectQuery();
150-
if (insertSelectQuery != null) {
151-
if (handleQuery) {
152-
handleQuery(insertSelectQuery);
153-
}
154-
} else if (handleInsert) {
148+
if (handleQuery && insertSelectQuery != null) {
149+
handleQuery(insertSelectQuery);
150+
}
151+
if (handleInsert && insert.getEntityList() != null) {
155152
handleInsert(insert.getEntityClass(), insert.getEntityList(), insert.getFieldValueMapList());
156153
}
157154
}

0 commit comments

Comments
 (0)