升级mybatis-jpa,更好的支持in操作
This commit is contained in:
parent
3e31d6fc26
commit
7187171173
@ -15,5 +15,5 @@ import java.lang.annotation.Target;
|
|||||||
public @interface ColumnMap {
|
public @interface ColumnMap {
|
||||||
String name(); // 对应数据库字段
|
String name(); // 对应数据库字段
|
||||||
String target(); // 映射表来源
|
String target(); // 映射表来源
|
||||||
String on(); // 映射关联字段
|
String on(); // 映射表字段
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,9 @@ import com.ruoyi.mybatis.enums.QueryEnum;
|
|||||||
public @interface Query {
|
public @interface Query {
|
||||||
QueryEnum operation() default QueryEnum.eq; // 操作符,如 eq, like, gt 等
|
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;
|
boolean params() default false;
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +1,15 @@
|
|||||||
package com.ruoyi.mybatis.domain;
|
package com.ruoyi.mybatis.domain;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
|
||||||
import com.ruoyi.common.core.domain.BaseEntity;
|
|
||||||
import com.ruoyi.mybatis.annotation.Column;
|
import com.ruoyi.mybatis.annotation.Column;
|
||||||
import com.ruoyi.mybatis.annotation.Query;
|
import com.ruoyi.mybatis.annotation.Query;
|
||||||
import com.ruoyi.mybatis.enums.QueryEnum;
|
import com.ruoyi.mybatis.utils.QueryUtil;
|
||||||
|
|
||||||
public class ColumnInfo {
|
public class ColumnInfo extends BaseColumnInfo {
|
||||||
private String columnName;
|
|
||||||
private String fieldName;
|
|
||||||
private Field field;
|
|
||||||
private Column column;
|
private Column column;
|
||||||
private Query query;
|
|
||||||
private String querySql;
|
|
||||||
|
|
||||||
public ColumnInfo(Field field) {
|
public ColumnInfo(Field field) {
|
||||||
this.field = field;
|
this.field = field;
|
||||||
@ -32,76 +25,12 @@ public class ColumnInfo {
|
|||||||
return columnName;
|
return columnName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTemplate() {
|
|
||||||
return getTemplate(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTemplate(boolean params) {
|
|
||||||
if (params) {
|
|
||||||
return "#{params." + fieldName + "}";
|
|
||||||
} else {
|
|
||||||
return "#{" + fieldName + "}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isPrimaryKey() {
|
public boolean isPrimaryKey() {
|
||||||
return this.column.primaryKey();
|
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<String, Object> 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) {
|
public String getQuerySql(Query query) {
|
||||||
if (query == null)
|
return QueryUtil.getQuerySql(this.getColumnName(), getTemplate(), query);
|
||||||
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());
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getQuerySql() {
|
public String getQuerySql() {
|
||||||
|
@ -6,19 +6,15 @@ import org.springframework.core.annotation.AnnotationUtils;
|
|||||||
|
|
||||||
import com.ruoyi.mybatis.annotation.ColumnMap;
|
import com.ruoyi.mybatis.annotation.ColumnMap;
|
||||||
import com.ruoyi.mybatis.annotation.Query;
|
import com.ruoyi.mybatis.annotation.Query;
|
||||||
|
import com.ruoyi.mybatis.utils.QueryUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库关联字段信息
|
* 数据库关联字段信息
|
||||||
*
|
*
|
||||||
* @author Dftre
|
* @author Dftre
|
||||||
*/
|
*/
|
||||||
public class MapColumnInfo {
|
public class MapColumnInfo extends BaseColumnInfo {
|
||||||
private String columnName;
|
|
||||||
private String fieldName;
|
|
||||||
private Field field;
|
|
||||||
private Query query;
|
|
||||||
private ColumnMap columnMap;
|
private ColumnMap columnMap;
|
||||||
private String querySql;
|
|
||||||
|
|
||||||
public MapColumnInfo(Field field) {
|
public MapColumnInfo(Field field) {
|
||||||
this.field = field;
|
this.field = field;
|
||||||
@ -38,57 +34,8 @@ public class MapColumnInfo {
|
|||||||
return columnName;
|
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) {
|
public String getQuerySql(Query query) {
|
||||||
if (query == null)
|
return QueryUtil.getQuerySql(this.getColumnName(), getTemplate(), query);
|
||||||
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());
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getQuerySql() {
|
public String getQuerySql() {
|
||||||
|
@ -142,6 +142,12 @@ public class TableInfo {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> List<MapColumnInfo> getNotNullMapColumnsForQuery(T entity) {
|
||||||
|
return this.mapColumns.stream()
|
||||||
|
.filter(column -> column.fieldQueryIsNotNull(entity))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
public <T> List<MapColumnInfo> getNotNullMapColumns(T entity) {
|
public <T> List<MapColumnInfo> getNotNullMapColumns(T entity) {
|
||||||
return this.mapColumns.stream()
|
return this.mapColumns.stream()
|
||||||
.filter(column -> column.fieldIsNotNull(entity))
|
.filter(column -> column.fieldIsNotNull(entity))
|
||||||
|
@ -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<String, Object> 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<String> 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<String> 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());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -41,13 +41,11 @@ public class SQLUtil {
|
|||||||
tableInfo.getJoinSql().stream()
|
tableInfo.getJoinSql().stream()
|
||||||
.filter(StringUtils::isNotEmpty)
|
.filter(StringUtils::isNotEmpty)
|
||||||
.forEach(sql::LEFT_OUTER_JOIN);
|
.forEach(sql::LEFT_OUTER_JOIN);
|
||||||
tableInfo.getNotNullMapColumns(entity).stream()
|
tableInfo.getNotNullMapColumnsForQuery(entity).stream()
|
||||||
.map(MapColumnInfo::getQuerySql)
|
.map(MapColumnInfo::getQuerySql)
|
||||||
.filter(StringUtils::isNotEmpty)
|
|
||||||
.forEach(sql::WHERE);
|
.forEach(sql::WHERE);
|
||||||
tableInfo.getNotNullColumnsForQuery(entity).stream()
|
tableInfo.getNotNullColumnsForQuery(entity).stream()
|
||||||
.map(ColumnInfo::getQuerySql)
|
.map(ColumnInfo::getQuerySql)
|
||||||
.filter(StringUtils::isNotEmpty)
|
|
||||||
.map(query -> tableInfo.getTableNameT() + "." + query)
|
.map(query -> tableInfo.getTableNameT() + "." + query)
|
||||||
.forEach(sql::WHERE);
|
.forEach(sql::WHERE);
|
||||||
if (tableInfo.hasDataScope()) {
|
if (tableInfo.hasDataScope()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user