mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-25 00:00:12 +08:00
update
This commit is contained in:
parent
909d6bdb3f
commit
ed44499161
@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
import com.github.mybatisplus.toolkit.Constant;
|
import com.github.mybatisplus.toolkit.Constant;
|
||||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -23,14 +22,14 @@ public interface MyBaseMapper<T> extends BaseMapper<T> {
|
|||||||
* @param clazz resultType
|
* @param clazz resultType
|
||||||
*/
|
*/
|
||||||
<DTO> DTO selectJoinOne(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
<DTO> DTO selectJoinOne(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
@Param(Constants.WRAPPER) BaseJoin myWrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表查询返回Map
|
* 连表查询返回Map
|
||||||
*
|
*
|
||||||
* @param myWrapper joinWrapper
|
* @param myWrapper joinWrapper
|
||||||
*/
|
*/
|
||||||
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) BaseJoin myWrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表查询返回记录集合
|
* 连表查询返回记录集合
|
||||||
@ -39,14 +38,14 @@ public interface MyBaseMapper<T> extends BaseMapper<T> {
|
|||||||
* @param clazz resultType
|
* @param clazz resultType
|
||||||
*/
|
*/
|
||||||
<DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
<DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
@Param(Constants.WRAPPER) BaseJoin myWrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表查询返回Map集合
|
* 连表查询返回Map集合
|
||||||
*
|
*
|
||||||
* @param myWrapper joinWrapper
|
* @param myWrapper joinWrapper
|
||||||
*/
|
*/
|
||||||
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) BaseJoin myWrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表查询返回记录集合并分页
|
* 连表查询返回记录集合并分页
|
||||||
@ -57,7 +56,7 @@ public interface MyBaseMapper<T> extends BaseMapper<T> {
|
|||||||
*/
|
*/
|
||||||
<DTO, P extends IPage<?>> IPage<DTO> selectJoinPage(P page,
|
<DTO, P extends IPage<?>> IPage<DTO> selectJoinPage(P page,
|
||||||
@Param(Constant.CLAZZ) Class<DTO> clazz,
|
@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
@Param(Constants.WRAPPER) BaseJoin myWrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表查询返回Map集合并分页
|
* 连表查询返回Map集合并分页
|
||||||
@ -65,5 +64,5 @@ public interface MyBaseMapper<T> extends BaseMapper<T> {
|
|||||||
* @param myWrapper joinWrapper
|
* @param myWrapper joinWrapper
|
||||||
*/
|
*/
|
||||||
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page,
|
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page,
|
||||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
@Param(Constants.WRAPPER) BaseJoin myWrapper);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.github.mybatisplus.base;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -16,31 +15,31 @@ public interface MyBaseService<T> extends IService<T> {
|
|||||||
/**
|
/**
|
||||||
* 连接查询返回一条记录
|
* 连接查询返回一条记录
|
||||||
*/
|
*/
|
||||||
<DTO> DTO selectJoinOne(Class<DTO> clazz, MyWrapper<T> wrapper);
|
<DTO> DTO selectJoinOne(Class<DTO> clazz, BaseJoin wrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接查询返回集合
|
* 连接查询返回集合
|
||||||
*/
|
*/
|
||||||
<DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyWrapper<T> wrapper);
|
<DTO> List<DTO> selectJoinList(Class<DTO> clazz, BaseJoin wrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接查询返回集合并分页
|
* 连接查询返回集合并分页
|
||||||
*/
|
*/
|
||||||
<DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyWrapper<T> wrapper);
|
<DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, BaseJoin wrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接查询返回Map
|
* 连接查询返回Map
|
||||||
*/
|
*/
|
||||||
Map<String, Object> selectJoinMap(MyWrapper<T> wrapper);
|
Map<String, Object> selectJoinMap(BaseJoin wrapper);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接查询返回Map集合
|
* 连接查询返回Map集合
|
||||||
*/
|
*/
|
||||||
List<Map<String, Object>> selectJoinMaps(MyWrapper<T> wrapper);
|
List<Map<String, Object>> selectJoinMaps(BaseJoin wrapper);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连接查询返回Map集合并分页
|
* 连接查询返回Map集合并分页
|
||||||
*/
|
*/
|
||||||
IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyWrapper<T> wrapper);
|
IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, BaseJoin wrapper);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.github.mybatisplus.base;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -15,33 +14,33 @@ public class MyBaseServiceImpl<M extends MyBaseMapper<T>, T> extends ServiceImpl
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <DTO> DTO selectJoinOne(Class<DTO> clazz, MyWrapper<T> wrapper) {
|
public <DTO> DTO selectJoinOne(Class<DTO> clazz, BaseJoin wrapper) {
|
||||||
return baseMapper.selectJoinOne(clazz, wrapper);
|
return baseMapper.selectJoinOne(clazz, wrapper);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyWrapper<T> wrapper) {
|
public <DTO> List<DTO> selectJoinList(Class<DTO> clazz, BaseJoin wrapper) {
|
||||||
return baseMapper.selectJoinList(clazz, wrapper);
|
return baseMapper.selectJoinList(clazz, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyWrapper<T> wrapper) {
|
public <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, BaseJoin wrapper) {
|
||||||
return baseMapper.selectJoinPage(page, clazz, wrapper);
|
return baseMapper.selectJoinPage(page, clazz, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> selectJoinMap(MyWrapper<T> wrapper) {
|
public Map<String, Object> selectJoinMap(BaseJoin wrapper) {
|
||||||
return baseMapper.selectJoinMap(wrapper);
|
return baseMapper.selectJoinMap(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Map<String, Object>> selectJoinMaps(MyWrapper<T> wrapper) {
|
public List<Map<String, Object>> selectJoinMaps(BaseJoin wrapper) {
|
||||||
return baseMapper.selectJoinMaps(wrapper);
|
return baseMapper.selectJoinMaps(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyWrapper<T> wrapper) {
|
public IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, BaseJoin wrapper) {
|
||||||
return baseMapper.selectJoinMapsPage(page, wrapper);
|
return baseMapper.selectJoinMapsPage(page, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.github.mybatisplus.interfaces;
|
||||||
|
|
||||||
|
public interface BaseJoin {
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package com.github.mybatisplus.query;
|
package com.github.mybatisplus.query;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
@ -19,7 +20,7 @@ import static java.util.stream.Collectors.joining;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLambdaWrapper<T, Children>>
|
public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLambdaWrapper<T, Children>>
|
||||||
extends MyAbstractWrapper<T, SFunction<T, ?>, Children> {
|
extends AbstractWrapper<T, SFunction<T, ?>, Children> {
|
||||||
|
|
||||||
private Map<String, ColumnCache> columnMap = null;
|
private Map<String, ColumnCache> columnMap = null;
|
||||||
private boolean initColumnMap = false;
|
private boolean initColumnMap = false;
|
||||||
|
@ -1,528 +0,0 @@
|
|||||||
package com.github.mybatisplus.query;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.interfaces.Compare;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.interfaces.Join;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.interfaces.Nested;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlLike;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
|
||||||
import com.github.mybatisplus.query.interfaces.MyJoin;
|
|
||||||
import com.github.mybatisplus.toolkit.Constant;
|
|
||||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.function.BiPredicate;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
|
|
||||||
import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY;
|
|
||||||
import static java.util.stream.Collectors.joining;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractWrapper}
|
|
||||||
*/
|
|
||||||
@SuppressWarnings({"serial", "unchecked"})
|
|
||||||
public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper<T, R, Children>> extends MyWrapper<T>
|
|
||||||
implements Compare<Children, R>, Nested<Children, Children>, Join<Children>, Func<Children, R>,
|
|
||||||
MyJoin<Children> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 占位符
|
|
||||||
*/
|
|
||||||
protected final Children typedThis = (Children) this;
|
|
||||||
/**
|
|
||||||
* 必要度量
|
|
||||||
*/
|
|
||||||
protected AtomicInteger paramNameSeq;
|
|
||||||
protected Map<String, Object> paramNameValuePairs;
|
|
||||||
protected SharedString lastSql;
|
|
||||||
/**
|
|
||||||
* SQL注释
|
|
||||||
*/
|
|
||||||
protected SharedString sqlComment;
|
|
||||||
/**
|
|
||||||
* SQL起始语句
|
|
||||||
*/
|
|
||||||
protected SharedString sqlFirst;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表字段
|
|
||||||
*/
|
|
||||||
protected SharedString from = SharedString.emptyString();
|
|
||||||
/**
|
|
||||||
* ß
|
|
||||||
* 数据库表映射实体类
|
|
||||||
*/
|
|
||||||
private T entity;
|
|
||||||
protected MergeSegments expression;
|
|
||||||
/**
|
|
||||||
* 实体类型(主要用于确定泛型以及取TableInfo缓存)
|
|
||||||
*/
|
|
||||||
private Class<T> entityClass;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getEntity() {
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Children setEntity(T entity) {
|
|
||||||
this.entity = entity;
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<T> getEntityClass() {
|
|
||||||
if (entityClass == null && entity != null) {
|
|
||||||
entityClass = (Class<T>) entity.getClass();
|
|
||||||
}
|
|
||||||
return entityClass;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Children setEntityClass(Class<T> entityClass) {
|
|
||||||
if (entityClass != null) {
|
|
||||||
this.entityClass = entityClass;
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull) {
|
|
||||||
if (condition && CollectionUtils.isNotEmpty(params)) {
|
|
||||||
params.forEach((k, v) -> {
|
|
||||||
if (StringUtils.checkValNotNull(v)) {
|
|
||||||
eq(k, v);
|
|
||||||
} else {
|
|
||||||
if (null2IsNull) {
|
|
||||||
isNull(k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <V> Children allEq(boolean condition, BiPredicate<R, V> filter, Map<R, V> params, boolean null2IsNull) {
|
|
||||||
if (condition && CollectionUtils.isNotEmpty(params)) {
|
|
||||||
params.forEach((k, v) -> {
|
|
||||||
if (filter.test(k, v)) {
|
|
||||||
if (StringUtils.checkValNotNull(v)) {
|
|
||||||
eq(k, v);
|
|
||||||
} else {
|
|
||||||
if (null2IsNull) {
|
|
||||||
isNull(k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children eq(boolean condition, R column, Object val) {
|
|
||||||
return addCondition(condition, column, EQ, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children ne(boolean condition, R column, Object val) {
|
|
||||||
return addCondition(condition, column, NE, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children gt(boolean condition, R column, Object val) {
|
|
||||||
return addCondition(condition, column, GT, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children ge(boolean condition, R column, Object val) {
|
|
||||||
return addCondition(condition, column, GE, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children lt(boolean condition, R column, Object val) {
|
|
||||||
return addCondition(condition, column, LT, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children le(boolean condition, R column, Object val) {
|
|
||||||
return addCondition(condition, column, LE, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children like(boolean condition, R column, Object val) {
|
|
||||||
return likeValue(condition, LIKE, column, val, SqlLike.DEFAULT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children notLike(boolean condition, R column, Object val) {
|
|
||||||
return likeValue(condition, NOT_LIKE, column, val, SqlLike.DEFAULT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children likeLeft(boolean condition, R column, Object val) {
|
|
||||||
return likeValue(condition, LIKE, column, val, SqlLike.LEFT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children likeRight(boolean condition, R column, Object val) {
|
|
||||||
return likeValue(condition, LIKE, column, val, SqlLike.RIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children between(boolean condition, R column, Object val1, Object val2) {
|
|
||||||
return doIt(condition, () -> columnToString(column), BETWEEN, () -> formatSql("{0}", val1), AND,
|
|
||||||
() -> formatSql("{0}", val2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children notBetween(boolean condition, R column, Object val1, Object val2) {
|
|
||||||
return doIt(condition, () -> columnToString(column), NOT_BETWEEN, () -> formatSql("{0}", val1), AND,
|
|
||||||
() -> formatSql("{0}", val2));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children and(boolean condition, Consumer<Children> consumer) {
|
|
||||||
return and(condition).addNestedCondition(condition, consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children or(boolean condition, Consumer<Children> consumer) {
|
|
||||||
return or(condition).addNestedCondition(condition, consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children nested(boolean condition, Consumer<Children> consumer) {
|
|
||||||
return addNestedCondition(condition, consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children not(boolean condition, Consumer<Children> consumer) {
|
|
||||||
return not(condition).addNestedCondition(condition, consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children or(boolean condition) {
|
|
||||||
return doIt(condition, OR);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children apply(boolean condition, String applySql, Object... value) {
|
|
||||||
return doIt(condition, APPLY, () -> formatSql(applySql, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children last(boolean condition, String lastSql) {
|
|
||||||
if (condition) {
|
|
||||||
this.lastSql.setStringValue(StringPool.SPACE + lastSql);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children comment(boolean condition, String comment) {
|
|
||||||
if (condition) {
|
|
||||||
this.sqlComment.setStringValue(comment);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children first(boolean condition, String firstSql) {
|
|
||||||
if (condition) {
|
|
||||||
this.sqlFirst.setStringValue(firstSql);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children exists(boolean condition, String existsSql) {
|
|
||||||
return doIt(condition, EXISTS, () -> String.format("(%s)", existsSql));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children notExists(boolean condition, String existsSql) {
|
|
||||||
return not(condition).exists(condition, existsSql);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children isNull(boolean condition, R column) {
|
|
||||||
return doIt(condition, () -> columnToString(column), IS_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children isNotNull(boolean condition, R column) {
|
|
||||||
return doIt(condition, () -> columnToString(column), IS_NOT_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children in(boolean condition, R column, Collection<?> coll) {
|
|
||||||
return doIt(condition, () -> columnToString(column), IN, inExpression(coll));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children notIn(boolean condition, R column, Collection<?> coll) {
|
|
||||||
return doIt(condition, () -> columnToString(column), NOT_IN, inExpression(coll));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children inSql(boolean condition, R column, String inValue) {
|
|
||||||
return doIt(condition, () -> columnToString(column), IN, () -> String.format("(%s)", inValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children notInSql(boolean condition, R column, String inValue) {
|
|
||||||
return doIt(condition, () -> columnToString(column), NOT_IN, () -> String.format("(%s)", inValue));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children groupBy(boolean condition, R... columns) {
|
|
||||||
if (ArrayUtils.isEmpty(columns)) {
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
return doIt(condition, GROUP_BY,
|
|
||||||
() -> columns.length == 1 ? columnToString(columns[0]) : columnsToString(columns));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children orderBy(boolean condition, boolean isAsc, R... columns) {
|
|
||||||
if (ArrayUtils.isEmpty(columns)) {
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
SqlKeyword mode = isAsc ? ASC : DESC;
|
|
||||||
for (R column : columns) {
|
|
||||||
doIt(condition, ORDER_BY, () -> columnToString(column), mode);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children having(boolean condition, String sqlHaving, Object... params) {
|
|
||||||
return doIt(condition, HAVING, () -> formatSqlIfNeed(condition, sqlHaving, params));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children func(boolean condition, Consumer<Children> consumer) {
|
|
||||||
if (condition) {
|
|
||||||
consumer.accept(typedThis);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内部自用
|
|
||||||
* <p>NOT 关键词</p>
|
|
||||||
*/
|
|
||||||
protected Children not(boolean condition) {
|
|
||||||
return doIt(condition, NOT);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内部自用
|
|
||||||
* <p>拼接 AND</p>
|
|
||||||
*/
|
|
||||||
protected Children and(boolean condition) {
|
|
||||||
return doIt(condition, AND);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内部自用
|
|
||||||
* <p>拼接 LIKE 以及 值</p>
|
|
||||||
*/
|
|
||||||
protected Children likeValue(boolean condition, SqlKeyword keyword, R column, Object val, SqlLike sqlLike) {
|
|
||||||
return doIt(condition, () -> columnToString(column), keyword, () -> formatSql("{0}", SqlUtils.concatLike(val, sqlLike)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 普通查询条件
|
|
||||||
*
|
|
||||||
* @param condition 是否执行
|
|
||||||
* @param column 属性
|
|
||||||
* @param sqlKeyword SQL 关键词
|
|
||||||
* @param val 条件值
|
|
||||||
*/
|
|
||||||
protected Children addCondition(boolean condition, R column, SqlKeyword sqlKeyword, Object val) {
|
|
||||||
return doIt(condition, () -> columnToString(column), sqlKeyword, () -> formatSql("{0}", val));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多重嵌套查询条件
|
|
||||||
*
|
|
||||||
* @param condition 查询条件值
|
|
||||||
*/
|
|
||||||
protected Children addNestedCondition(boolean condition, Consumer<Children> consumer) {
|
|
||||||
if (condition) {
|
|
||||||
final Children instance = instance();
|
|
||||||
consumer.accept(instance);
|
|
||||||
return doIt(true, APPLY, instance);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 子类返回一个自己的新对象
|
|
||||||
*/
|
|
||||||
protected abstract Children instance();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 格式化SQL
|
|
||||||
*
|
|
||||||
* @param sqlStr SQL语句部分
|
|
||||||
* @param params 参数集
|
|
||||||
* @return sql
|
|
||||||
*/
|
|
||||||
protected final String formatSql(String sqlStr, Object... params) {
|
|
||||||
return formatSqlIfNeed(true, sqlStr, params);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>
|
|
||||||
* 根据需要格式化SQL<br>
|
|
||||||
* <br>
|
|
||||||
* Format SQL for methods: EntityQ<T>.where/and/or...("name={0}", value);
|
|
||||||
* ALL the {<b>i</b>} will be replaced with #{MPGENVAL<b>i</b>}<br>
|
|
||||||
* <br>
|
|
||||||
* ew.where("sample_name=<b>{0}</b>", "haha").and("sample_age ><b>{0}</b>
|
|
||||||
* and sample_age<<b>{1}</b>", 18, 30) <b>TO</b>
|
|
||||||
* sample_name=<b>#{MPGENVAL1}</b> and sample_age>#<b>{MPGENVAL2}</b> and
|
|
||||||
* sample_age<<b>#{MPGENVAL3}</b><br>
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param need 是否需要格式化
|
|
||||||
* @param sqlStr SQL语句部分
|
|
||||||
* @param params 参数集
|
|
||||||
* @return sql
|
|
||||||
*/
|
|
||||||
protected final String formatSqlIfNeed(boolean need, String sqlStr, Object... params) {
|
|
||||||
if (!need || StringUtils.isBlank(sqlStr)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (ArrayUtils.isNotEmpty(params)) {
|
|
||||||
for (int i = 0; i < params.length; ++i) {
|
|
||||||
String genParamName = Constants.WRAPPER_PARAM + paramNameSeq.incrementAndGet();
|
|
||||||
sqlStr = sqlStr.replace(String.format("{%s}", i),
|
|
||||||
String.format(Constants.WRAPPER_PARAM_FORMAT, Constants.WRAPPER, genParamName));
|
|
||||||
paramNameValuePairs.put(genParamName, params[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sqlStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取in表达式 包含括号
|
|
||||||
*
|
|
||||||
* @param value 集合
|
|
||||||
*/
|
|
||||||
private ISqlSegment inExpression(Collection<?> value) {
|
|
||||||
return () -> value.stream().map(i -> formatSql("{0}", i))
|
|
||||||
.collect(joining(StringPool.COMMA, StringPool.LEFT_BRACKET, StringPool.RIGHT_BRACKET));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 必要的初始化
|
|
||||||
*/
|
|
||||||
protected void initNeed() {
|
|
||||||
paramNameSeq = new AtomicInteger(0);
|
|
||||||
paramNameValuePairs = new HashMap<>(16);
|
|
||||||
expression = new MergeSegments();
|
|
||||||
lastSql = SharedString.emptyString();
|
|
||||||
sqlComment = SharedString.emptyString();
|
|
||||||
sqlFirst = SharedString.emptyString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clear() {
|
|
||||||
entity = null;
|
|
||||||
paramNameSeq.set(0);
|
|
||||||
paramNameValuePairs.clear();
|
|
||||||
expression.clear();
|
|
||||||
lastSql.toEmpty();
|
|
||||||
sqlComment.toEmpty();
|
|
||||||
sqlFirst.toEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 对sql片段进行组装
|
|
||||||
*
|
|
||||||
* @param condition 是否执行
|
|
||||||
* @param sqlSegments sql片段数组
|
|
||||||
* @return children
|
|
||||||
*/
|
|
||||||
protected Children doIt(boolean condition, ISqlSegment... sqlSegments) {
|
|
||||||
if (condition) {
|
|
||||||
expression.add(sqlSegments);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getSqlSegment() {
|
|
||||||
return expression.getSqlSegment() + lastSql.getStringValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getSqlComment() {
|
|
||||||
if (StringUtils.isNotBlank(sqlComment.getStringValue())) {
|
|
||||||
return "/*" + StringEscape.escapeRawString(sqlComment.getStringValue()) + "*/";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getSqlFirst() {
|
|
||||||
if (StringUtils.isNotBlank(sqlFirst.getStringValue())) {
|
|
||||||
return StringEscape.escapeRawString(sqlFirst.getStringValue());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MergeSegments getExpression() {
|
|
||||||
return expression;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Object> getParamNameValuePairs() {
|
|
||||||
return paramNameValuePairs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 columnName
|
|
||||||
*/
|
|
||||||
protected String columnToString(R column) {
|
|
||||||
return (String) column;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 多字段转换为逗号 "," 分割字符串
|
|
||||||
*
|
|
||||||
* @param columns 多字段
|
|
||||||
*/
|
|
||||||
protected String columnsToString(R... columns) {
|
|
||||||
return Arrays.stream(columns).map(this::columnToString).collect(joining(StringPool.COMMA));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
public Children clone() {
|
|
||||||
return SerializationUtils.clone(typedThis);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Children join(String keyWord, boolean condition, String joinSql) {
|
|
||||||
if (condition) {
|
|
||||||
from.setStringValue(from.getStringValue() + keyWord + joinSql);
|
|
||||||
}
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
}
|
|
@ -36,10 +36,15 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
|
|||||||
*/
|
*/
|
||||||
private SharedString sqlSelect = new SharedString();
|
private SharedString sqlSelect = new SharedString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表字段
|
||||||
|
*/
|
||||||
|
private SharedString from = SharedString.emptyString();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主表别名
|
* 主表别名
|
||||||
*/
|
*/
|
||||||
private SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -197,4 +202,11 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
|
|||||||
sqlSelect.toNull();
|
sqlSelect.toNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyLambdaQueryWrapper<T> join(String keyWord, boolean condition, String joinSql) {
|
||||||
|
if (condition) {
|
||||||
|
from.setStringValue(from.getStringValue() + keyWord + joinSql);
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.github.mybatisplus.query;
|
package com.github.mybatisplus.query;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
||||||
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
||||||
@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
import com.github.mybatisplus.query.interfaces.MyJoin;
|
||||||
import com.github.mybatisplus.toolkit.Constant;
|
import com.github.mybatisplus.toolkit.Constant;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -22,18 +24,24 @@ import java.util.stream.Collectors;
|
|||||||
* copy {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
* copy {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class MyQueryWrapper<T> extends MyAbstractWrapper<T, String, MyQueryWrapper<T>>
|
public class MyQueryWrapper<T> extends AbstractWrapper<T, String, MyQueryWrapper<T>>
|
||||||
implements Query<MyQueryWrapper<T>, T, String> {
|
implements Query<MyQueryWrapper<T>, T, String>, MyJoin<MyQueryWrapper<T>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询字段
|
* 查询字段
|
||||||
*/
|
*/
|
||||||
private SharedString sqlSelect = new SharedString();
|
private SharedString sqlSelect = new SharedString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表字段
|
||||||
|
*/
|
||||||
|
private SharedString from = SharedString.emptyString();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主表别名
|
* 主表别名
|
||||||
*/
|
*/
|
||||||
private SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||||
|
|
||||||
|
|
||||||
public MyQueryWrapper() {
|
public MyQueryWrapper() {
|
||||||
this(null);
|
this(null);
|
||||||
@ -119,7 +127,7 @@ public class MyQueryWrapper<T> extends MyAbstractWrapper<T, String, MyQueryWrapp
|
|||||||
*/
|
*/
|
||||||
public MyLambdaQueryWrapper<T> lambda() {
|
public MyLambdaQueryWrapper<T> lambda() {
|
||||||
return new MyLambdaQueryWrapper<>(getEntity(), getEntityClass(), from, sqlSelect, paramNameSeq, paramNameValuePairs,
|
return new MyLambdaQueryWrapper<>(getEntity(), getEntityClass(), from, sqlSelect, paramNameSeq, paramNameValuePairs,
|
||||||
expression, lastSql, sqlComment, sqlFirst);
|
expression, lastSql, sqlComment, sqlFirst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,4 +147,12 @@ public class MyQueryWrapper<T> extends MyAbstractWrapper<T, String, MyQueryWrapp
|
|||||||
super.clear();
|
super.clear();
|
||||||
sqlSelect.toNull();
|
sqlSelect.toNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MyQueryWrapper<T> join(String keyWord, boolean condition, String joinSql) {
|
||||||
|
if (condition) {
|
||||||
|
from.setStringValue(from.getStringValue() + keyWord + joinSql);
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package com.github.mybatisplus.query.interfaces;
|
package com.github.mybatisplus.query.interfaces;
|
||||||
|
|
||||||
|
import com.github.mybatisplus.interfaces.BaseJoin;
|
||||||
import com.github.mybatisplus.toolkit.Constant;
|
import com.github.mybatisplus.toolkit.Constant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
*/
|
*/
|
||||||
public interface MyJoin<Children> {
|
public interface MyJoin<Children> extends BaseJoin {
|
||||||
|
|
||||||
default Children leftJoin(String joinSql) {
|
default Children leftJoin(String joinSql) {
|
||||||
return leftJoin(true, joinSql);
|
return leftJoin(true, joinSql);
|
||||||
|
@ -31,7 +31,7 @@ import static java.util.stream.Collectors.joining;
|
|||||||
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractWrapper}
|
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractWrapper}
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"serial", "unchecked"})
|
@SuppressWarnings({"serial", "unchecked"})
|
||||||
public abstract class MyAbstractWrapper<T, Children extends MyAbstractWrapper<T, Children>> extends MyWrapper<T>
|
public abstract class MyAbstractWrapper<T, Children extends MyAbstractWrapper<T, Children>> extends Wrapper<T>
|
||||||
implements MyCompare<Children>, Nested<Children, Children>, Join<Children>, MyFunc<Children> {
|
implements MyCompare<Children>, Nested<Children, Children>, Join<Children>, MyFunc<Children> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -37,17 +37,17 @@ public class MyJoinLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyJo
|
|||||||
/**
|
/**
|
||||||
* 查询表
|
* 查询表
|
||||||
*/
|
*/
|
||||||
private SharedString from = new SharedString();
|
private final SharedString from = new SharedString();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主表别名
|
* 主表别名
|
||||||
*/
|
*/
|
||||||
private SharedString alias = new SharedString();
|
private final SharedString alias = new SharedString();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询的字段
|
* 查询的字段
|
||||||
*/
|
*/
|
||||||
private List<SelectColumn> selectColumns = new ArrayList<>();
|
private final List<SelectColumn> selectColumns = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,170 +0,0 @@
|
|||||||
package com.github.mybatisplus.wrapper;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* copy {@link com.baomidou.mybatisplus.core.conditions.Wrapper}
|
|
||||||
* <p>
|
|
||||||
* join wrapper的顶级wrapper
|
|
||||||
* 与mp的Wrapper没做任何改动
|
|
||||||
* <p>
|
|
||||||
* 因为单表wrapper与多表wrapper体系不一样,暂时先分开,后续可以统一wrapper
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("all")
|
|
||||||
public abstract class MyWrapper<T> implements ISqlSegment {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 实体对象(子类实现)
|
|
||||||
*
|
|
||||||
* @return 泛型 T
|
|
||||||
*/
|
|
||||||
public abstract T getEntity();
|
|
||||||
|
|
||||||
public String getSqlSelect() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSqlSet() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSqlComment() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSqlFirst() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 MergeSegments
|
|
||||||
*/
|
|
||||||
public abstract MergeSegments getExpression();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取自定义SQL 简化自定义XML复杂情况
|
|
||||||
* <p>
|
|
||||||
* 使用方法: `select xxx from table` + ${ew.customSqlSegment}
|
|
||||||
* <p>
|
|
||||||
* 注意事项:
|
|
||||||
* 1. 逻辑删除需要自己拼接条件 (之前自定义也同样)
|
|
||||||
* 2. 不支持wrapper中附带实体的情况 (wrapper自带实体会更麻烦)
|
|
||||||
* 3. 用法 ${ew.customSqlSegment} (不需要where标签包裹,切记!)
|
|
||||||
* 4. ew是wrapper定义别名,不能使用其他的替换
|
|
||||||
*/
|
|
||||||
public String getCustomSqlSegment() {
|
|
||||||
MergeSegments expression = getExpression();
|
|
||||||
if (Objects.nonNull(expression)) {
|
|
||||||
NormalSegmentList normal = expression.getNormal();
|
|
||||||
String sqlSegment = getSqlSegment();
|
|
||||||
if (StringUtils.isNotBlank(sqlSegment)) {
|
|
||||||
if (normal.isEmpty()) {
|
|
||||||
return sqlSegment;
|
|
||||||
} else {
|
|
||||||
return Constants.WHERE + StringPool.SPACE + sqlSegment;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return StringPool.EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询条件为空(包含entity)
|
|
||||||
*/
|
|
||||||
public boolean isEmptyOfWhere() {
|
|
||||||
return isEmptyOfNormal() && isEmptyOfEntity();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询条件不为空(包含entity)
|
|
||||||
*/
|
|
||||||
public boolean nonEmptyOfWhere() {
|
|
||||||
return !isEmptyOfWhere();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询条件为空(不包含entity)
|
|
||||||
*/
|
|
||||||
public boolean isEmptyOfNormal() {
|
|
||||||
return CollectionUtils.isEmpty(getExpression().getNormal());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询条件为空(不包含entity)
|
|
||||||
*/
|
|
||||||
public boolean nonEmptyOfNormal() {
|
|
||||||
return !isEmptyOfNormal();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 深层实体判断属性
|
|
||||||
*
|
|
||||||
* @return true 不为空
|
|
||||||
*/
|
|
||||||
public boolean nonEmptyOfEntity() {
|
|
||||||
T entity = getEntity();
|
|
||||||
if (entity == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
|
|
||||||
if (tableInfo == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, e))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return StringUtils.isNotBlank(tableInfo.getKeyProperty()) ? Objects.nonNull(ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty())) : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据实体FieldStrategy属性来决定判断逻辑
|
|
||||||
*/
|
|
||||||
private boolean fieldStrategyMatch(T entity, TableFieldInfo e) {
|
|
||||||
switch (e.getWhereStrategy()) {
|
|
||||||
case NOT_NULL:
|
|
||||||
return Objects.nonNull(ReflectionKit.getFieldValue(entity, e.getProperty()));
|
|
||||||
case IGNORED:
|
|
||||||
return true;
|
|
||||||
case NOT_EMPTY:
|
|
||||||
return StringUtils.checkValNotNull(ReflectionKit.getFieldValue(entity, e.getProperty()));
|
|
||||||
case NEVER:
|
|
||||||
return false;
|
|
||||||
default:
|
|
||||||
return Objects.nonNull(ReflectionKit.getFieldValue(entity, e.getProperty()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 深层实体判断属性
|
|
||||||
*
|
|
||||||
* @return true 为空
|
|
||||||
*/
|
|
||||||
public boolean isEmptyOfEntity() {
|
|
||||||
return !nonEmptyOfEntity();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取格式化后的执行sql
|
|
||||||
*
|
|
||||||
* @return sql
|
|
||||||
* @since 3.3.1
|
|
||||||
*/
|
|
||||||
public String getTargetSql() {
|
|
||||||
return getSqlSegment().replaceAll("#\\{.+?}", "?");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 条件清空
|
|
||||||
*
|
|
||||||
* @since 3.3.1
|
|
||||||
*/
|
|
||||||
abstract public void clear();
|
|
||||||
}
|
|
@ -1,12 +1,13 @@
|
|||||||
package com.github.mybatisplus.wrapper.interfaces;
|
package com.github.mybatisplus.wrapper.interfaces;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.github.mybatisplus.interfaces.BaseJoin;
|
||||||
import com.github.mybatisplus.toolkit.Constant;
|
import com.github.mybatisplus.toolkit.Constant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
*/
|
*/
|
||||||
public interface MyLambdaJoin<Children> {
|
public interface MyLambdaJoin<Children> extends BaseJoin {
|
||||||
|
|
||||||
default <T, X> Children leftJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
default <T, X> Children leftJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||||
return leftJoin(true, clazz, left, right);
|
return leftJoin(true, clazz, left, right);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user