diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java index 7b19232..e451e72 100644 --- a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java @@ -15,5 +15,5 @@ import java.lang.annotation.Target; public @interface ColumnMap { String name(); // 对应数据库字段 String target(); // 映射表来源 - String on(); // 映射关联字段 + String on(); // 映射表字段 } diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java index 0eeda71..458c211 100644 --- a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java @@ -17,7 +17,9 @@ import com.ruoyi.mybatis.enums.QueryEnum; public @interface Query { QueryEnum operation() default QueryEnum.eq; // 操作符,如 eq, like, gt 等 - String[] section() default { "begin", "end" }; + String[] sections() default { "begin", "end" }; + + String section() default "section"; boolean params() default false; diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/BaseColumnInfo.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/BaseColumnInfo.java new file mode 100644 index 0000000..41b51c5 --- /dev/null +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/BaseColumnInfo.java @@ -0,0 +1,38 @@ +package com.ruoyi.mybatis.domain; + +import java.lang.reflect.Field; + +import com.ruoyi.mybatis.annotation.Query; +import com.ruoyi.mybatis.utils.QueryUtil; + +public class BaseColumnInfo { + protected String columnName; + protected String fieldName; + protected Field field; + protected Query query; + protected String querySql; + + public String getTemplate() { + return getTemplate(false); + } + + public String getTemplate(boolean params) { + if (params) { + return "#{params." + fieldName + "}"; + } else { + return "#{" + fieldName + "}"; + } + } + + public boolean fieldQueryIsNotNull(Object entity) { + return QueryUtil.fieldQueryIsNotNull(entity, field, query); + } + + public boolean fieldIsNotNull(Object entity) { + try { + return this.field.get(entity) != null; + } catch (IllegalAccessException e) { + throw new RuntimeException("Failed to access field for building query conditions.", e); + } + } +} diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java index 1f588eb..882f7cb 100644 --- a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java @@ -1,22 +1,15 @@ package com.ruoyi.mybatis.domain; import java.lang.reflect.Field; -import java.util.Map; import org.springframework.core.annotation.AnnotationUtils; -import com.ruoyi.common.core.domain.BaseEntity; import com.ruoyi.mybatis.annotation.Column; import com.ruoyi.mybatis.annotation.Query; -import com.ruoyi.mybatis.enums.QueryEnum; +import com.ruoyi.mybatis.utils.QueryUtil; -public class ColumnInfo { - private String columnName; - private String fieldName; - private Field field; +public class ColumnInfo extends BaseColumnInfo { private Column column; - private Query query; - private String querySql; public ColumnInfo(Field field) { this.field = field; @@ -32,76 +25,12 @@ public class ColumnInfo { return columnName; } - public String getTemplate() { - return getTemplate(false); - } - - public String getTemplate(boolean params) { - if (params) { - return "#{params." + fieldName + "}"; - } else { - return "#{" + fieldName + "}"; - } - } - public boolean isPrimaryKey() { return this.column.primaryKey(); } - public boolean fieldQueryIsNotNull(Object entity) { - try { - if (this.query != null && this.query.operation().equals(QueryEnum.between)) { - BaseEntity baseEntity = (BaseEntity) entity; - Map map = baseEntity.getParams(); - return map.get(this.query.section()[0]) != null && map.get(this.query.section()[1]) != null; - } else { - return this.field.get(entity) != null; - } - } catch (IllegalAccessException e) { - throw new RuntimeException("Failed to access field for building query conditions.", e); - } - } - - public boolean fieldIsNotNull(Object entity) { - try { - return this.field.get(entity) != null; - } catch (IllegalAccessException e) { - throw new RuntimeException("Failed to access field for building query conditions.", e); - } - } - public String getQuerySql(Query query) { - if (query == null) - return ""; - String column = this.getColumnName(); - String iField = getTemplate(); - String begin = "#{params." + query.section()[0] + "}"; - String end = "#{params." + query.section()[1] + "}"; - return switch (query.operation()) { - case eq -> column + " = " + iField; - case ne -> column + " <> " + iField; - case gt -> column + " > " + iField; - case ge -> column + " >= " + iField; - case le -> column + " < " + iField; - case lt -> column + " <= " + iField; - case between -> column + " between " + begin + " and " + end; - case notBetween -> column + " not between " + begin + " and " + end; - case like -> column + " like concat('%'," + iField + ",'%')"; - case notLike -> column + "not like concat('%'," + iField + ",'%')"; - case likeLeft -> column + "like concat('%'," + iField + ")"; - case likeRight -> column + "like concat(" + iField + ",'%')"; - case notLikeLeft -> column + "not like concat('%'," + iField + ")"; - case notLikeRight -> column + "not like concat(" + iField + ",'%')"; - case isNull -> column + " is null"; - case isNotNull -> column + " is not null"; - case in -> column + " in (" + iField + ""; - case notIn -> column + " not in (" + iField + ""; - case inSql -> column + " in (" + query.sql() + ")"; - case notInSql -> column + " not in (" + query.sql() + ")"; - default -> throw new IllegalArgumentException( - "Unsupported operation: " + query.operation()); - }; - + return QueryUtil.getQuerySql(this.getColumnName(), getTemplate(), query); } public String getQuerySql() { diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java index a056011..2b266b3 100644 --- a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java @@ -6,19 +6,15 @@ import org.springframework.core.annotation.AnnotationUtils; import com.ruoyi.mybatis.annotation.ColumnMap; import com.ruoyi.mybatis.annotation.Query; +import com.ruoyi.mybatis.utils.QueryUtil; /** * 数据库关联字段信息 * * @author Dftre */ -public class MapColumnInfo { - private String columnName; - private String fieldName; - private Field field; - private Query query; +public class MapColumnInfo extends BaseColumnInfo { private ColumnMap columnMap; - private String querySql; public MapColumnInfo(Field field) { this.field = field; @@ -38,57 +34,8 @@ public class MapColumnInfo { return columnName; } - public String getTemplate() { - return getTemplate(false); - } - - public String getTemplate(boolean params) { - if (params) { - return "#{params." + fieldName + "}"; - } else { - return "#{" + fieldName + "}"; - } - } - - public boolean fieldIsNotNull(Object entity) { - try { - return this.field.get(entity) != null; - } catch (IllegalAccessException e) { - throw new RuntimeException("Failed to access field for building query conditions.", e); - } - } - public String getQuerySql(Query query) { - if (query == null) - return ""; - String column = this.getColumnName(); - String iField = getTemplate(); - String begin = "#{params." + query.section()[0] + "}"; - String end = "#{params." + query.section()[1] + "}"; - return switch (query.operation()) { - case eq -> column + " = " + iField; - case ne -> column + " <> " + iField; - case gt -> column + " > " + iField; - case ge -> column + " >= " + iField; - case le -> column + " < " + iField; - case lt -> column + " <= " + iField; - case between -> column + " between " + begin + " and " + end; - case notBetween -> column + " not between " + begin + " and " + end; - case like -> column + " like concat('%'," + iField + ",'%')"; - case notLike -> column + "not like concat('%'," + iField + ",'%')"; - case likeLeft -> column + "like concat('%'," + iField + ")"; - case likeRight -> column + "like concat(" + iField + ",'%')"; - case notLikeLeft -> column + "not like concat('%'," + iField + ")"; - case notLikeRight -> column + "not like concat(" + iField + ",'%')"; - case isNull -> column + " is null"; - case isNotNull -> column + " is not null"; - case in -> column + " in (" + iField + ""; - case notIn -> column + " not in (" + iField + ""; - case inSql -> column + " in (" + query.sql() + ")"; - case notInSql -> column + " not in (" + query.sql() + ")"; - default -> throw new IllegalArgumentException( - "Unsupported operation: " + query.operation()); - }; + return QueryUtil.getQuerySql(this.getColumnName(), getTemplate(), query); } public String getQuerySql() { diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java index 37fa63c..d8228c6 100644 --- a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java @@ -142,6 +142,12 @@ public class TableInfo { .collect(Collectors.toList()); } + public List getNotNullMapColumnsForQuery(T entity) { + return this.mapColumns.stream() + .filter(column -> column.fieldQueryIsNotNull(entity)) + .collect(Collectors.toList()); + } + public List getNotNullMapColumns(T entity) { return this.mapColumns.stream() .filter(column -> column.fieldIsNotNull(entity)) diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/QueryUtil.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/QueryUtil.java new file mode 100644 index 0000000..675bc58 --- /dev/null +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/QueryUtil.java @@ -0,0 +1,86 @@ +package com.ruoyi.mybatis.utils; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import com.ruoyi.common.core.domain.BaseEntity; +import com.ruoyi.mybatis.annotation.Query; +import com.ruoyi.mybatis.enums.QueryEnum; + +public class QueryUtil { + + public static boolean fieldQueryIsNotNull(Object entity, Field field, Query query) { + try { + if (query != null) { + BaseEntity baseEntity = (BaseEntity) entity; + Map map = baseEntity.getParams(); + if (query.operation().equals(QueryEnum.between)) { + return map.get(query.sections()[0]) != null && map.get(query.sections()[1]) != null; + } else if (query.operation().equals(QueryEnum.in)) { + Object obj = map.get(query.section()); + if (obj == null) { + return false; + } else { + if (obj instanceof String) { + List list = new ArrayList<>(); + for (String split : ((String) obj).split(",")) { + list.add("\"" + split + "\""); + } + map.put(query.section() + "_operation", String.join(",", list)); + return true; + } else if (obj instanceof Collection) { + List list = new ArrayList<>(); + for (Object split : ((Collection) obj)) { + list.add("\"" + split.toString() + "\""); + } + map.put(query.section() + "_operation", String.join(",", list)); + return true; + } else { + return false; + } + } + + } + } + return field.get(entity) != null; + } catch (IllegalAccessException e) { + throw new RuntimeException("Failed to access field for building query conditions.", e); + } + } + + public static String getQuerySql(String column, String iField, Query query) { + if (query == null) + return ""; + String begin = "#{params." + query.sections()[0] + "}"; + String end = "#{params." + query.sections()[1] + "}"; + String inParams = "${params." + query.section() + "_operation" + "}"; + return switch (query.operation()) { + case eq -> column + " = " + iField; + case ne -> column + " <> " + iField; + case gt -> column + " > " + iField; + case ge -> column + " >= " + iField; + case le -> column + " < " + iField; + case lt -> column + " <= " + iField; + case between -> column + " between " + begin + " and " + end; + case notBetween -> column + " not between " + begin + " and " + end; + case like -> column + " like concat('%'," + iField + ",'%')"; + case notLike -> column + "not like concat('%'," + iField + ",'%')"; + case likeLeft -> column + "like concat('%'," + iField + ")"; + case likeRight -> column + "like concat(" + iField + ",'%')"; + case notLikeLeft -> column + "not like concat('%'," + iField + ")"; + case notLikeRight -> column + "not like concat(" + iField + ",'%')"; + case isNull -> column + " is null"; + case isNotNull -> column + " is not null"; + case in -> column + " in (" + inParams + ")"; + case notIn -> column + " not in (" + inParams + ")"; + case inSql -> column + " in (" + query.sql() + ")"; + case notInSql -> column + " not in (" + query.sql() + ")"; + default -> throw new IllegalArgumentException( + "Unsupported operation: " + query.operation()); + }; + } + +} diff --git a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java index ad179ff..b34899a 100644 --- a/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java +++ b/ruoyi-plugins/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java @@ -41,13 +41,11 @@ public class SQLUtil { tableInfo.getJoinSql().stream() .filter(StringUtils::isNotEmpty) .forEach(sql::LEFT_OUTER_JOIN); - tableInfo.getNotNullMapColumns(entity).stream() + tableInfo.getNotNullMapColumnsForQuery(entity).stream() .map(MapColumnInfo::getQuerySql) - .filter(StringUtils::isNotEmpty) .forEach(sql::WHERE); tableInfo.getNotNullColumnsForQuery(entity).stream() .map(ColumnInfo::getQuerySql) - .filter(StringUtils::isNotEmpty) .map(query -> tableInfo.getTableNameT() + "." + query) .forEach(sql::WHERE); if (tableInfo.hasDataScope()) {