feat: add QueryWrapper.create(entity)

This commit is contained in:
开源海哥 2023-08-26 15:43:54 +08:00
parent 23c0a0eba5
commit 7a13d28a67
2 changed files with 26 additions and 9 deletions

View File

@ -37,13 +37,26 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
* 根据实体类对象构建查询条件
*
* @param entity 实体类对象
* @return 查询对象
* @return 查询对象 QueryWrapper
*/
public static QueryWrapper create(Object entity) {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass()));
return tableInfo.buildQueryWrapper(entity);
return tableInfo.buildQueryWrapper(entity, null);
}
/**
* 根据实体类构建查询条件
*
* @param entity 实体类对象
* @param operators 每个属性对应的操作符
* @return 查询对象 QueryWrapper
*/
public static QueryWrapper create(Object entity, Map<String, SqlOperator> operators) {
TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass()));
return tableInfo.buildQueryWrapper(entity, operators);
}
@SuppressWarnings("unchecked")
public <Q extends QueryWrapper> WithBuilder<Q> with(String name) {
if (with == null) {
@ -657,16 +670,16 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
public QueryWrapper limit(Number rows) {
if (rows != null) {
setLimitRows(rows.longValue());
}else {
} else {
setLimitRows(null);
}
return this;
}
public QueryWrapper offset(Number offset) {
if (offset!= null) {
if (offset != null) {
setLimitOffset(offset.longValue());
}else {
} else {
setLimitOffset(null);
}
return this;
@ -784,7 +797,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
/**
* 获取 queryWrapper 的参数
* 在构建 sql 的时候需要保证 where having 的前面
@ -818,7 +830,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
}
/**
* 获取 queryWrapper 的参数
* 在构建 sql 的时候需要保证 where having 的前面

View File

@ -19,6 +19,7 @@ import com.mybatisflex.annotation.*;
import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.constant.SqlConsts;
import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
@ -922,7 +923,7 @@ public class TableInfo {
}
public QueryWrapper buildQueryWrapper(Object entity) {
public QueryWrapper buildQueryWrapper(Object entity, Map<String, SqlOperator> operators) {
QueryColumn[] queryColumns = new QueryColumn[defaultQueryColumns.length];
for (int i = 0; i < defaultQueryColumns.length; i++) {
queryColumns[i] = columnQueryMapping.get(defaultQueryColumns[i]);
@ -941,7 +942,12 @@ public class TableInfo {
Object value = metaObject.getValue(property);
if (value != null && !"".equals(value)) {
QueryColumn queryColumn = buildQueryColumn(column);
queryWrapper.and(queryColumn.eq(value));
if (operators != null && operators.containsKey(property)) {
SqlOperator operator = operators.get(property);
queryWrapper.and(QueryCondition.create(queryColumn, operator, value));
} else {
queryWrapper.and(queryColumn.eq(value));
}
}
});
return queryWrapper;