Skip to content

Commit

Permalink
新增通用查询 & 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
veasion committed Jan 7, 2023
1 parent d020bb5 commit 78f0d50
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ veasion-db 是一个轻量级持久层ORM框架,除slf4j-api外不依赖任何
<dependency>
<groupId>cn.veasion</groupId>
<artifactId>veasion-db</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
</dependency>
```
支持sql解析生成veasion-db代码
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>cn.veasion</groupId>
<artifactId>veasion-db</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>

<name>veasion-db</name>
<url>https://github.com/veasion/veasion-db</url>
Expand Down
113 changes: 113 additions & 0 deletions src/main/java/cn/veasion/db/criteria/CommonQueryCriteria.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cn.veasion.db.criteria;

import cn.veasion.db.query.OrderParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* CommonQueryCriteria
*
* @author luozhuowei
* @date 2023/1/7
*/
public class CommonQueryCriteria {

@AutoCriteria
private Map<String, Object> filters;

private Integer page;
private Integer size;

private List<OrderParam> orders;

public Map<String, Object> getFilters() {
return filters;
}

public void setFilters(Map<String, Object> filters) {
this.filters = filters;
}

public Integer getPage() {
return page;
}

public CommonQueryCriteria setPage(Integer page) {
this.page = page;
return this;
}

public Integer getSize() {
return size;
}

public CommonQueryCriteria setSize(Integer size) {
this.size = size;
return this;
}

public void setOrder(OrderParam orderParam) {
if (orderParam != null) {
if (orders == null) {
orders = new ArrayList<>();
}
orders.add(orderParam);
}
}

public List<OrderParam> getOrders() {
return orders;
}

public void setOrders(List<OrderParam> orders) {
this.orders = orders;
}

public void withLike(String key) {
withLike(key, true, true);
}

public void withLike(String key, boolean left, boolean right) {
Object v;
if (filters != null && (v = filters.get(key)) != null) {
String value = v.toString().trim();
if ("".equals(value)) {
return;
}
if (left && !value.startsWith("%")) {
value = "%" + value;
}
if (right && !value.endsWith("%")) {
value += "%";
}
filters.put(key, value);
}
}

public CommonQueryCriteria addFilter(String key, Object value) {
if (filters == null) {
filters = new HashMap<>();
}
filters.put(key, value);
return this;
}

public CommonQueryCriteria gte(String key, Object value) {
return addFilter("start_" + key, value);
}

public CommonQueryCriteria lte(String key, Object value) {
return addFilter("end_" + key, value);
}

public Object remove(String key) {
if (filters != null) {
return filters.remove(key);
}
return null;
}

}
71 changes: 68 additions & 3 deletions src/main/java/cn/veasion/db/jdbc/EntityDao.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cn.veasion.db.jdbc;

import cn.veasion.db.base.Page;
import cn.veasion.db.criteria.CommonQueryCriteria;
import cn.veasion.db.query.AbstractQuery;
import cn.veasion.db.query.EntityQuery;
import cn.veasion.db.query.Query;
import cn.veasion.db.query.SubQuery;
import cn.veasion.db.update.AbstractUpdate;
import cn.veasion.db.update.BatchEntityInsert;
import cn.veasion.db.update.Delete;
Expand All @@ -14,8 +17,14 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
* EntityDao
Expand All @@ -32,7 +41,7 @@ default ID add(T entity) {
ID add(EntityInsert entityInsert);

default ID[] batchAdd(List<T> entityList) {
return batchAdd(entityList, 50);
return batchAdd(entityList, 100);
}

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

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

<E> List<E> queryList(CommonQueryCriteria queryCriteria, Class<E> clazz, Consumer<EntityQuery> consumer);

default <E> List<E> queryList(CommonQueryCriteria queryCriteria, Class<E> clazz) {
return queryList(queryCriteria, clazz, null);
}

default List<T> queryList(CommonQueryCriteria queryCriteria) {
return queryList(queryCriteria, getEntityClass());
}

default Page<T> queryPage(AbstractQuery<?> query) {
return queryPage(query, getEntityClass());
}

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

<E> Page<E> queryPage(CommonQueryCriteria queryCriteria, Class<E> clazz, Consumer<EntityQuery> consumer);

default <E> Page<E> queryPage(CommonQueryCriteria queryCriteria, Class<E> clazz) {
return queryPage(queryCriteria, clazz, null);
}

default Page<T> queryPage(CommonQueryCriteria queryCriteria) {
return queryPage(queryCriteria, getEntityClass());
}

default int updateById(T entity) {
return update(new EntityUpdate(entity).eq(getIdField()).excludeUpdateFilterFields().skipNullField());
}

int update(AbstractUpdate<?> update);

int delete(Delete delete);

default int deleteById(ID id) {
return delete(new Delete().eq(getIdField(), id));
return deleteByIds(Collections.singletonList(id));
}

int delete(Delete delete);
default int deleteByIds(List<ID> ids) {
return delete(new Delete().in(getIdField(), ids));
}

default int queryCount(AbstractQuery<?> query) {
Integer count = queryForType(new SubQuery(query, "t").selectExpression("count(1)", "count"), Integer.class);
return count != null ? count : 0;
}

default <K, E, V> Map<K, V> groupQuery(AbstractQuery<?> query, Class<E> resultClass, Function<? super E, K> keyMapper, Function<? super E, V> valueMapper) {
List<E> list = queryList(query, resultClass);
if (list == null || list.isEmpty()) {
return new HashMap<>();
}
return list.stream().collect(Collectors.toMap(keyMapper, valueMapper, (a, b) -> a));
}

default <K, E> Map<K, List<E>> groupListQuery(AbstractQuery<?> query, Class<E> resultClass, Function<? super E, K> keyMapper) {
List<E> list = queryList(query, resultClass);
if (list == null || list.isEmpty()) {
return new HashMap<>();
}
return list.stream().collect(Collectors.groupingBy(keyMapper));
}

default <K, E, V> Map<K, List<V>> groupListQuery(AbstractQuery<?> query, Class<E> resultClass, Function<? super E, K> keyMapper, Function<? super E, V> valueMapper) {
List<E> list = queryList(query, resultClass);
if (list == null || list.isEmpty()) {
return new HashMap<>();
}
return list.stream().collect(Collectors.groupingBy(keyMapper, Collector.of(ArrayList::new, (l, t) -> l.add(valueMapper.apply(t)), (a, b) -> {
a.addAll(b);
return a;
})));
}

@SuppressWarnings("unchecked")
default ID[] batchAdd(List<T> entityList, int maxBatchSize) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/cn/veasion/db/jdbc/JdbcDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static int executeUpdate(Connection connection, String sql, Object... par
try {
ps = prepareStatement(connection, sql, params);
count = ps.executeUpdate();
LOGGER.info("<== Updates: {}", count);
LOGGER.debug("<== Updates: {}", count);
} catch (SQLException e) {
throw new DbException("更新异常", e);
} finally {
Expand All @@ -67,7 +67,7 @@ public static Object[] executeInsert(Connection connection, String sql, Object..
keys.add(result.getObject(1));
}
}
LOGGER.info("<== Updates: {}", count);
LOGGER.debug("<== Updates: {}", count);
} catch (SQLException e) {
throw new DbException("新增异常", e);
} finally {
Expand All @@ -85,7 +85,7 @@ public static int executeInsertNoKeys(Connection connection, String sql, Object.
try {
ps = prepareStatement(connection, sql, params);
count = ps.executeUpdate();
LOGGER.info("<== Updates: {}", count);
LOGGER.debug("<== Updates: {}", count);
} catch (SQLException e) {
throw new DbException("新增异常", e);
} finally {
Expand Down Expand Up @@ -136,7 +136,7 @@ public static List<Map<String, Object>> listForMap(Connection connection, boolea
}
list.add(map);
}
LOGGER.info("<== Total: {}", list.size());
LOGGER.debug("<== Total: {}", list.size());
} catch (SQLException e) {
throw new DbException("查询异常", e);
} finally {
Expand Down Expand Up @@ -227,7 +227,7 @@ public static <T> List<T> listForType(Connection connection, Class<T> clazz, Fie
}
list.add(obj);
}
LOGGER.info("<== Total: {}", list.size());
LOGGER.debug("<== Total: {}", list.size());
} catch (SQLException e) {
throw new DbException("查询异常", e);
} catch (RuntimeException e) {
Expand Down Expand Up @@ -281,7 +281,7 @@ public static Object queryOnly(Connection connection, String sql, Object... para
while (rs.next()) {
total++;
}
LOGGER.info("<== Total: {}", total);
LOGGER.debug("<== Total: {}", total);
} catch (SQLException e) {
throw new DbException("查询异常", e);
} finally {
Expand All @@ -296,9 +296,9 @@ private static PreparedStatement prepareStatement(Connection connection, String
}

private static PreparedStatement prepareStatement(Connection connection, Integer autoGeneratedKeys, String sql, Object... params) throws SQLException {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("==> Preparing: {}", sql);
LOGGER.info("==> Parameters: {}", paramToString(params));
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("==> Preparing: {}", sql);
LOGGER.debug("==> Parameters: {}", paramToString(params));
}
PreparedStatement ps = autoGeneratedKeys == null ? connection.prepareStatement(sql) : connection.prepareStatement(sql, autoGeneratedKeys);
if (params != null && params.length > 0) {
Expand Down
Loading

0 comments on commit 78f0d50

Please sign in to comment.