Skip to content

Commit 78f0d50

Browse files
author
veasion
committed
新增通用查询 & 优化代码
1 parent d020bb5 commit 78f0d50

File tree

10 files changed

+312
-21
lines changed

10 files changed

+312
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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.4</version>
15+
<version>1.2.5</version>
1616
</dependency>
1717
```
1818
支持sql解析生成veasion-db代码

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.4</version>
10+
<version>1.2.5</version>
1111

1212
<name>veasion-db</name>
1313
<url>https://github.com/veasion/veasion-db</url>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package cn.veasion.db.criteria;
2+
3+
import cn.veasion.db.query.OrderParam;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* CommonQueryCriteria
12+
*
13+
* @author luozhuowei
14+
* @date 2023/1/7
15+
*/
16+
public class CommonQueryCriteria {
17+
18+
@AutoCriteria
19+
private Map<String, Object> filters;
20+
21+
private Integer page;
22+
private Integer size;
23+
24+
private List<OrderParam> orders;
25+
26+
public Map<String, Object> getFilters() {
27+
return filters;
28+
}
29+
30+
public void setFilters(Map<String, Object> filters) {
31+
this.filters = filters;
32+
}
33+
34+
public Integer getPage() {
35+
return page;
36+
}
37+
38+
public CommonQueryCriteria setPage(Integer page) {
39+
this.page = page;
40+
return this;
41+
}
42+
43+
public Integer getSize() {
44+
return size;
45+
}
46+
47+
public CommonQueryCriteria setSize(Integer size) {
48+
this.size = size;
49+
return this;
50+
}
51+
52+
public void setOrder(OrderParam orderParam) {
53+
if (orderParam != null) {
54+
if (orders == null) {
55+
orders = new ArrayList<>();
56+
}
57+
orders.add(orderParam);
58+
}
59+
}
60+
61+
public List<OrderParam> getOrders() {
62+
return orders;
63+
}
64+
65+
public void setOrders(List<OrderParam> orders) {
66+
this.orders = orders;
67+
}
68+
69+
public void withLike(String key) {
70+
withLike(key, true, true);
71+
}
72+
73+
public void withLike(String key, boolean left, boolean right) {
74+
Object v;
75+
if (filters != null && (v = filters.get(key)) != null) {
76+
String value = v.toString().trim();
77+
if ("".equals(value)) {
78+
return;
79+
}
80+
if (left && !value.startsWith("%")) {
81+
value = "%" + value;
82+
}
83+
if (right && !value.endsWith("%")) {
84+
value += "%";
85+
}
86+
filters.put(key, value);
87+
}
88+
}
89+
90+
public CommonQueryCriteria addFilter(String key, Object value) {
91+
if (filters == null) {
92+
filters = new HashMap<>();
93+
}
94+
filters.put(key, value);
95+
return this;
96+
}
97+
98+
public CommonQueryCriteria gte(String key, Object value) {
99+
return addFilter("start_" + key, value);
100+
}
101+
102+
public CommonQueryCriteria lte(String key, Object value) {
103+
return addFilter("end_" + key, value);
104+
}
105+
106+
public Object remove(String key) {
107+
if (filters != null) {
108+
return filters.remove(key);
109+
}
110+
return null;
111+
}
112+
113+
}

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

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

33
import cn.veasion.db.base.Page;
4+
import cn.veasion.db.criteria.CommonQueryCriteria;
45
import cn.veasion.db.query.AbstractQuery;
6+
import cn.veasion.db.query.EntityQuery;
57
import cn.veasion.db.query.Query;
8+
import cn.veasion.db.query.SubQuery;
69
import cn.veasion.db.update.AbstractUpdate;
710
import cn.veasion.db.update.BatchEntityInsert;
811
import cn.veasion.db.update.Delete;
@@ -14,8 +17,14 @@
1417
import java.lang.reflect.Field;
1518
import java.util.ArrayList;
1619
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.HashMap;
1722
import java.util.List;
1823
import java.util.Map;
24+
import java.util.function.Consumer;
25+
import java.util.function.Function;
26+
import java.util.stream.Collector;
27+
import java.util.stream.Collectors;
1928

2029
/**
2130
* EntityDao
@@ -32,7 +41,7 @@ default ID add(T entity) {
3241
ID add(EntityInsert entityInsert);
3342

3443
default ID[] batchAdd(List<T> entityList) {
35-
return batchAdd(entityList, 50);
44+
return batchAdd(entityList, 100);
3645
}
3746

3847
ID[] batchAdd(BatchEntityInsert batchEntityInsert);
@@ -65,23 +74,79 @@ default List<T> queryList(AbstractQuery<?> query) {
6574

6675
<E> List<E> queryList(AbstractQuery<?> query, Class<E> clazz);
6776

77+
<E> List<E> queryList(CommonQueryCriteria queryCriteria, Class<E> clazz, Consumer<EntityQuery> consumer);
78+
79+
default <E> List<E> queryList(CommonQueryCriteria queryCriteria, Class<E> clazz) {
80+
return queryList(queryCriteria, clazz, null);
81+
}
82+
83+
default List<T> queryList(CommonQueryCriteria queryCriteria) {
84+
return queryList(queryCriteria, getEntityClass());
85+
}
86+
6887
default Page<T> queryPage(AbstractQuery<?> query) {
6988
return queryPage(query, getEntityClass());
7089
}
7190

7291
<E> Page<E> queryPage(AbstractQuery<?> query, Class<E> clazz);
7392

93+
<E> Page<E> queryPage(CommonQueryCriteria queryCriteria, Class<E> clazz, Consumer<EntityQuery> consumer);
94+
95+
default <E> Page<E> queryPage(CommonQueryCriteria queryCriteria, Class<E> clazz) {
96+
return queryPage(queryCriteria, clazz, null);
97+
}
98+
99+
default Page<T> queryPage(CommonQueryCriteria queryCriteria) {
100+
return queryPage(queryCriteria, getEntityClass());
101+
}
102+
74103
default int updateById(T entity) {
75104
return update(new EntityUpdate(entity).eq(getIdField()).excludeUpdateFilterFields().skipNullField());
76105
}
77106

78107
int update(AbstractUpdate<?> update);
79108

109+
int delete(Delete delete);
110+
80111
default int deleteById(ID id) {
81-
return delete(new Delete().eq(getIdField(), id));
112+
return deleteByIds(Collections.singletonList(id));
82113
}
83114

84-
int delete(Delete delete);
115+
default int deleteByIds(List<ID> ids) {
116+
return delete(new Delete().in(getIdField(), ids));
117+
}
118+
119+
default int queryCount(AbstractQuery<?> query) {
120+
Integer count = queryForType(new SubQuery(query, "t").selectExpression("count(1)", "count"), Integer.class);
121+
return count != null ? count : 0;
122+
}
123+
124+
default <K, E, V> Map<K, V> groupQuery(AbstractQuery<?> query, Class<E> resultClass, Function<? super E, K> keyMapper, Function<? super E, V> valueMapper) {
125+
List<E> list = queryList(query, resultClass);
126+
if (list == null || list.isEmpty()) {
127+
return new HashMap<>();
128+
}
129+
return list.stream().collect(Collectors.toMap(keyMapper, valueMapper, (a, b) -> a));
130+
}
131+
132+
default <K, E> Map<K, List<E>> groupListQuery(AbstractQuery<?> query, Class<E> resultClass, Function<? super E, K> keyMapper) {
133+
List<E> list = queryList(query, resultClass);
134+
if (list == null || list.isEmpty()) {
135+
return new HashMap<>();
136+
}
137+
return list.stream().collect(Collectors.groupingBy(keyMapper));
138+
}
139+
140+
default <K, E, V> Map<K, List<V>> groupListQuery(AbstractQuery<?> query, Class<E> resultClass, Function<? super E, K> keyMapper, Function<? super E, V> valueMapper) {
141+
List<E> list = queryList(query, resultClass);
142+
if (list == null || list.isEmpty()) {
143+
return new HashMap<>();
144+
}
145+
return list.stream().collect(Collectors.groupingBy(keyMapper, Collector.of(ArrayList::new, (l, t) -> l.add(valueMapper.apply(t)), (a, b) -> {
146+
a.addAll(b);
147+
return a;
148+
})));
149+
}
85150

86151
@SuppressWarnings("unchecked")
87152
default ID[] batchAdd(List<T> entityList, int maxBatchSize) {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static int executeUpdate(Connection connection, String sql, Object... par
4040
try {
4141
ps = prepareStatement(connection, sql, params);
4242
count = ps.executeUpdate();
43-
LOGGER.info("<== Updates: {}", count);
43+
LOGGER.debug("<== Updates: {}", count);
4444
} catch (SQLException e) {
4545
throw new DbException("更新异常", e);
4646
} finally {
@@ -67,7 +67,7 @@ public static Object[] executeInsert(Connection connection, String sql, Object..
6767
keys.add(result.getObject(1));
6868
}
6969
}
70-
LOGGER.info("<== Updates: {}", count);
70+
LOGGER.debug("<== Updates: {}", count);
7171
} catch (SQLException e) {
7272
throw new DbException("新增异常", e);
7373
} finally {
@@ -85,7 +85,7 @@ public static int executeInsertNoKeys(Connection connection, String sql, Object.
8585
try {
8686
ps = prepareStatement(connection, sql, params);
8787
count = ps.executeUpdate();
88-
LOGGER.info("<== Updates: {}", count);
88+
LOGGER.debug("<== Updates: {}", count);
8989
} catch (SQLException e) {
9090
throw new DbException("新增异常", e);
9191
} finally {
@@ -136,7 +136,7 @@ public static List<Map<String, Object>> listForMap(Connection connection, boolea
136136
}
137137
list.add(map);
138138
}
139-
LOGGER.info("<== Total: {}", list.size());
139+
LOGGER.debug("<== Total: {}", list.size());
140140
} catch (SQLException e) {
141141
throw new DbException("查询异常", e);
142142
} finally {
@@ -227,7 +227,7 @@ public static <T> List<T> listForType(Connection connection, Class<T> clazz, Fie
227227
}
228228
list.add(obj);
229229
}
230-
LOGGER.info("<== Total: {}", list.size());
230+
LOGGER.debug("<== Total: {}", list.size());
231231
} catch (SQLException e) {
232232
throw new DbException("查询异常", e);
233233
} catch (RuntimeException e) {
@@ -281,7 +281,7 @@ public static Object queryOnly(Connection connection, String sql, Object... para
281281
while (rs.next()) {
282282
total++;
283283
}
284-
LOGGER.info("<== Total: {}", total);
284+
LOGGER.debug("<== Total: {}", total);
285285
} catch (SQLException e) {
286286
throw new DbException("查询异常", e);
287287
} finally {
@@ -296,9 +296,9 @@ private static PreparedStatement prepareStatement(Connection connection, String
296296
}
297297

298298
private static PreparedStatement prepareStatement(Connection connection, Integer autoGeneratedKeys, String sql, Object... params) throws SQLException {
299-
if (LOGGER.isInfoEnabled()) {
300-
LOGGER.info("==> Preparing: {}", sql);
301-
LOGGER.info("==> Parameters: {}", paramToString(params));
299+
if (LOGGER.isDebugEnabled()) {
300+
LOGGER.debug("==> Preparing: {}", sql);
301+
LOGGER.debug("==> Parameters: {}", paramToString(params));
302302
}
303303
PreparedStatement ps = autoGeneratedKeys == null ? connection.prepareStatement(sql) : connection.prepareStatement(sql, autoGeneratedKeys);
304304
if (params != null && params.length > 0) {

0 commit comments

Comments
 (0)