升级mybatis-jpa,更好的支持in操作

This commit is contained in:
Dftre 2024-05-17 17:24:46 +08:00
parent 3e31d6fc26
commit 7187171173
8 changed files with 141 additions and 135 deletions

View File

@ -15,5 +15,5 @@ import java.lang.annotation.Target;
public @interface ColumnMap {
String name(); // 对应数据库字段
String target(); // 映射表来源
String on(); // 映射关联字段
String on(); // 映射字段
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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<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) {
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() {

View File

@ -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() {

View File

@ -142,6 +142,12 @@ public class TableInfo {
.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) {
return this.mapColumns.stream()
.filter(column -> column.fieldIsNotNull(entity))

View File

@ -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());
};
}
}

View File

@ -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()) {