解决区间查询字段无法插入的问题

This commit is contained in:
Dftre 2024-05-07 12:51:50 +08:00
parent 0d214a5b78
commit 9db38aea58
4 changed files with 36 additions and 16 deletions

View File

@ -5,14 +5,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* 标注数据库
*
* @author Dftre
*/
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface Table { public @interface Table {
String name(); String name();
String[] columns() default {}; String[] columns() default {};
String[] orderBy() default {};
} }

View File

@ -1,17 +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;
/**
* 数据库字段信息
*
* @author Dftre
*/
public class ColumnInfo { public class ColumnInfo {
private String columnName; private String columnName;
private String fieldName; private String fieldName;
@ -50,6 +48,20 @@ public class ColumnInfo {
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) { public boolean fieldIsNotNull(Object entity) {
try { try {
return this.field.get(entity) != null; return this.field.get(entity) != null;

View File

@ -75,6 +75,10 @@ public class TableInfo {
} }
} }
public String[] getOrderBy() {
return this.table.orderBy();
}
public boolean hasDataScope() { public boolean hasDataScope() {
return this.hasDataScopeValue; return this.hasDataScopeValue;
} }
@ -126,6 +130,12 @@ public class TableInfo {
return mapColumns; return mapColumns;
} }
public <T> List<ColumnInfo> getNotNullColumnsForQuery(T entity) {
return this.columns.stream()
.filter(column -> column.fieldQueryIsNotNull(entity))
.collect(Collectors.toList());
}
public <T> List<ColumnInfo> getNotNullColumns(T entity) { public <T> List<ColumnInfo> getNotNullColumns(T entity) {
return this.columns.stream() return this.columns.stream()
.filter(column -> column.fieldIsNotNull(entity)) .filter(column -> column.fieldIsNotNull(entity))

View File

@ -1,6 +1,7 @@
package com.ruoyi.mybatis.utils; package com.ruoyi.mybatis.utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -41,7 +42,7 @@ public class SQLUtil {
tableInfo.getNotNullMapColumns(entity).stream() tableInfo.getNotNullMapColumns(entity).stream()
.map(MapColumnInfo::getQuerySql) .map(MapColumnInfo::getQuerySql)
.forEach(sql::WHERE); .forEach(sql::WHERE);
tableInfo.getNotNullColumns(entity).stream() tableInfo.getNotNullColumnsForQuery(entity).stream()
.map(ColumnInfo::getQuerySql) .map(ColumnInfo::getQuerySql)
.map(query -> tableInfo.getTableNameT() + "." + query) .map(query -> tableInfo.getTableNameT() + "." + query)
.forEach(sql::WHERE); .forEach(sql::WHERE);
@ -49,10 +50,15 @@ public class SQLUtil {
sql.WHERE("1=1 ${params.dataScope}"); sql.WHERE("1=1 ${params.dataScope}");
} }
Arrays.stream(tableInfo.getOrderBy())
.map(order -> tableInfo.getTableNameT() + "." + order)
.forEach(sql::ORDER_BY);
} else { } else {
tableInfo.getNotNullColumns(entity).stream() tableInfo.getNotNullColumnsForQuery(entity).stream()
.map(ColumnInfo::getQuerySql) .map(ColumnInfo::getQuerySql)
.forEach(sql::WHERE); .forEach(sql::WHERE);
Arrays.stream(tableInfo.getOrderBy())
.forEach(sql::ORDER_BY);
} }
return sql.toString(); return sql.toString();
} }
@ -109,10 +115,6 @@ public class SQLUtil {
.map(column -> tableInfo.getTableNameT() + "." + column.getColumnName() + " = " .map(column -> tableInfo.getTableNameT() + "." + column.getColumnName() + " = "
+ column.getTemplate()) + column.getTemplate())
.forEach(sql::WHERE); .forEach(sql::WHERE);
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
} else { } else {
tableInfo.getPrimaryKeys().stream() tableInfo.getPrimaryKeys().stream()
.map(column -> column.getColumnName() + " = " + column.getTemplate()) .map(column -> column.getColumnName() + " = " + column.getTemplate())