mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
feat: add QueryWrapper.create(entity)
This commit is contained in:
parent
23c0a0eba5
commit
7a13d28a67
@ -37,13 +37,26 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
* 根据实体类对象,构建查询条件
|
* 根据实体类对象,构建查询条件
|
||||||
*
|
*
|
||||||
* @param entity 实体类对象
|
* @param entity 实体类对象
|
||||||
* @return 查询对象
|
* @return 查询对象 QueryWrapper
|
||||||
*/
|
*/
|
||||||
public static QueryWrapper create(Object entity) {
|
public static QueryWrapper create(Object entity) {
|
||||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass()));
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
public <Q extends QueryWrapper> WithBuilder<Q> with(String name) {
|
public <Q extends QueryWrapper> WithBuilder<Q> with(String name) {
|
||||||
if (with == null) {
|
if (with == null) {
|
||||||
@ -657,16 +670,16 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
public QueryWrapper limit(Number rows) {
|
public QueryWrapper limit(Number rows) {
|
||||||
if (rows != null) {
|
if (rows != null) {
|
||||||
setLimitRows(rows.longValue());
|
setLimitRows(rows.longValue());
|
||||||
}else {
|
} else {
|
||||||
setLimitRows(null);
|
setLimitRows(null);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper offset(Number offset) {
|
public QueryWrapper offset(Number offset) {
|
||||||
if (offset!= null) {
|
if (offset != null) {
|
||||||
setLimitOffset(offset.longValue());
|
setLimitOffset(offset.longValue());
|
||||||
}else {
|
} else {
|
||||||
setLimitOffset(null);
|
setLimitOffset(null);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@ -784,7 +797,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 queryWrapper 的参数
|
* 获取 queryWrapper 的参数
|
||||||
* 在构建 sql 的时候,需要保证 where 在 having 的前面
|
* 在构建 sql 的时候,需要保证 where 在 having 的前面
|
||||||
@ -818,7 +830,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 queryWrapper 的参数
|
* 获取 queryWrapper 的参数
|
||||||
* 在构建 sql 的时候,需要保证 where 在 having 的前面
|
* 在构建 sql 的时候,需要保证 where 在 having 的前面
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import com.mybatisflex.annotation.*;
|
|||||||
import com.mybatisflex.core.FlexConsts;
|
import com.mybatisflex.core.FlexConsts;
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
import com.mybatisflex.core.constant.SqlConsts;
|
import com.mybatisflex.core.constant.SqlConsts;
|
||||||
|
import com.mybatisflex.core.constant.SqlOperator;
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.exception.locale.LocalizedFormats;
|
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];
|
QueryColumn[] queryColumns = new QueryColumn[defaultQueryColumns.length];
|
||||||
for (int i = 0; i < defaultQueryColumns.length; i++) {
|
for (int i = 0; i < defaultQueryColumns.length; i++) {
|
||||||
queryColumns[i] = columnQueryMapping.get(defaultQueryColumns[i]);
|
queryColumns[i] = columnQueryMapping.get(defaultQueryColumns[i]);
|
||||||
@ -941,7 +942,12 @@ public class TableInfo {
|
|||||||
Object value = metaObject.getValue(property);
|
Object value = metaObject.getValue(property);
|
||||||
if (value != null && !"".equals(value)) {
|
if (value != null && !"".equals(value)) {
|
||||||
QueryColumn queryColumn = buildQueryColumn(column);
|
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;
|
return queryWrapper;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user