diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 1718a64e..3ca3d6a2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -37,13 +37,26 @@ public class QueryWrapper extends BaseQueryWrapper { * 根据实体类对象,构建查询条件 * * @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 operators) { + TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass())); + return tableInfo.buildQueryWrapper(entity, operators); + } + + @SuppressWarnings("unchecked") public WithBuilder with(String name) { if (with == null) { @@ -657,16 +670,16 @@ public class QueryWrapper extends BaseQueryWrapper { 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 的参数 * 在构建 sql 的时候,需要保证 where 在 having 的前面 @@ -818,7 +830,6 @@ public class QueryWrapper extends BaseQueryWrapper { } - /** * 获取 queryWrapper 的参数 * 在构建 sql 的时候,需要保证 where 在 having 的前面 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 0c14fd53..ab7e76ed 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -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 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;