Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf701b18fa | ||
|
|
ebaeb1ac56 | ||
|
|
45124eba81 | ||
|
|
4cd0b2338d | ||
|
|
35fa531be9 | ||
|
|
b690656848 | ||
|
|
6e5f33f641 | ||
|
|
53b8c4c23f | ||
|
|
f8beb03362 | ||
|
|
f5528fbfad | ||
|
|
90c97f211b | ||
|
|
42dee2e4ad | ||
|
|
66eb6108cd | ||
|
|
82f950e71d | ||
|
|
cceabb0c8d | ||
|
|
62de8e4885 | ||
|
|
9f7460e959 | ||
|
|
90d4c5e5f6 |
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
<groupId>com.imyeyu.spring</groupId>
|
<groupId>com.imyeyu.spring</groupId>
|
||||||
<artifactId>timi-spring</artifactId>
|
<artifactId>timi-spring</artifactId>
|
||||||
<version>0.0.20</version>
|
<version>0.0.18</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@@ -141,7 +141,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.imyeyu.io</groupId>
|
<groupId>com.imyeyu.io</groupId>
|
||||||
<artifactId>timi-io</artifactId>
|
<artifactId>timi-io</artifactId>
|
||||||
<version>0.0.6</version>
|
<version>0.0.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.imyeyu.java</groupId>
|
<groupId>com.imyeyu.java</groupId>
|
||||||
|
|||||||
@@ -37,6 +37,34 @@ public class Page<T> extends BasePage {
|
|||||||
/** 排序字段映射 */
|
/** 排序字段映射 */
|
||||||
protected LinkedHashMap<String, BaseMapper.OrderType> orderMap;
|
protected LinkedHashMap<String, BaseMapper.OrderType> orderMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建分页参数
|
||||||
|
*
|
||||||
|
* @param index 页码
|
||||||
|
* @param size 每页数量
|
||||||
|
*/
|
||||||
|
public Page(int index, int size) {
|
||||||
|
super(index, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取偏移量
|
||||||
|
*
|
||||||
|
* @return 偏移量
|
||||||
|
*/
|
||||||
|
public long getOffset() {
|
||||||
|
return (long) index * size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取限制数量
|
||||||
|
*
|
||||||
|
* @return 限制数量
|
||||||
|
*/
|
||||||
|
public long getLimit() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加排序字段
|
* 添加排序字段
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -82,21 +81,6 @@ public abstract class BaseSQLProvider {
|
|||||||
* @return SQL
|
* @return SQL
|
||||||
*/
|
*/
|
||||||
protected String buildSelectByPageSQL(EntityMeta meta, String tableName, Page<?> page, String offset, String limit) {
|
protected String buildSelectByPageSQL(EntityMeta meta, String tableName, Page<?> page, String offset, String limit) {
|
||||||
return buildSelectByPageSQL(meta, tableName, page, offset, limit, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建分页查询 SQL
|
|
||||||
*
|
|
||||||
* @param meta 实体元数据
|
|
||||||
* @param tableName 表名
|
|
||||||
* @param page 分页参数
|
|
||||||
* @param offset 偏移量占位符
|
|
||||||
* @param limit 限制数量占位符
|
|
||||||
* @param pageParameterPath Page 参数路径前缀
|
|
||||||
* @return SQL
|
|
||||||
*/
|
|
||||||
protected String buildSelectByPageSQL(EntityMeta meta, String tableName, Page<?> page, String offset, String limit, String pageParameterPath) {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append("SELECT %s FROM `%s` WHERE 1 = 1".formatted(meta.selectPageClause, tableName));
|
sql.append("SELECT %s FROM `%s` WHERE 1 = 1".formatted(meta.selectPageClause, tableName));
|
||||||
|
|
||||||
@@ -104,11 +88,7 @@ public abstract class BaseSQLProvider {
|
|||||||
appendSoftDeleteCondition(sql, meta);
|
appendSoftDeleteCondition(sql, meta);
|
||||||
|
|
||||||
// 添加 Page 查询条件
|
// 添加 Page 查询条件
|
||||||
if (pageParameterPath.isEmpty()) {
|
appendPageConditions(sql, page);
|
||||||
appendPageConditions(sql, page);
|
|
||||||
} else {
|
|
||||||
appendPageConditions(sql, page, pageParameterPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加排序
|
// 添加排序
|
||||||
appendOrderBy(sql, meta, page);
|
appendOrderBy(sql, meta, page);
|
||||||
@@ -127,19 +107,6 @@ public abstract class BaseSQLProvider {
|
|||||||
* @return SQL
|
* @return SQL
|
||||||
*/
|
*/
|
||||||
protected String buildCountByPageSQL(EntityMeta meta, String tableName, Page<?> page) {
|
protected String buildCountByPageSQL(EntityMeta meta, String tableName, Page<?> page) {
|
||||||
return buildCountByPageSQL(meta, tableName, page, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建分页统计 SQL
|
|
||||||
*
|
|
||||||
* @param meta 实体元数据
|
|
||||||
* @param tableName 表名
|
|
||||||
* @param page 分页参数
|
|
||||||
* @param pageParameterPath Page 参数路径前缀
|
|
||||||
* @return SQL
|
|
||||||
*/
|
|
||||||
protected String buildCountByPageSQL(EntityMeta meta, String tableName, Page<?> page, String pageParameterPath) {
|
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append("SELECT COUNT(*) FROM `%s` WHERE 1 = 1".formatted(tableName));
|
sql.append("SELECT COUNT(*) FROM `%s` WHERE 1 = 1".formatted(tableName));
|
||||||
|
|
||||||
@@ -147,11 +114,7 @@ public abstract class BaseSQLProvider {
|
|||||||
appendSoftDeleteCondition(sql, meta);
|
appendSoftDeleteCondition(sql, meta);
|
||||||
|
|
||||||
// 添加 Page 查询条件
|
// 添加 Page 查询条件
|
||||||
if (pageParameterPath.isEmpty()) {
|
appendPageConditions(sql, page);
|
||||||
appendPageConditions(sql, page);
|
|
||||||
} else {
|
|
||||||
appendPageConditions(sql, page, pageParameterPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
@@ -175,82 +138,30 @@ public abstract class BaseSQLProvider {
|
|||||||
* @param page 分页参数
|
* @param page 分页参数
|
||||||
*/
|
*/
|
||||||
protected void appendPageConditions(StringBuilder sql, Page<?> page) {
|
protected void appendPageConditions(StringBuilder sql, Page<?> page) {
|
||||||
appendPageConditions(sql, page, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加 Page 查询条件
|
|
||||||
*
|
|
||||||
* @param sql SQL 构建器
|
|
||||||
* @param page 分页参数
|
|
||||||
* @param pageParameterPath Page 参数路径前缀
|
|
||||||
*/
|
|
||||||
protected void appendPageConditions(StringBuilder sql, Page<?> page, String pageParameterPath) {
|
|
||||||
// 精准查询
|
// 精准查询
|
||||||
appendPageExampleConditions(
|
if (TimiJava.isNotEmpty(page.getEqualsExample())) {
|
||||||
sql,
|
Object obj = page.getEqualsExample();
|
||||||
page.getEqualsExample(),
|
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
||||||
page.getEqualsLogic(),
|
String conditionClause = metaExample.fieldColumnList.stream()
|
||||||
false,
|
.filter(fc -> fc.isNotEmpty(obj))
|
||||||
pageParameterPath
|
.map(fc -> "`%s` = '%s'".formatted(fc.columnName, fc.getAsString(obj)))
|
||||||
);
|
.collect(Collectors.joining(" %s ".formatted(page.getEqualsLogic())));
|
||||||
|
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||||
|
sql.append(" AND ").append(conditionClause);
|
||||||
|
}
|
||||||
|
}
|
||||||
// 模糊查询
|
// 模糊查询
|
||||||
appendPageExampleConditions(
|
if (TimiJava.isNotEmpty(page.getLikesExample())) {
|
||||||
sql,
|
Object obj = page.getLikesExample();
|
||||||
page.getLikesExample(),
|
EntityMeta metaExample = getEntityMeta(obj.getClass());
|
||||||
page.getLikesLogic(),
|
String conditionClause = metaExample.fieldColumnList.stream()
|
||||||
true,
|
.filter(fc -> fc.isNotEmpty(obj))
|
||||||
pageParameterPath
|
.map(fc -> "`%s` LIKE CONCAT('%%', '%s', '%%')".formatted(fc.columnName, fc.getAsString(obj)))
|
||||||
);
|
.collect(Collectors.joining(" %s ".formatted(page.getLikesLogic())));
|
||||||
}
|
if (TimiJava.isNotEmpty(conditionClause)) {
|
||||||
|
sql.append(" AND (").append(conditionClause).append(')');
|
||||||
/**
|
}
|
||||||
* 添加一组 Page 示例条件
|
|
||||||
*
|
|
||||||
* @param sql SQL 构建器
|
|
||||||
* @param example 示例对象
|
|
||||||
* @param logic 条件连接逻辑
|
|
||||||
* @param likes 是否使用模糊匹配
|
|
||||||
* @param pageParameterPath Page 参数路径前缀
|
|
||||||
*/
|
|
||||||
private void appendPageExampleConditions(StringBuilder sql, Object example, Logic logic, boolean likes, String pageParameterPath) {
|
|
||||||
if (TimiJava.isEmpty(example)) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
EntityMeta metaExample = getEntityMeta(example.getClass());
|
|
||||||
String parameterPrefix = "%s%s.".formatted(pageParameterPath, likes ? "likesExample" : "equalsExample");
|
|
||||||
String conditionClause = metaExample.fieldColumnList.stream()
|
|
||||||
.map(fc -> buildPageCondition(fc, example, likes, parameterPrefix))
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.collect(Collectors.joining(" %s ".formatted(logic == null ? Logic.AND : logic)));
|
|
||||||
if (TimiJava.isNotEmpty(conditionClause)) {
|
|
||||||
sql.append(" AND (").append(conditionClause).append(')');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建单个 Page 示例条件
|
|
||||||
*
|
|
||||||
* @param fieldColumn 字段元数据
|
|
||||||
* @param example 示例对象
|
|
||||||
* @param likes 是否使用模糊匹配
|
|
||||||
* @param parameterPrefix 参数路径前缀
|
|
||||||
* @return 条件子句;字段值为空时返回 null
|
|
||||||
*/
|
|
||||||
private String buildPageCondition(FieldColumn fieldColumn, Object example, boolean likes, String parameterPrefix) {
|
|
||||||
Object value = fieldColumn.getValue(example);
|
|
||||||
if (TimiJava.isEmpty(value)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (value instanceof Boolean booleanValue) {
|
|
||||||
return "`%s` IS %s".formatted(fieldColumn.columnName, booleanValue ? "TRUE" : "FALSE");
|
|
||||||
}
|
|
||||||
String parameter = "#{%s%s}".formatted(parameterPrefix, fieldColumn.fieldName);
|
|
||||||
if (likes) {
|
|
||||||
return "`%s` LIKE CONCAT('%%', %s, '%%')".formatted(fieldColumn.columnName, parameter);
|
|
||||||
}
|
|
||||||
return "`%s` = %s".formatted(fieldColumn.columnName, parameter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -737,7 +648,11 @@ public abstract class BaseSQLProvider {
|
|||||||
* @return true 为 null
|
* @return true 为 null
|
||||||
*/
|
*/
|
||||||
public boolean isNull(Object entity) {
|
public boolean isNull(Object entity) {
|
||||||
return getValue(entity) == null;
|
try {
|
||||||
|
return Ref.getFieldValue(entity, field, Object.class) == null;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -757,7 +672,11 @@ public abstract class BaseSQLProvider {
|
|||||||
* @return true 为空
|
* @return true 为空
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty(Object entity) {
|
public boolean isEmpty(Object entity) {
|
||||||
return TimiJava.isEmpty(getValue(entity));
|
try {
|
||||||
|
return TimiJava.isEmpty(Ref.getFieldValue(entity, field, Object.class));
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -777,18 +696,8 @@ public abstract class BaseSQLProvider {
|
|||||||
* @return 字符串值
|
* @return 字符串值
|
||||||
*/
|
*/
|
||||||
public String getAsString(Object obj) {
|
public String getAsString(Object obj) {
|
||||||
return getValue(obj).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取字段值
|
|
||||||
*
|
|
||||||
* @param entity 实体
|
|
||||||
* @return 字段值
|
|
||||||
*/
|
|
||||||
public Object getValue(Object entity) {
|
|
||||||
try {
|
try {
|
||||||
return Ref.getFieldValue(entity, field, Object.class);
|
return field.get(obj).toString();
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
|
|||||||
*/
|
*/
|
||||||
public String selectByPage(ProviderContext context, @Param("tableName") String tableName, @Param("page") Page<?> page) {
|
public String selectByPage(ProviderContext context, @Param("tableName") String tableName, @Param("page") Page<?> page) {
|
||||||
EntityMeta meta = getEntityMeta(context);
|
EntityMeta meta = getEntityMeta(context);
|
||||||
return buildSelectByPageSQL(meta, tableName, page, "#{page.offset}", "#{page.limit}", "page.");
|
return buildSelectByPageSQL(meta, tableName, page, "#{page.offset}", "#{page.limit}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,7 +38,7 @@ public class DynamicTableSQLProvider extends BaseSQLProvider {
|
|||||||
*/
|
*/
|
||||||
public String countByPage(ProviderContext context, @Param("tableName") String tableName, @Param("page") Page<?> page) {
|
public String countByPage(ProviderContext context, @Param("tableName") String tableName, @Param("page") Page<?> page) {
|
||||||
EntityMeta meta = getEntityMeta(context);
|
EntityMeta meta = getEntityMeta(context);
|
||||||
return buildCountByPageSQL(meta, tableName, page, "page.");
|
return buildCountByPageSQL(meta, tableName, page);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class SQLProvider extends BaseSQLProvider {
|
|||||||
*/
|
*/
|
||||||
public String selectByPage(ProviderContext context, @Param("page") Page<?> page) {
|
public String selectByPage(ProviderContext context, @Param("page") Page<?> page) {
|
||||||
EntityMeta meta = getEntityMeta(context);
|
EntityMeta meta = getEntityMeta(context);
|
||||||
return buildSelectByPageSQL(meta, meta.table, page, "#{offset}", "#{limit}", "");
|
return buildSelectByPageSQL(meta, meta.table, page, "#{offset}", "#{limit}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,7 +42,7 @@ public class SQLProvider extends BaseSQLProvider {
|
|||||||
*/
|
*/
|
||||||
public String countByPage(ProviderContext context, @Param("page") Page<?> page) {
|
public String countByPage(ProviderContext context, @Param("page") Page<?> page) {
|
||||||
EntityMeta meta = getEntityMeta(context);
|
EntityMeta meta = getEntityMeta(context);
|
||||||
return buildCountByPageSQL(meta, meta.table, page, "");
|
return buildCountByPageSQL(meta, meta.table, page);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user