mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
update
This commit is contained in:
parent
fb89f5d84c
commit
99890d94dd
@ -191,7 +191,7 @@ ORDER BY addr.id DESC
|
||||
|
||||
## MyLambdaQueryWrapper用法
|
||||
|
||||
### MyLambdaQueryWrapper更符合面向对象(OOP),没有难以理解的常量(魔术值),全部基于lambda,但好像不那么好理解
|
||||
### MyLambdaQueryWrapper更符合面向对象(OOP),没有难以理解的常量(魔术值),全部基于lambda,但灵活性不如上面的
|
||||
|
||||
#### 简单的3表查询
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.github.mybatisplus.base;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
@ -19,48 +19,51 @@ public interface MyBaseMapper<T> extends BaseMapper<T> {
|
||||
/**
|
||||
* 连表查询返回一条记录
|
||||
*
|
||||
* @param queryWrapper joinWrapper
|
||||
* @param clazz resultType
|
||||
* @param myWrapper joinWrapper
|
||||
* @param clazz resultType
|
||||
*/
|
||||
<DTO> DTO selectJoinOne(@Param(Constant.CLAZZ) Class<DTO> clazz, @Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
<DTO> DTO selectJoinOne(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
||||
|
||||
/**
|
||||
* 连表查询返回Map
|
||||
*
|
||||
* @param queryWrapper joinWrapper
|
||||
* @param myWrapper joinWrapper
|
||||
*/
|
||||
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
||||
|
||||
/**
|
||||
* 连表查询返回记录集合
|
||||
*
|
||||
* @param queryWrapper joinWrapper
|
||||
* @param clazz resultType
|
||||
* @param myWrapper joinWrapper
|
||||
* @param clazz resultType
|
||||
*/
|
||||
<DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz, @Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
<DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
||||
|
||||
/**
|
||||
* 连表查询返回Map集合
|
||||
*
|
||||
* @param queryWrapper joinWrapper
|
||||
* @param myWrapper joinWrapper
|
||||
*/
|
||||
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
||||
|
||||
/**
|
||||
* 连表查询返回记录集合并分页
|
||||
*
|
||||
* @param queryWrapper joinWrapper
|
||||
* @param clazz resultType
|
||||
* @param <DTO> 分页返回对象
|
||||
* @param myWrapper joinWrapper
|
||||
* @param clazz resultType
|
||||
* @param <DTO> 分页返回对象
|
||||
*/
|
||||
<DTO, P extends IPage<?>> IPage<DTO> selectJoinPage(P page,
|
||||
@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||
@Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
||||
|
||||
/**
|
||||
* 连表查询返回Map集合并分页
|
||||
*
|
||||
* @param queryWrapper joinWrapper
|
||||
* @param myWrapper joinWrapper
|
||||
*/
|
||||
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page, @Param(Constants.WRAPPER) Wrapper<?> queryWrapper);
|
||||
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page,
|
||||
@Param(Constants.WRAPPER) MyWrapper<?> myWrapper);
|
||||
}
|
||||
|
@ -2,8 +2,7 @@ package com.github.mybatisplus.base;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.github.mybatisplus.func.MyWrapperFunc;
|
||||
import com.github.mybatisplus.wrapper.MyLambdaQueryWrapper;
|
||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -14,78 +13,34 @@ import java.util.Map;
|
||||
*/
|
||||
public interface MyBaseService<T> extends IService<T> {
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <DTO> DTO selectJoinOne(Class<DTO> clazz, MyWrapperFunc<T> wrapperFunc) {
|
||||
return selectJoinOne(clazz, wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回一条记录
|
||||
*/
|
||||
<DTO> DTO selectJoinOne(Class<DTO> clazz, MyLambdaQueryWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyWrapperFunc<T> wrapperFunc) {
|
||||
return selectJoinList(clazz, wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||
}
|
||||
<DTO> DTO selectJoinOne(Class<DTO> clazz, MyWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* 连接查询返回集合
|
||||
*/
|
||||
<DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyLambdaQueryWrapper<T> wrapper);
|
||||
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyWrapperFunc<T> wrapperFunc) {
|
||||
return selectJoinListPage(page, clazz, wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||
}
|
||||
<DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* 连接查询返回集合并分页
|
||||
*/
|
||||
<DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyLambdaQueryWrapper<T> wrapper);
|
||||
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Map<String, Object> selectJoinMap(MyWrapperFunc<T> wrapperFunc) {
|
||||
return selectJoinMap(wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||
}
|
||||
<DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* 连接查询返回Map
|
||||
*/
|
||||
Map<String, Object> selectJoinMap(MyLambdaQueryWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default List<Map<String, Object>> selectJoinMaps(MyWrapperFunc<T> wrapperFunc) {
|
||||
return selectJoinMaps(wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||
}
|
||||
Map<String, Object> selectJoinMap(MyWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* 连接查询返回Map集合
|
||||
*/
|
||||
List<Map<String, Object>> selectJoinMaps(MyLambdaQueryWrapper<T> wrapper);
|
||||
List<Map<String, Object>> selectJoinMaps(MyWrapper<T> wrapper);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyWrapperFunc<T> wrapperFunc) {
|
||||
return selectJoinMapsPage(page, wrapperFunc.apply(new MyLambdaQueryWrapper<>()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回Map集合并分页
|
||||
*/
|
||||
IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyLambdaQueryWrapper<T> wrapper);
|
||||
|
||||
IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyWrapper<T> wrapper);
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.github.mybatisplus.base;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.github.mybatisplus.wrapper.MyLambdaQueryWrapper;
|
||||
import com.github.mybatisplus.wrapper.MyWrapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -15,33 +16,33 @@ public class MyBaseServiceImpl<M extends MyBaseMapper<T>, T> extends ServiceImpl
|
||||
|
||||
|
||||
@Override
|
||||
public <DTO> DTO selectJoinOne(Class<DTO> clazz, MyLambdaQueryWrapper<T> wrapper) {
|
||||
public <DTO> DTO selectJoinOne(Class<DTO> clazz, MyWrapper<T> wrapper) {
|
||||
return baseMapper.selectJoinOne(clazz, wrapper);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyLambdaQueryWrapper<T> wrapper) {
|
||||
public <DTO> List<DTO> selectJoinList(Class<DTO> clazz, MyWrapper<T> wrapper) {
|
||||
return baseMapper.selectJoinList(clazz, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyLambdaQueryWrapper<T> wrapper) {
|
||||
public <DTO, P extends IPage<?>> IPage<DTO> selectJoinListPage(P page, Class<DTO> clazz, MyWrapper<T> wrapper) {
|
||||
return baseMapper.selectJoinPage(page, clazz, wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> selectJoinMap(MyLambdaQueryWrapper<T> wrapper) {
|
||||
public Map<String, Object> selectJoinMap(MyWrapper<T> wrapper) {
|
||||
return baseMapper.selectJoinMap(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> selectJoinMaps(MyLambdaQueryWrapper<T> wrapper) {
|
||||
public List<Map<String, Object>> selectJoinMaps(MyWrapper<T> wrapper) {
|
||||
return baseMapper.selectJoinMaps(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyLambdaQueryWrapper<T> wrapper) {
|
||||
public IPage<Map<String, Object>> selectJoinMapsPage(IPage<Map<String, Object>> page, MyWrapper<T> wrapper) {
|
||||
return baseMapper.selectJoinMapsPage(page, wrapper);
|
||||
}
|
||||
|
||||
|
@ -1,15 +0,0 @@
|
||||
package com.github.mybatisplus.func;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
|
||||
/**
|
||||
* 支持别名的 SFunction
|
||||
*
|
||||
* @author yulichang
|
||||
* @see SFunction
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface MySFunction<T, R> extends SFunction<T, R> {
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package com.github.mybatisplus.func;
|
||||
|
||||
import com.github.mybatisplus.wrapper.MyLambdaQueryWrapper;
|
||||
|
||||
/**
|
||||
* @author yulichang
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface MyWrapperFunc<T> {
|
||||
|
||||
MyLambdaQueryWrapper<T> apply(MyLambdaQueryWrapper<T> wrapper);
|
||||
}
|
@ -16,7 +16,7 @@ public class SelectJoinList extends MyAbstractMethod {
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||
tableInfo.getTableName(), Constant.TABLE_ALIAS, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.github.mybatisplus.method;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlSource;
|
||||
|
||||
@ -16,7 +15,7 @@ public class SelectJoinMap extends MyAbstractMethod {
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||
tableInfo.getTableName(), Constant.TABLE_ALIAS, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public class SelectJoinMaps extends MyAbstractMethod {
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||
tableInfo.getTableName(), Constant.TABLE_ALIAS, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public class SelectJoinMapsPage extends MyAbstractMethod {
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||
tableInfo.getTableName(), Constant.TABLE_ALIAS, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public class SelectJoinOne extends MyAbstractMethod {
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||
tableInfo.getTableName(), Constant.TABLE_ALIAS, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public class SelectJoinPage extends MyAbstractMethod {
|
||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
|
||||
tableInfo.getTableName(), Constant.TABLE_ALIAS, sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
@ -15,22 +15,22 @@ public enum SqlMethod {
|
||||
* 连表查询
|
||||
*/
|
||||
SELECT_JOIN_ONE("selectJoinOne", "返回一条记录",
|
||||
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
|
||||
SELECT_JOIN_LIST("selectJoinList", "返回List集合",
|
||||
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
|
||||
SELECT_JOIN_MAP("selectJoinMap", "返回一个Map",
|
||||
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
|
||||
SELECT_JOIN_MAPS("selectJoinMaps", "返回Map集合",
|
||||
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
|
||||
SELECT_JOIN_PAGE("selectJoinPage", "连表查询并分页",
|
||||
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
|
||||
|
||||
SELECT_JOIN_MAPS_PAGE("selectJoinMapsPage", "返回Map集合并分页",
|
||||
"<script>\nSELECT %s FROM %s %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>");
|
||||
"<script>\nSELECT %s FROM %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>");
|
||||
|
||||
private final String method;
|
||||
private final String desc;
|
||||
|
@ -32,7 +32,7 @@ public abstract class MyAbstractLambda<T, Children extends MyAbstractLambda<T, C
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected String columnsToString(boolean onlyColumn, SFunction<T, ?>... columns) {
|
||||
return Arrays.stream(columns).map(i -> Constant.TABLE_ALIAS + StringPool.DOT + columnToString(i, onlyColumn)).collect(joining(StringPool.COMMA));
|
||||
return Arrays.stream(columns).map(i -> columnToString(i, onlyColumn)).collect(joining(StringPool.COMMA));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,6 +16,7 @@ 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;
|
||||
@ -33,8 +34,8 @@ 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 Wrapper<T>
|
||||
implements Compare<Children, R>, Nested<Children, Children>, Join<Children>, Func<Children, R>, MyJoin<Children> {
|
||||
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> {
|
||||
|
||||
/**
|
||||
* 占位符
|
||||
@ -94,31 +95,6 @@ public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children leftJoin(boolean condition, String joinSql) {
|
||||
if (condition) {
|
||||
from.setStringValue(from.getStringValue() + Constant.LEFT_JOIN + joinSql);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children rightJoin(boolean condition, String joinSql) {
|
||||
if (condition) {
|
||||
from.setStringValue(from.getStringValue() + Constant.RIGHT_JOIN + joinSql);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Children innerJoin(boolean condition, String joinSql) {
|
||||
if (condition) {
|
||||
from.setStringValue(from.getStringValue() + Constant.INNER_JOIN + joinSql);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull) {
|
||||
if (condition && CollectionUtils.isNotEmpty(params)) {
|
||||
|
@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.mybatisplus.query.interfaces.MyJoin;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -28,13 +29,18 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class MyLambdaQuery<T> extends MyAbstractLambda<T, MyLambdaQuery<T>>
|
||||
implements Query<MyLambdaQuery<T>, T, SFunction<T, ?>> {
|
||||
implements Query<MyLambdaQuery<T>, T, SFunction<T, ?>>, MyJoin<MyLambdaQuery<T>> {
|
||||
|
||||
/**
|
||||
* 查询字段
|
||||
*/
|
||||
private SharedString sqlSelect = new SharedString();
|
||||
|
||||
/**
|
||||
* 主表别名
|
||||
*/
|
||||
private SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(entity)
|
||||
@ -170,6 +176,11 @@ public class MyLambdaQuery<T> extends MyAbstractLambda<T, MyLambdaQuery<T>>
|
||||
return from.getStringValue();
|
||||
}
|
||||
|
||||
|
||||
public String getAlias() {
|
||||
return alias.getStringValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于生成嵌套 sql
|
||||
* <p>故 sqlSelect from不向下传递</p>
|
||||
@ -185,4 +196,13 @@ public class MyLambdaQuery<T> extends MyAbstractLambda<T, MyLambdaQuery<T>>
|
||||
super.clear();
|
||||
sqlSelect.toNull();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQuery<T> join(String keyWord, boolean condition, String joinSql) {
|
||||
if (condition) {
|
||||
from.setStringValue(from.getStringValue() + keyWord + joinSql);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@ -24,6 +25,11 @@ public class MyQuery<T> extends MyAbstractWrapper<T, String, MyQuery<T>>
|
||||
*/
|
||||
private SharedString sqlSelect = new SharedString();
|
||||
|
||||
/**
|
||||
* 主表别名
|
||||
*/
|
||||
private SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||
|
||||
public MyQuery() {
|
||||
this(null);
|
||||
}
|
||||
@ -84,6 +90,10 @@ public class MyQuery<T> extends MyAbstractWrapper<T, String, MyQuery<T>>
|
||||
return from.getStringValue();
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias.getStringValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个支持 lambda 函数写法的 wrapper
|
||||
*/
|
||||
|
@ -1,22 +1,32 @@
|
||||
package com.github.mybatisplus.query.interfaces;
|
||||
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
|
||||
public interface MyJoin<Children> {
|
||||
|
||||
default Children leftJoin(String joinSql) {
|
||||
return leftJoin(true, joinSql);
|
||||
}
|
||||
|
||||
Children leftJoin(boolean condition, String joinSql);
|
||||
default Children leftJoin(boolean condition, String joinSql) {
|
||||
return join(Constant.LEFT_JOIN, condition, joinSql);
|
||||
}
|
||||
|
||||
default Children rightJoin(String joinSql) {
|
||||
return rightJoin(true, joinSql);
|
||||
}
|
||||
|
||||
Children rightJoin(boolean condition, String joinSql);
|
||||
default Children rightJoin(boolean condition, String joinSql){
|
||||
return join(Constant.RIGHT_JOIN, condition, joinSql);
|
||||
}
|
||||
|
||||
default Children innerJoin(String joinSql) {
|
||||
return innerJoin(true, joinSql);
|
||||
}
|
||||
|
||||
Children innerJoin(boolean condition, String joinSql);
|
||||
default Children innerJoin(boolean condition, String joinSql){
|
||||
return join(Constant.INNER_JOIN, condition, joinSql);
|
||||
}
|
||||
|
||||
Children join(String keyWord, boolean condition, String joinSql);
|
||||
}
|
||||
|
@ -11,8 +11,6 @@ public interface Constant {
|
||||
*/
|
||||
String TABLE_ALIAS = "t";
|
||||
|
||||
String AS = " AS ";
|
||||
|
||||
String ON = " ON ";
|
||||
|
||||
String EQUALS = " = ";
|
||||
|
@ -3,7 +3,6 @@ package com.github.mybatisplus.toolkit;
|
||||
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.mybatisplus.func.MySFunction;
|
||||
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||
|
||||
/**
|
||||
@ -11,17 +10,20 @@ import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||
* @see LambdaUtils
|
||||
* @see PropertyNamer
|
||||
*/
|
||||
public class MyLambdaUtils {
|
||||
@SuppressWarnings("all")
|
||||
public final class MyLambdaUtils {
|
||||
|
||||
|
||||
public static <T> String getName(SFunction<T, ?> fn) {
|
||||
return PropertyNamer.methodToProperty(LambdaUtils.resolve(fn).getImplMethodName());
|
||||
}
|
||||
|
||||
public static <T> String getColumn(MySFunction<T, ?> fn) {
|
||||
public static <T> String getColumn(SFunction<T, ?> fn) {
|
||||
return StringUtils.camelToUnderline(getName(fn));
|
||||
}
|
||||
|
||||
public static <T> Class<T> getEntityClass(MySFunction<T, ?> fn) {
|
||||
return (Class<T>) LambdaUtils.resolve(fn).getImplClass();
|
||||
public static <T> Class<T> getEntityClass(SFunction<T, ?> fn) {
|
||||
return (Class<T>) LambdaUtils.resolve(fn).getInstantiatedType();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,39 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.mybatisplus.wrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
|
||||
import com.github.mybatisplus.toolkit.MyLambdaUtils;
|
||||
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
/**
|
||||
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper}
|
||||
* Lambda 语法使用 Wrapper
|
||||
* <p>统一处理解析 lambda 获取 column</p>
|
||||
*
|
||||
* @author hubin miemie HCL
|
||||
* @since 2017-05-26
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLambdaWrapper<T, Children>>
|
||||
extends MyAbstractWrapper<T, SFunction<T, ?>, Children> {
|
||||
extends MyAbstractWrapper<T, Children> {
|
||||
|
||||
private Map<String, ColumnCache> columnMap = null;
|
||||
private boolean initColumnMap = false;
|
||||
|
||||
/**
|
||||
* 参与连接的表<class,别名>
|
||||
*/
|
||||
protected Map<Class<?>, SubClass<?, ?>> subTable = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <X> String columnsToString(SFunction<X, ?>... columns) {
|
||||
return columnsToString(true, columns);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String columnToString(SFunction<T, ?> column) {
|
||||
return columnToString(column, true);
|
||||
protected <X> String columnToString(X column) {
|
||||
return columnToString((SFunction<?, ?>) column, true);
|
||||
}
|
||||
|
||||
protected String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
|
||||
return getColumn(LambdaUtils.resolve(column), onlyColumn);
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <X> String columnsToString(boolean onlyColumn, SFunction<X, ?>... columns) {
|
||||
return Arrays.stream(columns).map(i -> columnToString(i, onlyColumn)).collect(joining(StringPool.COMMA));
|
||||
}
|
||||
|
||||
protected String column2String(SFunction<?, ?> column, boolean onlyColumn) {
|
||||
return getColumn(LambdaUtils.resolve(column), onlyColumn);
|
||||
@Override
|
||||
protected <X> String columnsToString(X... columns) {
|
||||
return Arrays.stream(columns).map(i -> columnToString((SFunction<?, ?>) i, true)).collect(joining(StringPool.COMMA));
|
||||
}
|
||||
|
||||
protected String columnToString(SFunction<?, ?> column, boolean onlyColumn) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(MyLambdaUtils.getEntityClass(column));
|
||||
Assert.notNull(info, "can not find table for lambda");
|
||||
return info.getTableName() + StringPool.DOT + getColumn(LambdaUtils.resolve(column), onlyColumn);
|
||||
}
|
||||
|
||||
|
||||
@ -58,7 +99,14 @@ public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLamb
|
||||
}
|
||||
|
||||
private void tryInitCache(Class<?> lambdaClass) {
|
||||
columnMap = LambdaUtils.getColumnMap(lambdaClass);
|
||||
if (!initColumnMap) {
|
||||
final Class<T> entityClass = getEntityClass();
|
||||
if (entityClass != null) {
|
||||
lambdaClass = entityClass;
|
||||
}
|
||||
columnMap = LambdaUtils.getColumnMap(lambdaClass);
|
||||
initColumnMap = true;
|
||||
}
|
||||
Assert.notNull(columnMap, "can not find lambda cache for this entity [%s]", lambdaClass.getName());
|
||||
}
|
||||
|
||||
@ -68,4 +116,44 @@ public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLamb
|
||||
fieldName, lambdaClass.getName());
|
||||
return columnCache;
|
||||
}
|
||||
|
||||
public static class SubClass<L, R> {
|
||||
|
||||
private String tableAlias;
|
||||
|
||||
private SFunction<L, ?> left;
|
||||
|
||||
private SFunction<R, ?> right;
|
||||
|
||||
|
||||
public SubClass(String tableAlias, SFunction<L, ?> left, SFunction<R, ?> right) {
|
||||
this.tableAlias = tableAlias;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public String getTableAlias() {
|
||||
return tableAlias;
|
||||
}
|
||||
|
||||
public void setTableAlias(String tableAlias) {
|
||||
this.tableAlias = tableAlias;
|
||||
}
|
||||
|
||||
public SFunction<L, ?> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(SFunction<L, ?> left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public SFunction<R, ?> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(SFunction<R, ?> right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.mybatisplus.wrapper;
|
||||
|
||||
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.Join;
|
||||
import com.baomidou.mybatisplus.core.conditions.interfaces.Nested;
|
||||
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.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.mybatisplus.wrapper.interfaces.MyCompare;
|
||||
import com.github.mybatisplus.wrapper.interfaces.MyFunc;
|
||||
|
||||
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}
|
||||
* 查询条件封装
|
||||
*
|
||||
* @author hubin miemie HCL
|
||||
* @since 2017-05-26
|
||||
*/
|
||||
@SuppressWarnings({"serial", "unchecked"})
|
||||
public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper<T, R, Children>> extends Wrapper<T> {
|
||||
|
||||
public abstract class MyAbstractWrapper<T, Children extends MyAbstractWrapper<T, Children>> extends MyWrapper<T>
|
||||
implements MyCompare<Children>, Nested<Children, Children>, Join<Children>, MyFunc<Children> {
|
||||
|
||||
/**
|
||||
* 占位符
|
||||
@ -73,6 +105,280 @@ public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X, V> Children allEq(boolean condition, Map<SFunction<X, ?>, 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 <X, V> Children allEq(boolean condition, BiPredicate<SFunction<X, ?>, V> filter, Map<SFunction<X, ?>, 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 <X> Children eq(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return addCondition(condition, column, EQ, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children ne(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return addCondition(condition, column, NE, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children gt(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return addCondition(condition, column, GT, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children ge(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return addCondition(condition, column, GE, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children lt(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return addCondition(condition, column, LT, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children le(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return addCondition(condition, column, LE, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children like(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return likeValue(condition, LIKE, column, val, SqlLike.DEFAULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children notLike(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return likeValue(condition, NOT_LIKE, column, val, SqlLike.DEFAULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children likeLeft(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return likeValue(condition, LIKE, column, val, SqlLike.LEFT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children likeRight(boolean condition, SFunction<X, ?> column, Object val) {
|
||||
return likeValue(condition, LIKE, column, val, SqlLike.RIGHT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children between(boolean condition, SFunction<X, ?> column, Object val1, Object val2) {
|
||||
return doIt(condition, () -> columnToString(column), BETWEEN, () -> formatSql("{0}", val1), AND,
|
||||
() -> formatSql("{0}", val2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children notBetween(boolean condition, SFunction<X, ?> 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 <X> Children isNull(boolean condition, SFunction<X, ?> column) {
|
||||
return doIt(condition, () -> columnToString(column), IS_NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children isNotNull(boolean condition, SFunction<X, ?> column) {
|
||||
return doIt(condition, () -> columnToString(column), IS_NOT_NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children in(boolean condition, SFunction<X, ?> column, Collection<?> coll) {
|
||||
return doIt(condition, () -> columnToString(column), IN, inExpression(coll));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children notIn(boolean condition, SFunction<X, ?> column, Collection<?> coll) {
|
||||
return doIt(condition, () -> columnToString(column), NOT_IN, inExpression(coll));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children inSql(boolean condition, SFunction<X, ?> column, String inValue) {
|
||||
return doIt(condition, () -> columnToString(column), IN, () -> String.format("(%s)", inValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children notInSql(boolean condition, SFunction<X, ?> column, String inValue) {
|
||||
return doIt(condition, () -> columnToString(column), NOT_IN, () -> String.format("(%s)", inValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children groupBy(boolean condition, SFunction<X, ?>... columns) {
|
||||
if (ArrayUtils.isEmpty(columns)) {
|
||||
return typedThis;
|
||||
}
|
||||
return doIt(condition, GROUP_BY,
|
||||
() -> columns.length == 1 ? columnToString(columns[0]) : columnsToString(columns));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Children orderBy(boolean condition, boolean isAsc, SFunction<X, ?>... columns) {
|
||||
if (ArrayUtils.isEmpty(columns)) {
|
||||
return typedThis;
|
||||
}
|
||||
SqlKeyword mode = isAsc ? ASC : DESC;
|
||||
for (SFunction<X, ?> 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 <X> Children likeValue(boolean condition, SqlKeyword keyword, SFunction<X, ?> 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 <X> Children addCondition(boolean condition, SFunction<X, ?> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 子类返回一个自己的新对象
|
||||
*/
|
||||
@ -122,6 +428,16 @@ public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 必要的初始化
|
||||
*/
|
||||
@ -145,6 +461,20 @@ public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper
|
||||
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();
|
||||
@ -171,14 +501,26 @@ public abstract class MyAbstractWrapper<T, R, Children extends MyAbstractWrapper
|
||||
return expression;
|
||||
}
|
||||
|
||||
public Map<String, Object> getParamNameValuePairs() {
|
||||
return paramNameValuePairs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 columnName
|
||||
*/
|
||||
protected String columnToString(R column) {
|
||||
protected <X> String columnToString(X column) {
|
||||
return (String) column;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多字段转换为逗号 "," 分割字符串
|
||||
*
|
||||
* @param columns 多字段
|
||||
*/
|
||||
protected <X> String columnsToString(X... columns) {
|
||||
return Arrays.stream(columns).map(this::columnToString).collect(joining(StringPool.COMMA));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public Children clone() {
|
||||
|
@ -1,354 +0,0 @@
|
||||
package com.github.mybatisplus.wrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
||||
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
||||
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.ArrayUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.mybatisplus.func.MySFunction;
|
||||
import com.github.mybatisplus.toolkit.MyLambdaUtils;
|
||||
import com.github.mybatisplus.wrapper.interfaces.MyJoin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 链接查询子Query
|
||||
*
|
||||
* @author yulichang
|
||||
* @see com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||
* @since 2021/01/19
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class MyJoinLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyJoinLambdaQueryWrapper<T>>
|
||||
implements Query<MyJoinLambdaQueryWrapper<T>, T, SFunction<T, ?>>, MyJoin<MyJoinLambdaQueryWrapper<T>, T> {
|
||||
|
||||
protected SharedString sqlSelect = new SharedString();
|
||||
/**
|
||||
* select字段
|
||||
*/
|
||||
protected List<SelectColumn> selectColumnList = new ArrayList<>();
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
protected SharedString from = new SharedString();
|
||||
/**
|
||||
* 子表列表
|
||||
*/
|
||||
protected List<SubTable> classList = new ArrayList<>();
|
||||
/**
|
||||
* 表序号
|
||||
*/
|
||||
protected int rUid = 1;
|
||||
|
||||
|
||||
|
||||
public MyJoinLambdaQueryWrapper() {
|
||||
setEntity(null);
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
public MyJoinLambdaQueryWrapper(int rUid) {
|
||||
this.rUid = rUid;
|
||||
setEntity(null);
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
public MyJoinLambdaQueryWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||
SharedString lastSql, SharedString sqlComment) {
|
||||
setEntity(entity);
|
||||
setEntityClass(entityClass);
|
||||
this.paramNameSeq = paramNameSeq;
|
||||
this.paramNameValuePairs = paramNameValuePairs;
|
||||
this.expression = mergeSegments;
|
||||
this.sqlSelect = sqlSelect;
|
||||
this.lastSql = lastSql;
|
||||
this.sqlComment = sqlComment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MyJoinLambdaQueryWrapper<T> instance() {
|
||||
return new MyJoinLambdaQueryWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
super.clear();
|
||||
this.sqlSelect.toNull();
|
||||
this.selectColumnList.clear();
|
||||
this.from.toNull();
|
||||
this.classList.clear();
|
||||
this.rUid = 1;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
@Override
|
||||
public final MyJoinLambdaQueryWrapper<T> select(SFunction<T, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(columns)) {
|
||||
for (SFunction<T, ?> str : columns) {
|
||||
selectColumnList.add(new SelectColumn(rUid, columnToString(str), null, null));
|
||||
}
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有字段 (后置处理)
|
||||
*/
|
||||
public MyJoinLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||
setEntityClass(clazz);
|
||||
TableInfo info = TableInfoHelper.getTableInfo(getEntityClass());
|
||||
Assert.notNull(info, "can not find table to entity %s", clazz);
|
||||
info.getFieldList().forEach(s ->
|
||||
selectColumnList.add(new SelectColumn(this.rUid, s.getColumn(), null, null)));
|
||||
if (StringUtils.isNotBlank(info.getKeyColumn())) {
|
||||
selectColumnList.add(new SelectColumn(this.rUid, info.getKeyColumn(), null, null));
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段去重(同时使用select和as的情况下去除select中重复查询的列,优先使用as列作为查询字段)
|
||||
* 注意:
|
||||
* 只对selectDistinct前面的 select selectAll as(asCount ...)有效
|
||||
* 只对当前的表去重,对连接查询的其他表不生效,如需要在对应的表下使用此方法
|
||||
*/
|
||||
public MyJoinLambdaQueryWrapper<T> selectDistinct() {
|
||||
List<SelectColumn> collect = selectColumnList.stream().filter(i -> i.getUid() == rUid).collect(Collectors.toList());
|
||||
List<SelectColumn> distinctList = new ArrayList<>();
|
||||
|
||||
List<SelectColumn> collect1 = collect.stream().distinct().collect(Collectors.toList());
|
||||
if (collect.size() != collect1.size()) {
|
||||
collect.forEach(i -> {
|
||||
if (!collect1.contains(i)) {
|
||||
distinctList.add(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (SelectColumn x : collect1) {
|
||||
if (x.getAs() != null) {
|
||||
for (SelectColumn y : collect1) {
|
||||
if (x.getUid() == y.getUid() && x.getColumn().equals(y.getColumn()) && y.getAs() == null) {
|
||||
distinctList.add(y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selectColumnList.removeIf(distinctList::contains);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyJoinLambdaQueryWrapper<T> select(Predicate<TableFieldInfo> predicate) {
|
||||
return select(getEntityClass(), predicate);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MyJoinLambdaQueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||
setEntityClass(entityClass);
|
||||
TableInfo info = TableInfoHelper.getTableInfo(getEntityClass());
|
||||
Assert.notNull(info, "can not find table to entity %s", entityClass);
|
||||
info.getFieldList().stream().filter(predicate).forEach(s ->
|
||||
selectColumnList.add(new SelectColumn(this.rUid, s.getColumn(), null, null)));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, TE, RE> MyJoinLambdaQueryWrapper<T> join(boolean condition,
|
||||
String alias,
|
||||
String keyWord,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
if (condition) {
|
||||
setEntityClass(MyLambdaUtils.getEntityClass(leftCondition));
|
||||
int childrenId = rUid + 1;
|
||||
Class<R> clazz = MyLambdaUtils.getEntityClass(rightCondition);
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(info, "can not find table to entity %s", clazz);
|
||||
classList.add(new SubTable(alias, keyWord, rUid, MyLambdaUtils.getColumn(leftCondition), childrenId, MyLambdaUtils.getColumn(rightCondition), info.getTableName()));
|
||||
MyJoinLambdaQueryWrapper<R> apply = (MyJoinLambdaQueryWrapper<R>) rightWrapper.apply(new MyJoinLambdaQueryWrapper<>(childrenId));
|
||||
classList.addAll(apply.classList);
|
||||
this.selectColumnList.addAll(apply.selectColumnList);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连表条件
|
||||
*/
|
||||
public static class SubTable {
|
||||
|
||||
private String alias;
|
||||
|
||||
private String keyWord;
|
||||
|
||||
private int leftUid;
|
||||
|
||||
private String leftColumn;
|
||||
|
||||
private int rightUid;
|
||||
|
||||
private String rightColumn;
|
||||
|
||||
private String rightTableName;
|
||||
|
||||
public SubTable(String alias, String keyWord, int leftUid, String leftColumn, int rightUid, String rightColumn, String rightTableName) {
|
||||
this.alias = alias;
|
||||
this.keyWord = keyWord;
|
||||
this.leftUid = leftUid;
|
||||
this.leftColumn = leftColumn;
|
||||
this.rightUid = rightUid;
|
||||
this.rightColumn = rightColumn;
|
||||
this.rightTableName = rightTableName;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
public void setAlias(String alias) {
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
public String getKeyWord() {
|
||||
return keyWord;
|
||||
}
|
||||
|
||||
public void setKeyWord(String keyWord) {
|
||||
this.keyWord = keyWord;
|
||||
}
|
||||
|
||||
public int getLeftUid() {
|
||||
return leftUid;
|
||||
}
|
||||
|
||||
public void setLeftUid(int leftUid) {
|
||||
this.leftUid = leftUid;
|
||||
}
|
||||
|
||||
public String getLeftColumn() {
|
||||
return leftColumn;
|
||||
}
|
||||
|
||||
public void setLeftColumn(String leftColumn) {
|
||||
this.leftColumn = leftColumn;
|
||||
}
|
||||
|
||||
public int getRightUid() {
|
||||
return rightUid;
|
||||
}
|
||||
|
||||
public void setRightUid(int rightUid) {
|
||||
this.rightUid = rightUid;
|
||||
}
|
||||
|
||||
public String getRightColumn() {
|
||||
return rightColumn;
|
||||
}
|
||||
|
||||
public void setRightColumn(String rightColumn) {
|
||||
this.rightColumn = rightColumn;
|
||||
}
|
||||
|
||||
public String getRightTableName() {
|
||||
return rightTableName;
|
||||
}
|
||||
|
||||
public void setRightTableName(String rightTableName) {
|
||||
this.rightTableName = rightTableName;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* select查询字段
|
||||
*/
|
||||
public static class SelectColumn {
|
||||
|
||||
private int uid;
|
||||
|
||||
private String column;
|
||||
|
||||
private String as;
|
||||
|
||||
private SelectFunc function;
|
||||
|
||||
public SelectColumn() {
|
||||
}
|
||||
|
||||
public SelectColumn(int uid, String column, String as, SelectFunc function) {
|
||||
this.uid = uid;
|
||||
this.column = column;
|
||||
this.as = as;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public int getUid() {
|
||||
return uid;
|
||||
}
|
||||
|
||||
public void setUid(int uid) {
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String getAs() {
|
||||
return as;
|
||||
}
|
||||
|
||||
public void setAs(String as) {
|
||||
this.as = as;
|
||||
}
|
||||
|
||||
public SelectFunc getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
public void setFunction(SelectFunc function) {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof SelectColumn)) {
|
||||
return false;
|
||||
}
|
||||
SelectColumn v = (SelectColumn) o;
|
||||
return v.getUid() == this.uid
|
||||
&& eq(v.getColumn(), this.column)
|
||||
&& eq(v.getAs(), this.as)
|
||||
&& eq(v.getFunction(), this.function);
|
||||
}
|
||||
|
||||
private boolean eq(Object o1, Object o2) {
|
||||
if (o1 == null && o2 == null) {
|
||||
return true;
|
||||
}
|
||||
if (o1 == null || o2 == null) {
|
||||
return false;
|
||||
}
|
||||
return o1.equals(o2);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,506 +1,233 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.mybatisplus.wrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
||||
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
||||
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.exceptions.MybatisPlusException;
|
||||
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 com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.mybatisplus.func.MySFunction;
|
||||
import com.github.mybatisplus.wrapper.interfaces.MyLambdaJoin;
|
||||
import com.github.mybatisplus.wrapper.interfaces.MySFunctionQuery;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
import com.github.mybatisplus.toolkit.MyLambdaUtils;
|
||||
import com.github.mybatisplus.wrapper.interfaces.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
|
||||
import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 连接查询Query
|
||||
* 只允许主表有条件查询,内部的表只允许select和继续连表的功能,
|
||||
* 所以将条件查询接口在此实现 结构臃肿,有待进一步优化
|
||||
* Lambda 语法使用 Wrapper
|
||||
*
|
||||
* @author yulichang
|
||||
* @see com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
|
||||
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Compare
|
||||
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Join
|
||||
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Nested
|
||||
* @see com.baomidou.mybatisplus.core.conditions.interfaces.Func
|
||||
* @since 2021/01/19
|
||||
* @author hubin miemie HCL
|
||||
* @since 2017-05-26
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
@SuppressWarnings("serial")
|
||||
public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambdaQueryWrapper<T>>
|
||||
implements
|
||||
Query<MyLambdaQueryWrapper<T>, T, SFunction<T, ?>>,
|
||||
MyJoin<MyLambdaQueryWrapper<T>, T>,
|
||||
MyCompare<MyLambdaQueryWrapper<T>>,
|
||||
MyNested<MyLambdaQueryWrapper<T>, MyLambdaQueryWrapper<T>>,
|
||||
MyFunc<MyLambdaQueryWrapper<T>>,
|
||||
MyMPJoin<MyLambdaQueryWrapper<T>> {
|
||||
implements MySFunctionQuery<MyLambdaQueryWrapper<T>>, MyLambdaJoin<MyLambdaQueryWrapper<T>> {
|
||||
|
||||
/**
|
||||
* 查询字段
|
||||
*/
|
||||
private SharedString sqlSelect = new SharedString();
|
||||
|
||||
private MyJoinLambdaQueryWrapper<T> wrapper = new MyJoinLambdaQueryWrapper<>();
|
||||
/**
|
||||
* 查询表
|
||||
*/
|
||||
private SharedString from = new SharedString();
|
||||
|
||||
|
||||
/**
|
||||
* 表别名初始序号
|
||||
* 主表别名
|
||||
*/
|
||||
public static final int TABLE_ALIAS_INDEX = 0;
|
||||
private SharedString alias = new SharedString();
|
||||
|
||||
|
||||
private List<SelectColumn> selectColumns = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(entity)
|
||||
*/
|
||||
public MyLambdaQueryWrapper() {
|
||||
this(null);
|
||||
this((T) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(entity)
|
||||
*/
|
||||
public MyLambdaQueryWrapper(T entity) {
|
||||
wrapper.rUid = TABLE_ALIAS_INDEX;
|
||||
wrapper.setEntity(entity);
|
||||
super.setEntity(entity);
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(entity)
|
||||
*/
|
||||
public MyLambdaQueryWrapper(Class<T> entityClass) {
|
||||
super.setEntityClass(entityClass);
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(...)
|
||||
*/
|
||||
MyLambdaQueryWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
|
||||
int rUid, List<MyJoinLambdaQueryWrapper.SelectColumn> selectColumns, List<MyJoinLambdaQueryWrapper.SubTable> classList) {
|
||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
|
||||
super.setEntity(entity);
|
||||
super.setEntityClass(entityClass);
|
||||
this.paramNameSeq = paramNameSeq;
|
||||
this.paramNameValuePairs = paramNameValuePairs;
|
||||
this.expression = mergeSegments;
|
||||
this.wrapper.sqlSelect = sqlSelect;
|
||||
this.sqlSelect = sqlSelect;
|
||||
this.lastSql = lastSql;
|
||||
this.sqlComment = sqlComment;
|
||||
this.sqlFirst = sqlFirst;
|
||||
|
||||
wrapper.rUid = rUid;
|
||||
wrapper.selectColumnList = selectColumns;
|
||||
wrapper.classList = classList;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MyLambdaQueryWrapper<T> instance() {
|
||||
return new MyLambdaQueryWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
|
||||
wrapper.rUid, wrapper.selectColumnList, wrapper.classList);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <R, TE, RE> MyLambdaQueryWrapper<T> join(boolean condition,
|
||||
String alias,
|
||||
String keyWord,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
if (condition) {
|
||||
setEntityClass(MyLambdaUtils.getEntityClass(leftCondition));
|
||||
Class<R> clazz = MyLambdaUtils.getEntityClass(rightCondition);
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(info, "can not find table to entity %s", clazz);
|
||||
wrapper.classList.add(new MyJoinLambdaQueryWrapper.SubTable(alias,
|
||||
keyWord,
|
||||
wrapper.rUid,
|
||||
MyLambdaUtils.getColumn(leftCondition),
|
||||
wrapper.classList.size() + 1,
|
||||
MyLambdaUtils.getColumn(rightCondition),
|
||||
info.getTableName()));
|
||||
MyJoinLambdaQueryWrapper<R> apply = rightWrapper.apply(new MyJoinLambdaQueryWrapper<>(wrapper.classList.size()));
|
||||
wrapper.classList.addAll(apply.classList);
|
||||
wrapper.selectColumnList.addAll(apply.selectColumnList);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* SELECT 部分 SQL 设置
|
||||
*
|
||||
* @param columns 查询字段
|
||||
*/
|
||||
@SafeVarargs
|
||||
@Override
|
||||
public final MyLambdaQueryWrapper<T> select(SFunction<T, ?>... columns) {
|
||||
wrapper.select(columns);
|
||||
return this;
|
||||
public final <S> MyLambdaQueryWrapper<T> select(SFunction<S, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(columns)) {
|
||||
for (SFunction<S, ?> s : columns) {
|
||||
Class<S> clazz = MyLambdaUtils.getEntityClass(s);
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
selectColumns.add(new SelectColumn(clazz, info.getTableName(), MyLambdaUtils.getColumn(s)));
|
||||
}
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> select(Predicate<TableFieldInfo> predicate) {
|
||||
wrapper.select(predicate);
|
||||
return this;
|
||||
public <E> MyLambdaQueryWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(info, "table can not be find");
|
||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
|
||||
i -> selectColumns.add(new SelectColumn(entityClass, info.getTableName(), i.getColumn())));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||
wrapper.select(entityClass, predicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public MyLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||
wrapper.selectAll(clazz);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyLambdaQueryWrapper<T> selectDistinct() {
|
||||
wrapper.selectDistinct();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSqlSelect() {
|
||||
if (StringUtils.isBlank(wrapper.sqlSelect.getStringValue()) && CollectionUtils.isNotEmpty(wrapper.selectColumnList)) {
|
||||
List<String> collect = new ArrayList<>();
|
||||
wrapper.selectColumnList.forEach(c -> {
|
||||
String s = Constant.TABLE_ALIAS + (c.getUid() == TABLE_ALIAS_INDEX ? "" : c.getUid()) + StringPool.DOT + c.getColumn();
|
||||
if (c.getFunction() != null) {
|
||||
s = String.format(c.getFunction().getSql(), s);
|
||||
}
|
||||
if (Objects.isNull(c.getAs())) {
|
||||
collect.add(s);
|
||||
} else {
|
||||
collect.add(s + Constant.AS + c.getAs());
|
||||
}
|
||||
});
|
||||
wrapper.sqlSelect.setStringValue(String.join(StringPool.COMMA, collect));
|
||||
if (StringUtils.isBlank(sqlSelect.getStringValue())) {
|
||||
String s = selectColumns.stream().map(i -> i.getTableName() + StringPool.DOT + i.getColumnName()).collect(Collectors.joining(StringPool.COMMA));
|
||||
sqlSelect.setStringValue(s);
|
||||
}
|
||||
return wrapper.sqlSelect.getStringValue();
|
||||
return sqlSelect.getStringValue();
|
||||
}
|
||||
|
||||
|
||||
public String getFrom() {
|
||||
return from.getStringValue();
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias.getStringValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于生成嵌套 sql
|
||||
* <p>故 sqlSelect 不向下传递</p>
|
||||
*/
|
||||
@Override
|
||||
protected MyLambdaQueryWrapper<T> instance() {
|
||||
return new MyLambdaQueryWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
super.clear();
|
||||
sqlSelect.toNull();
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
if (StringUtils.isNotBlank(wrapper.from.getStringValue())) {
|
||||
return wrapper.from.getStringValue();
|
||||
@Override
|
||||
public <L, X> MyLambdaQueryWrapper<T> join(String keyWord, boolean condition, Class<L> clazz, SFunction<L, ?> left, SFunction<X, ?> right) {
|
||||
if (condition) {
|
||||
TableInfo leftInfo = TableInfoHelper.getTableInfo(clazz);
|
||||
TableInfo rightInfo = TableInfoHelper.getTableInfo(MyLambdaUtils.getEntityClass(right));
|
||||
|
||||
String s = keyWord + leftInfo.getTableName() + Constant.ON + leftInfo.getTableName() + StringPool.DOT
|
||||
+ MyLambdaUtils.getColumn(left) + Constant.EQUALS + rightInfo.getTableName() + StringPool.DOT
|
||||
+ MyLambdaUtils.getColumn(right);
|
||||
|
||||
if (StringUtils.isBlank(from.getStringValue())) {
|
||||
from.setStringValue(s);
|
||||
} else {
|
||||
from.setStringValue(from.getStringValue() + s);
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
wrapper.classList.forEach(right ->
|
||||
sb.append(right.getKeyWord()).append(right.getRightTableName())
|
||||
.append(StringPool.SPACE)
|
||||
.append(Constant.TABLE_ALIAS)
|
||||
.append(right.getRightUid() == TABLE_ALIAS_INDEX ? StringPool.EMPTY : right.getRightUid())
|
||||
.append(Constant.ON)
|
||||
.append(Constant.TABLE_ALIAS)
|
||||
.append(right.getLeftUid() == TABLE_ALIAS_INDEX ? StringPool.EMPTY : right.getLeftUid())
|
||||
.append(StringPool.DOT)
|
||||
.append(right.getLeftColumn())
|
||||
.append(Constant.EQUALS)
|
||||
.append(Constant.TABLE_ALIAS)
|
||||
.append(right.getRightUid() == TABLE_ALIAS_INDEX ? StringPool.EMPTY : right.getRightUid())
|
||||
.append(StringPool.DOT)
|
||||
.append(right.getRightColumn())
|
||||
.append(StringPool.SPACE)
|
||||
);
|
||||
wrapper.from.setStringValue(sb.toString());
|
||||
return wrapper.from.getStringValue();
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表前缀
|
||||
* select字段
|
||||
*/
|
||||
private String getClassTablePrefix(Class<?> clazz) {
|
||||
if (getEntityClass() == clazz) {
|
||||
return Constant.TABLE_ALIAS;
|
||||
} else {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(info, "can not find table to entity %s", clazz);
|
||||
String tableName = info.getTableName();
|
||||
for (MyJoinLambdaQueryWrapper.SubTable sub : wrapper.classList) {
|
||||
if (sub.getRightTableName().equals(tableName)) {
|
||||
return Constant.TABLE_ALIAS + sub.getRightUid();
|
||||
}
|
||||
}
|
||||
public static class SelectColumn {
|
||||
|
||||
private Class<?> clazz;
|
||||
|
||||
private String tableName;
|
||||
|
||||
private String columnName;
|
||||
|
||||
|
||||
public SelectColumn(Class<?> clazz, String tableName, String columnName) {
|
||||
this.clazz = clazz;
|
||||
this.tableName = tableName;
|
||||
this.columnName = columnName;
|
||||
}
|
||||
throw new MybatisPlusException("table not find");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取column
|
||||
*/
|
||||
private <E, F> String getColumn(MySFunction<E, F> column, String alias) {
|
||||
if (alias != null) {
|
||||
if (alias.equals(Constant.TABLE_ALIAS)) {
|
||||
return Constant.TABLE_ALIAS + StringPool.DOT + column2String(column, true);
|
||||
}
|
||||
for (MyJoinLambdaQueryWrapper.SubTable sub : wrapper.classList) {
|
||||
if (alias.equals(sub.getAlias())) {
|
||||
return Constant.TABLE_ALIAS + sub.getRightUid() + StringPool.DOT + column2String(column, true);
|
||||
}
|
||||
}
|
||||
|
||||
public Class<?> getClazz() {
|
||||
return clazz;
|
||||
}
|
||||
return getClassTablePrefix(MyLambdaUtils.getEntityClass(column)) + StringPool.DOT + column2String(column, true);
|
||||
}
|
||||
|
||||
private <E, F> String getColumn(String alias, MySFunction<E, F>... column) {
|
||||
return Arrays.stream(column).map(i -> getColumn(i, alias)).collect(joining(StringPool.COMMA));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> eq(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), EQ,
|
||||
() -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F, X, Y> MyLambdaQueryWrapper<T> eq(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), EQ, () -> getColumn(val, as));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> ne(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), NE, () -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F, X, Y> MyLambdaQueryWrapper<T> ne(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), NE, () -> getColumn(val, as));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> gt(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), GT, () -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F, X, Y> MyLambdaQueryWrapper<T> gt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), GT, () -> getColumn(val, as));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> ge(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), GE, () -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F, X, Y> MyLambdaQueryWrapper<T> ge(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), GE, () -> getColumn(val, as));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> lt(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), LT, () -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F, X, Y> MyLambdaQueryWrapper<T> lt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), LT, () -> getColumn(val, as));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> le(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), LE, () -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F, X, Y> MyLambdaQueryWrapper<T> le(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return doIt(condition, () -> getColumn(column, alias), LE, () -> getColumn(val, as));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> between(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return doIt(condition, () -> getColumn(column, alias), BETWEEN, () -> formatSql("{0}", val1), AND,
|
||||
() -> formatSql("{0}", val2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> notBetween(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return doIt(condition, () -> getColumn(column, alias), NOT_BETWEEN, () -> formatSql("{0}", val1), AND,
|
||||
() -> formatSql("{0}", val2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> like(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return likeValue(condition, LIKE, getColumn(column, alias), val, SqlLike.DEFAULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> notLike(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return likeValue(condition, NOT_LIKE, getColumn(column, alias), val, SqlLike.DEFAULT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> likeLeft(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return likeValue(condition, LIKE, getColumn(column, alias), val, SqlLike.LEFT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> likeRight(boolean condition, String alias, MySFunction<E, F> column, Object val) {
|
||||
return likeValue(condition, LIKE, getColumn(column, alias), val, SqlLike.RIGHT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> and(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||
return and(condition).addNestedCondition(condition, consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> or(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||
return or(condition).addNestedCondition(condition, consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> nested(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||
return addNestedCondition(condition, consumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> not(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||
return not(condition).addNestedCondition(condition, consumer);
|
||||
}
|
||||
|
||||
|
||||
private MyLambdaQueryWrapper<T> and(boolean condition) {
|
||||
return doIt(condition, AND);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> or(boolean condition) {
|
||||
return doIt(condition, OR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> apply(boolean condition, String applySql, Object... value) {
|
||||
return doIt(condition, APPLY, () -> formatSql(applySql, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> last(boolean condition, String lastSql) {
|
||||
if (condition) {
|
||||
this.lastSql.setStringValue(StringPool.SPACE + lastSql);
|
||||
public void setClazz(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> comment(boolean condition, String comment) {
|
||||
if (condition) {
|
||||
this.sqlComment.setStringValue(comment);
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> first(boolean condition, String firstSql) {
|
||||
if (condition) {
|
||||
this.sqlFirst.setStringValue(firstSql);
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> exists(boolean condition, String existsSql) {
|
||||
return doIt(condition, EXISTS, () -> String.format("(%s)", existsSql));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> notExists(boolean condition, String existsSql) {
|
||||
return not(condition).exists(condition, existsSql);
|
||||
}
|
||||
|
||||
private MyLambdaQueryWrapper<T> not(boolean condition) {
|
||||
return doIt(condition, NOT);
|
||||
}
|
||||
|
||||
private MyLambdaQueryWrapper<T> doIt(boolean condition, ISqlSegment... sqlSegments) {
|
||||
if (condition) {
|
||||
expression.add(sqlSegments);
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private MyLambdaQueryWrapper<T> likeValue(boolean condition, SqlKeyword keyword, String column, Object val, SqlLike sqlLike) {
|
||||
return doIt(condition, () -> column, keyword, () -> formatSql("{0}", SqlUtils.concatLike(val, sqlLike)));
|
||||
}
|
||||
|
||||
protected MyLambdaQueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
|
||||
return doIt(condition, () -> column, sqlKeyword, () -> formatSql("{0}", val));
|
||||
}
|
||||
|
||||
private MyLambdaQueryWrapper<T> addNestedCondition(boolean condition, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||
if (condition) {
|
||||
final MyLambdaQueryWrapper<T> instance = instance();
|
||||
consumer.accept(instance);
|
||||
return doIt(true, APPLY, instance);
|
||||
public void setColumnName(String columnName) {
|
||||
this.columnName = columnName;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private ISqlSegment inExpression(Collection<?> value) {
|
||||
return () -> value.stream().map(i -> formatSql("{0}", i))
|
||||
.collect(joining(StringPool.COMMA, StringPool.LEFT_BRACKET, StringPool.RIGHT_BRACKET));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> isNull(boolean condition, String alias, MySFunction<E, F> column) {
|
||||
return doIt(condition, () -> getColumn(column, alias), IS_NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> isNotNull(boolean condition, String alias, MySFunction<E, F> column) {
|
||||
return doIt(condition, () -> getColumn(column, alias), IS_NOT_NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> in(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||
return doIt(condition, () -> getColumn(column, alias), IN, inExpression(coll));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> notIn(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||
return doIt(condition, () -> getColumn(column, alias), NOT_IN, inExpression(coll));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> inSql(boolean condition, String alias, MySFunction<E, F> column, String inValue) {
|
||||
return doIt(condition, () -> getColumn(column, alias), IN, () -> String.format("(%s)", inValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> notInSql(boolean condition, String alias, MySFunction<E, F> column, String inValue) {
|
||||
return doIt(condition, () -> getColumn(column, alias), NOT_IN, () -> String.format("(%s)", inValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> groupBy(boolean condition, String alias, MySFunction<E, F>... columns) {
|
||||
if (ArrayUtils.isEmpty(columns)) {
|
||||
return this;
|
||||
}
|
||||
return doIt(condition, GROUP_BY,
|
||||
() -> columns.length == 1 ? getColumn(columns[0], alias) : getColumn(alias, columns));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E, F> MyLambdaQueryWrapper<T> orderBy(boolean condition, String alias, boolean isAsc, MySFunction<E, F>... columns) {
|
||||
if (ArrayUtils.isEmpty(columns)) {
|
||||
return this;
|
||||
}
|
||||
SqlKeyword mode = isAsc ? ASC : DESC;
|
||||
for (MySFunction<E, F> column : columns) {
|
||||
doIt(condition, ORDER_BY, () -> getColumn(column, alias), mode);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> having(boolean condition, String alias, String sqlHaving, Object... params) {
|
||||
return doIt(condition, HAVING, () -> formatSqlIfNeed(condition, sqlHaving, params));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyLambdaQueryWrapper<T> func(boolean condition, String alias, Consumer<MyLambdaQueryWrapper<T>> consumer) {
|
||||
if (condition) {
|
||||
consumer.accept(this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
170
src/main/java/com/github/mybatisplus/wrapper/MyWrapper.java
Normal file
170
src/main/java/com/github/mybatisplus/wrapper/MyWrapper.java
Normal file
@ -0,0 +1,170 @@
|
||||
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,27 +0,0 @@
|
||||
package com.github.mybatisplus.wrapper;
|
||||
|
||||
public enum SelectFunc {
|
||||
|
||||
SUM("SUM( %s )", "求和"),
|
||||
COUNT("COUNT( %s )", "数量"),
|
||||
AVG("AVG( %s )", "平均值"),
|
||||
MAX("MAX( %s )", "最大值"),
|
||||
MIN("MIN( %s )", "最小值"),
|
||||
DATE_FORMAT("DATE_FORMAT( %s ,'%Y-%m-%d %H:%i:%s')", "日期格式化");
|
||||
|
||||
private final String sql;
|
||||
private final String desc;
|
||||
|
||||
SelectFunc(String sql, String desc) {
|
||||
this.sql = sql;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
@ -1,422 +1,287 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import com.github.mybatisplus.func.MySFunction;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
/**
|
||||
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Compare}
|
||||
* 查询条件封装
|
||||
* <p>比较值</p>
|
||||
*
|
||||
* @author hubin miemie HCL
|
||||
* @since 2017-05-26
|
||||
*/
|
||||
public interface MyCompare<Children> extends Serializable {
|
||||
|
||||
/* 遗弃allEq */
|
||||
|
||||
/* ***************************************** eq start ********************************************* */
|
||||
|
||||
<E, F> Children eq(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
default <E, F> Children eq(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return eq(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children eq(String alias, MySFunction<E, F> column, Object val) {
|
||||
return eq(true, alias, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children eq(MySFunction<E, F> column, Object val) {
|
||||
return eq(true, null, column, val);
|
||||
}
|
||||
|
||||
<E, F, X, Y> Children eq(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||
|
||||
default <E, F, X, Y> Children eq(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return eq(true, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children eq(Boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return eq(condition, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children eq(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return eq(true, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children eq(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return eq(true, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children eq(Boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return eq(condition, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children eq(Boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return eq(condition, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children eq(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return eq(true, alias, column, as, val);
|
||||
}
|
||||
|
||||
/* ***************************************** eq end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** ne start ********************************************* */
|
||||
|
||||
default <E, F> Children ne(MySFunction<E, F> column, Object val) {
|
||||
return ne(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children ne(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return ne(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children ne(String alias, MySFunction<E, F> column, Object val) {
|
||||
return ne(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children ne(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
default <E, F, X, Y> Children ne(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ne(true, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ne(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ne(condition, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ne(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ne(true, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ne(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return ne(true, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ne(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ne(condition, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ne(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return ne(true, alias, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ne(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return ne(condition, null, column, as, val);
|
||||
}
|
||||
|
||||
<E, F, X, Y> Children ne(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||
|
||||
/* ***************************************** ne end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** gt start ********************************************* */
|
||||
|
||||
default <E, F> Children gt(MySFunction<E, F> column, Object val) {
|
||||
return gt(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children gt(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return gt(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children gt(String alias, MySFunction<E, F> column, Object val) {
|
||||
return gt(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children gt(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
default <E, F, X, Y> Children gt(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return gt(true, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children gt(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return gt(condition, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children gt(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return gt(true, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children gt(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return gt(true, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children gt(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return gt(condition, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children gt(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return gt(true, alias, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children gt(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return gt(condition, null, column, as, val);
|
||||
}
|
||||
|
||||
<E, F, X, Y> Children gt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||
|
||||
|
||||
/* ***************************************** gt end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** ge start ********************************************* */
|
||||
|
||||
default <E, F> Children ge(MySFunction<E, F> column, Object val) {
|
||||
return ge(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children ge(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return ge(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children ge(String alias, MySFunction<E, F> column, Object val) {
|
||||
return ge(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children ge(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
default <E, F, X, Y> Children ge(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ge(true, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ge(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ge(condition, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ge(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ge(true, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ge(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return ge(true, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ge(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return ge(condition, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ge(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return ge(true, alias, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children ge(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return ge(condition, null, column, as, val);
|
||||
}
|
||||
|
||||
<E, F, X, Y> Children ge(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||
|
||||
/* ***************************************** ge end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** lt start ********************************************* */
|
||||
|
||||
default <E, F> Children lt(MySFunction<E, F> column, Object val) {
|
||||
return lt(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children lt(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return lt(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children lt(String alias, MySFunction<E, F> column, Object val) {
|
||||
return lt(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children lt(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
default <E, F, X, Y> Children lt(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return lt(true, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children lt(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return lt(condition, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children lt(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return lt(true, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children lt(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return lt(true, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children lt(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return lt(condition, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children lt(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return lt(true, alias, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children lt(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return lt(condition, null, column, as, val);
|
||||
}
|
||||
|
||||
<E, F, X, Y> Children lt(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||
|
||||
/* ***************************************** lt end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** lt start ********************************************* */
|
||||
|
||||
default <E, F> Children le(MySFunction<E, F> column, Object val) {
|
||||
return le(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children le(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return le(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children le(String alias, MySFunction<E, F> column, Object val) {
|
||||
return le(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children le(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
default <E, F, X, Y> Children le(MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return le(true, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children le(boolean condition, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return le(condition, null, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children le(String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return le(true, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children le(MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return le(true, null, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children le(boolean condition, String alias, MySFunction<E, F> column, MySFunction<X, Y> val) {
|
||||
return le(condition, alias, column, null, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children le(String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return le(true, alias, column, as, val);
|
||||
}
|
||||
|
||||
default <E, F, X, Y> Children le(boolean condition, MySFunction<E, F> column, String as, MySFunction<X, Y> val) {
|
||||
return le(condition, null, column, as, val);
|
||||
}
|
||||
|
||||
<E, F, X, Y> Children le(boolean condition, String alias, MySFunction<E, F> column, String as, MySFunction<X, Y> val);
|
||||
|
||||
/* ***************************************** le end ********************************************* */
|
||||
|
||||
/* ***************************************** between start ********************************************* */
|
||||
|
||||
default <E, F> Children between(MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return between(true, null, column, val1, val2);
|
||||
}
|
||||
|
||||
default <E, F> Children between(boolean condition, MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return between(condition, null, column, val1, val2);
|
||||
}
|
||||
|
||||
default <E, F> Children between(String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return between(true, alias, column, val1, val2);
|
||||
}
|
||||
|
||||
<E, F> Children between(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2);
|
||||
|
||||
/* ***************************************** between end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** notBetween start ********************************************* */
|
||||
|
||||
default <E, F> Children notBetween(MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return notBetween(true, null, column, val1, val2);
|
||||
}
|
||||
|
||||
default <E, F> Children notBetween(boolean condition, MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return notBetween(condition, null, column, val1, val2);
|
||||
}
|
||||
|
||||
default <E, F> Children notBetween(String alias, MySFunction<E, F> column, Object val1, Object val2) {
|
||||
return notBetween(true, alias, column, val1, val2);
|
||||
}
|
||||
|
||||
<E, F> Children notBetween(boolean condition, String alias, MySFunction<E, F> column, Object val1, Object val2);
|
||||
|
||||
/* ***************************************** notBetween end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** like start ********************************************* */
|
||||
|
||||
default <E, F> Children like(MySFunction<E, F> column, Object val) {
|
||||
return like(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children like(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return like(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children like(String alias, MySFunction<E, F> column, Object val) {
|
||||
return like(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children like(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
/* ***************************************** like end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** notLike start ********************************************* */
|
||||
|
||||
default <E, F> Children notLike(MySFunction<E, F> column, Object val) {
|
||||
return notLike(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children notLike(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return notLike(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children notLike(String alias, MySFunction<E, F> column, Object val) {
|
||||
return notLike(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children notLike(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
/* ***************************************** notLike end ********************************************* */
|
||||
|
||||
|
||||
/* ***************************************** likeLeft start ********************************************* */
|
||||
|
||||
default <E, F> Children likeLeft(MySFunction<E, F> column, Object val) {
|
||||
return likeLeft(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children likeLeft(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return likeLeft(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children likeLeft(String alias, MySFunction<E, F> column, Object val) {
|
||||
return likeLeft(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children likeLeft(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
/* ***************************************** likeLeft end ********************************************* */
|
||||
|
||||
|
||||
|
||||
/* ***************************************** likeRight start ********************************************* */
|
||||
|
||||
default <E, F> Children likeRight(MySFunction<E, F> column, Object val) {
|
||||
return likeRight(true, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children likeRight(boolean condition, MySFunction<E, F> column, Object val) {
|
||||
return likeRight(condition, null, column, val);
|
||||
}
|
||||
|
||||
default <E, F> Children likeRight(String alias, MySFunction<E, F> column, Object val) {
|
||||
return likeRight(true, alias, column, val);
|
||||
}
|
||||
|
||||
<E, F> Children likeRight(boolean condition, String alias, MySFunction<E, F> column, Object val);
|
||||
|
||||
/* ***************************************** likeRight end ********************************************* */
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, V> Children allEq(Map<SFunction<R,?>, V> params) {
|
||||
return allEq(params, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, V> Children allEq(Map<SFunction<R,?>, V> params, boolean null2IsNull) {
|
||||
return allEq(true, params, null2IsNull);
|
||||
}
|
||||
|
||||
/**
|
||||
* map 所有非空属性等于 =
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param params map 类型的参数, key 是字段名, value 是字段值
|
||||
* @param null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段\
|
||||
* @return children
|
||||
*/
|
||||
<R, V> Children allEq(boolean condition, Map<SFunction<R,?>, V> params, boolean null2IsNull);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, V> Children allEq(BiPredicate<SFunction<R,?>, V> filter, Map<SFunction<R,?>, V> params) {
|
||||
return allEq(filter, params, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, V> Children allEq(BiPredicate<SFunction<R,?>, V> filter, Map<SFunction<R,?>, V> params, boolean null2IsNull) {
|
||||
return allEq(true, filter, params, null2IsNull);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段过滤接口,传入多参数时允许对参数进行过滤
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param filter 返回 true 来允许字段传入比对条件中
|
||||
* @param params map 类型的参数, key 是字段名, value 是字段值
|
||||
* @param null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段
|
||||
* @return children
|
||||
*/
|
||||
<R, V> Children allEq(boolean condition, BiPredicate<SFunction<R,?>, V> filter, Map<SFunction<R,?>, V> params, boolean null2IsNull);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children eq(SFunction<R,?> column, Object val) {
|
||||
return eq(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 等于 =
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children eq(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children ne(SFunction<R,?> column, Object val) {
|
||||
return ne(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不等于 <>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children ne(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children gt(SFunction<R,?> column, Object val) {
|
||||
return gt(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于 >
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children gt(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children ge(SFunction<R,?> column, Object val) {
|
||||
return ge(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于等于 >=
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children ge(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children lt(SFunction<R,?> column, Object val) {
|
||||
return lt(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于 <
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children lt(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children le(SFunction<R,?> column, Object val) {
|
||||
return le(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于等于 <=
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children le(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children between(SFunction<R,?> column, Object val1, Object val2) {
|
||||
return between(true, column, val1, val2);
|
||||
}
|
||||
|
||||
/**
|
||||
* BETWEEN 值1 AND 值2
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val1 值1
|
||||
* @param val2 值2
|
||||
* @return children
|
||||
*/
|
||||
<R> Children between(boolean condition, SFunction<R,?> column, Object val1, Object val2);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children notBetween(SFunction<R,?> column, Object val1, Object val2) {
|
||||
return notBetween(true, column, val1, val2);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOT BETWEEN 值1 AND 值2
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val1 值1
|
||||
* @param val2 值2
|
||||
* @return children
|
||||
*/
|
||||
<R> Children notBetween(boolean condition, SFunction<R,?> column, Object val1, Object val2);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children like(SFunction<R,?> column, Object val) {
|
||||
return like(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* LIKE '%值%'
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children like(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children notLike(SFunction<R,?> column, Object val) {
|
||||
return notLike(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOT LIKE '%值%'
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children notLike(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children likeLeft(SFunction<R,?> column, Object val) {
|
||||
return likeLeft(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* LIKE '%值'
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children likeLeft(boolean condition, SFunction<R,?> column, Object val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children likeRight(SFunction<R,?> column, Object val) {
|
||||
return likeRight(true, column, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* LIKE '值%'
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val 值
|
||||
* @return children
|
||||
*/
|
||||
<R> Children likeRight(boolean condition, SFunction<R,?> column, Object val);
|
||||
}
|
||||
|
@ -1,6 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import com.github.mybatisplus.func.MySFunction;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
@ -11,203 +26,289 @@ import java.util.function.Consumer;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
/**
|
||||
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Func}
|
||||
* 查询条件封装
|
||||
*
|
||||
* @author hubin miemie HCL
|
||||
* @since 2017-05-26
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public interface MyFunc<Children> extends Serializable {
|
||||
|
||||
default <E, F> Children isNull(MySFunction<E, F> column) {
|
||||
return isNull(true, null, column);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children isNull(SFunction<R,?> column) {
|
||||
return isNull(true, column);
|
||||
}
|
||||
|
||||
default <E, F> Children isNull(boolean condition, MySFunction<E, F> column) {
|
||||
return isNull(condition, null, column);
|
||||
}
|
||||
/**
|
||||
* 字段 IS NULL
|
||||
* <p>例: isNull("name")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @return children
|
||||
*/
|
||||
<R> Children isNull(boolean condition, SFunction<R,?> column);
|
||||
|
||||
default <E, F> Children isNull(String alias, MySFunction<E, F> column) {
|
||||
return isNull(true, alias, column);
|
||||
}
|
||||
|
||||
<E, F> Children isNull(boolean condition, String alias, MySFunction<E, F> column);
|
||||
|
||||
|
||||
default <E, F> Children isNotNull(MySFunction<E, F> column) {
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children isNotNull(SFunction<R,?> column) {
|
||||
return isNotNull(true, column);
|
||||
}
|
||||
|
||||
default <E, F> Children isNotNull(boolean condition, MySFunction<E, F> column) {
|
||||
return isNotNull(condition, null, column);
|
||||
/**
|
||||
* 字段 IS NOT NULL
|
||||
* <p>例: isNotNull("name")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @return children
|
||||
*/
|
||||
<R> Children isNotNull(boolean condition, SFunction<R,?> column);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children in(SFunction<R,?> column, Collection<?> coll) {
|
||||
return in(true, column, coll);
|
||||
}
|
||||
|
||||
default <E, F> Children isNotNull(String alias, MySFunction<E, F> column) {
|
||||
return isNotNull(true, alias, column);
|
||||
/**
|
||||
* 字段 IN (value.get(0), value.get(1), ...)
|
||||
* <p>例: in("id", Arrays.asList(1, 2, 3, 4, 5))</p>
|
||||
*
|
||||
* <li> 如果集合为 empty 则不会进行 sql 拼接 </li>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param coll 数据集合
|
||||
* @return children
|
||||
*/
|
||||
<R> Children in(boolean condition, SFunction<R,?> column, Collection<?> coll);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children in(SFunction<R,?> column, Object... values) {
|
||||
return in(true, column, values);
|
||||
}
|
||||
|
||||
<E, F> Children isNotNull(boolean condition, String alias, MySFunction<E, F> column);
|
||||
|
||||
default <E, F> Children in(MySFunction<E, F> column, Collection<?> coll) {
|
||||
return in(true, null, column, coll);
|
||||
}
|
||||
|
||||
default <E, F> Children in(boolean condition, MySFunction<E, F> column, Collection<?> coll) {
|
||||
return in(condition, null, column, coll);
|
||||
}
|
||||
|
||||
default <E, F> Children in(String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||
return in(true, alias, column, coll);
|
||||
}
|
||||
|
||||
<E, F> Children in(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll);
|
||||
|
||||
default <E, F> Children in(MySFunction<E, F> column, Object... values) {
|
||||
return in(true, null, column, values);
|
||||
}
|
||||
|
||||
default <E, F> Children in(boolean condition, MySFunction<E, F> column, Object... values) {
|
||||
return in(condition, null, column, values);
|
||||
}
|
||||
|
||||
default <E, F> Children in(String alias, MySFunction<E, F> column, Object... values) {
|
||||
return in(true, alias, column, values);
|
||||
}
|
||||
|
||||
default <E, F> Children in(boolean condition, String alias, MySFunction<E, F> column, Object... values) {
|
||||
return in(condition, alias, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
|
||||
/**
|
||||
* 字段 IN (v0, v1, ...)
|
||||
* <p>例: in("id", 1, 2, 3, 4, 5)</p>
|
||||
*
|
||||
* <li> 如果动态数组为 empty 则不会进行 sql 拼接 </li>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param values 数据数组
|
||||
* @return children
|
||||
*/
|
||||
default <R> Children in(boolean condition, SFunction<R,?> column, Object... values) {
|
||||
return in(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
|
||||
.collect(toList()));
|
||||
}
|
||||
|
||||
default <E, F> Children notIn(MySFunction<E, F> column, Collection<?> coll) {
|
||||
return notIn(true, null, column, coll);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children notIn(SFunction<R,?> column, Collection<?> coll) {
|
||||
return notIn(true, column, coll);
|
||||
}
|
||||
|
||||
default <E, F> Children notIn(boolean condition, MySFunction<E, F> column, Collection<?> coll) {
|
||||
return notIn(condition, null, column, coll);
|
||||
/**
|
||||
* 字段 NOT IN (value.get(0), value.get(1), ...)
|
||||
* <p>例: notIn("id", Arrays.asList(1, 2, 3, 4, 5))</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param coll 数据集合
|
||||
* @return children
|
||||
*/
|
||||
<R> Children notIn(boolean condition, SFunction<R,?> column, Collection<?> coll);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children notIn(SFunction<R,?> column, Object... value) {
|
||||
return notIn(true, column, value);
|
||||
}
|
||||
|
||||
default <E, F> Children notIn(String alias, MySFunction<E, F> column, Collection<?> coll) {
|
||||
return notIn(true, alias, column, coll);
|
||||
}
|
||||
|
||||
<E, F> Children notIn(boolean condition, String alias, MySFunction<E, F> column, Collection<?> coll);
|
||||
|
||||
default <E, F> Children notIn(MySFunction<E, F> column, Object... value) {
|
||||
return notIn(true, null, column, value);
|
||||
}
|
||||
|
||||
default <E, F> Children notIn(boolean condition, MySFunction<E, F> column, Object... value) {
|
||||
return notIn(condition, null, column, value);
|
||||
}
|
||||
|
||||
default <E, F> Children notIn(String alias, MySFunction<E, F> column, Object... value) {
|
||||
return notIn(true, alias, column, value);
|
||||
}
|
||||
|
||||
default <E, F> Children notIn(boolean condition, String alias, MySFunction<E, F> column, Object... values) {
|
||||
return notIn(condition, alias, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
|
||||
/**
|
||||
* 字段 NOT IN (v0, v1, ...)
|
||||
* <p>例: notIn("id", 1, 2, 3, 4, 5)</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param values 数据数组
|
||||
* @return children
|
||||
*/
|
||||
default <R> Children notIn(boolean condition, SFunction<R,?> column, Object... values) {
|
||||
return notIn(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
|
||||
.collect(toList()));
|
||||
}
|
||||
|
||||
default <E, F> Children inSql(MySFunction<E, F> column, String inValue) {
|
||||
return inSql(true, null, column, inValue);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children inSql(SFunction<R,?> column, String inValue) {
|
||||
return inSql(true, column, inValue);
|
||||
}
|
||||
|
||||
default <E, F> Children inSql(boolean condition, MySFunction<E, F> column, String inValue) {
|
||||
return inSql(condition, null, column, inValue);
|
||||
/**
|
||||
* 字段 IN ( sql语句 )
|
||||
* <p>!! sql 注入方式的 in 方法 !!</p>
|
||||
* <p>例1: inSql("id", "1, 2, 3, 4, 5, 6")</p>
|
||||
* <p>例2: inSql("id", "select id from table where id < 3")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param inValue sql语句
|
||||
* @return children
|
||||
*/
|
||||
<R> Children inSql(boolean condition, SFunction<R,?> column, String inValue);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children notInSql(SFunction<R,?> column, String inValue) {
|
||||
return notInSql(true, column, inValue);
|
||||
}
|
||||
|
||||
default <E, F> Children inSql(String alias, MySFunction<E, F> column, String inValue) {
|
||||
return inSql(true, alias, column, inValue);
|
||||
/**
|
||||
* 字段 NOT IN ( sql语句 )
|
||||
* <p>!! sql 注入方式的 not in 方法 !!</p>
|
||||
* <p>例1: notInSql("id", "1, 2, 3, 4, 5, 6")</p>
|
||||
* <p>例2: notInSql("id", "select id from table where id < 3")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param inValue sql语句 ---> 1,2,3,4,5,6 或者 select id from table where id < 3
|
||||
* @return children
|
||||
*/
|
||||
<R> Children notInSql(boolean condition, SFunction<R,?> column, String inValue);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children groupBy(SFunction<R,?> column) {
|
||||
return groupBy(true, column);
|
||||
}
|
||||
|
||||
<E, F> Children inSql(boolean condition, String alias, MySFunction<E, F> column, String inValue);
|
||||
|
||||
default <E, F> Children notInSql(MySFunction<E, F> column, String inValue) {
|
||||
return notInSql(true, null, column, inValue);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children groupBy(SFunction<R,?>... columns) {
|
||||
return groupBy(true, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children notInSql(boolean condition, MySFunction<E, F> column, String inValue) {
|
||||
return notInSql(condition, null, column, inValue);
|
||||
/**
|
||||
* 分组:GROUP BY 字段, ...
|
||||
* <p>例: groupBy("id", "name")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param columns 字段数组
|
||||
* @return children
|
||||
*/
|
||||
<R> Children groupBy(boolean condition, SFunction<R,?>... columns);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children orderByAsc(SFunction<R,?> column) {
|
||||
return orderByAsc(true, column);
|
||||
}
|
||||
|
||||
default <E, F> Children notInSql(String alias, MySFunction<E, F> column, String inValue) {
|
||||
return notInSql(true, alias, column, inValue);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children orderByAsc(SFunction<R,?>... columns) {
|
||||
return orderByAsc(true, columns);
|
||||
}
|
||||
|
||||
<E, F> Children notInSql(boolean condition, String alias, MySFunction<E, F> column, String inValue);
|
||||
|
||||
default <E, F> Children groupBy(MySFunction<E, F>... columns) {
|
||||
return groupBy(true, null, columns);
|
||||
/**
|
||||
* 排序:ORDER BY 字段, ... ASC
|
||||
* <p>例: orderByAsc("id", "name")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param columns 字段数组
|
||||
* @return children
|
||||
*/
|
||||
default <R> Children orderByAsc(boolean condition, SFunction<R,?>... columns) {
|
||||
return orderBy(condition, true, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children groupBy(boolean condition, MySFunction<E, F>... columns) {
|
||||
return groupBy(condition, null, columns);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children orderByDesc(SFunction<R,?> column) {
|
||||
return orderByDesc(true, column);
|
||||
}
|
||||
|
||||
default <E, F> Children groupBy(String alias, MySFunction<E, F>... columns) {
|
||||
return groupBy(true, alias, columns);
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R> Children orderByDesc(SFunction<R,?>... columns) {
|
||||
return orderByDesc(true, columns);
|
||||
}
|
||||
|
||||
<E, F> Children groupBy(boolean condition, String alias, MySFunction<E, F>... columns);
|
||||
|
||||
default <E, F> Children orderByAsc(MySFunction<E, F>... columns) {
|
||||
return orderByAsc(true, null, columns);
|
||||
/**
|
||||
* 排序:ORDER BY 字段, ... DESC
|
||||
* <p>例: orderByDesc("id", "name")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param columns 字段数组
|
||||
* @return children
|
||||
*/
|
||||
default <R> Children orderByDesc(boolean condition, SFunction<R,?>... columns) {
|
||||
return orderBy(condition, false, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByAsc(boolean condition, MySFunction<E, F>... columns) {
|
||||
return orderByAsc(condition, null, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByAsc(String alias, MySFunction<E, F>... columns) {
|
||||
return orderByAsc(true, alias, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByAsc(boolean condition, String alias, MySFunction<E, F>... columns) {
|
||||
return orderBy(condition, alias, true, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByDesc(MySFunction<E, F>... columns) {
|
||||
return orderByDesc(true, null, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByDesc(boolean condition, MySFunction<E, F>... columns) {
|
||||
return orderByDesc(condition, null, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByDesc(String alias, MySFunction<E, F>... columns) {
|
||||
return orderByDesc(true, alias, columns);
|
||||
}
|
||||
|
||||
default <E, F> Children orderByDesc(boolean condition, String alias, MySFunction<E, F>... columns) {
|
||||
return orderBy(condition, alias, false, columns);
|
||||
}
|
||||
|
||||
<E, F> Children orderBy(boolean condition, String alias, boolean isAsc, MySFunction<E, F>... columns);
|
||||
/**
|
||||
* 排序:ORDER BY 字段, ...
|
||||
* <p>例: orderBy(true, "id", "name")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param isAsc 是否是 ASC 排序
|
||||
* @param columns 字段数组
|
||||
* @return children
|
||||
*/
|
||||
<R> Children orderBy(boolean condition, boolean isAsc, SFunction<R,?>... columns);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children having(String sqlHaving, Object... params) {
|
||||
return having(true, null, sqlHaving, params);
|
||||
return having(true, sqlHaving, params);
|
||||
}
|
||||
|
||||
default Children having(boolean condition, String sqlHaving, Object... params) {
|
||||
return having(condition, null, sqlHaving, params);
|
||||
}
|
||||
|
||||
default Children having(String alias, String sqlHaving, Object... params) {
|
||||
return having(true, alias, sqlHaving, params);
|
||||
}
|
||||
|
||||
Children having(boolean condition, String alias, String sqlHaving, Object... params);
|
||||
/**
|
||||
* HAVING ( sql语句 )
|
||||
* <p>例1: having("sum(age) > 10")</p>
|
||||
* <p>例2: having("sum(age) > {0}", 10)</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param sqlHaving sql 语句
|
||||
* @param params 参数数组
|
||||
* @return children
|
||||
*/
|
||||
Children having(boolean condition, String sqlHaving, Object... params);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children func(Consumer<Children> consumer) {
|
||||
return func(true, null, consumer);
|
||||
return func(true, consumer);
|
||||
}
|
||||
|
||||
default Children func(boolean condition, Consumer<Children> consumer) {
|
||||
return func(condition, null, consumer);
|
||||
}
|
||||
|
||||
default Children func(String alias, Consumer<Children> consumer) {
|
||||
return func(true, alias, consumer);
|
||||
}
|
||||
|
||||
Children func(boolean condition, String alias, Consumer<Children> consumer);
|
||||
/**
|
||||
* 消费函数
|
||||
*
|
||||
* @param consumer 消费函数
|
||||
* @return children
|
||||
* @since 3.3.1
|
||||
*/
|
||||
Children func(boolean condition, Consumer<Children> consumer);
|
||||
}
|
||||
|
@ -1,111 +0,0 @@
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import com.github.mybatisplus.func.MySFunction;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
import com.github.mybatisplus.wrapper.MyJoinLambdaQueryWrapper;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author yulichang
|
||||
*/
|
||||
public interface MyJoin<Children, T> {
|
||||
|
||||
|
||||
default <R, TE, RE> Children leftJoin(boolean condition,
|
||||
String alias,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return join(condition, alias, Constant.LEFT_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
|
||||
default <R, TE, RE> Children leftJoin(MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return leftJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
default <R, TE, RE> Children leftJoin(String alias,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return leftJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
default <R, TE, RE> Children leftJoin(boolean condition,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return leftJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
|
||||
default <R, TE, RE> Children rightJoin(boolean condition,
|
||||
String alias,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return join(condition, alias, Constant.RIGHT_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
|
||||
default <R, TE, RE> Children rightJoin(MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return rightJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
default <R, TE, RE> Children rightJoin(String alias,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return rightJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
default <R, TE, RE> Children rightJoin(boolean condition,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return rightJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
|
||||
default <R, TE, RE> Children innerJoin(boolean condition,
|
||||
String alias,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return join(condition, alias, Constant.INNER_JOIN, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
|
||||
default <R, TE, RE> Children innerJoin(MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return innerJoin(true, null, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
default <R, TE, RE> Children innerJoin(String alias,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return innerJoin(true, alias, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
default <R, TE, RE> Children innerJoin(boolean condition,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper) {
|
||||
return innerJoin(condition, null, leftCondition, rightCondition, rightWrapper);
|
||||
}
|
||||
|
||||
|
||||
<R, TE, RE> Children join(boolean condition,
|
||||
String alias,
|
||||
String keyWord,
|
||||
MySFunction<T, TE> leftCondition,
|
||||
MySFunction<R, RE> rightCondition,
|
||||
Function<MyJoinLambdaQueryWrapper<R>, MyJoinLambdaQueryWrapper<R>> rightWrapper);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.mybatisplus.toolkit.Constant;
|
||||
|
||||
public interface MyLambdaJoin<Children> {
|
||||
|
||||
default <T, X> Children leftJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return leftJoin(true, clazz, left, right);
|
||||
}
|
||||
|
||||
default <T, X> Children leftJoin(boolean condition, Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.LEFT_JOIN, condition, clazz, left, right);
|
||||
}
|
||||
|
||||
default <T, X> Children rightJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return rightJoin(true, clazz, left, right);
|
||||
}
|
||||
|
||||
default <T, X> Children rightJoin(boolean condition, Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.RIGHT_JOIN, condition, clazz, left, right);
|
||||
}
|
||||
|
||||
default <T, X> Children innerJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return innerJoin(true, clazz, left, right);
|
||||
}
|
||||
|
||||
default <T, X> Children innerJoin(boolean condition, Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.INNER_JOIN, condition, clazz, left, right);
|
||||
}
|
||||
|
||||
<T, X> Children join(String keyWord, boolean condition, Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right);
|
||||
|
||||
|
||||
}
|
@ -1,130 +0,0 @@
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Join}
|
||||
*/
|
||||
public interface MyMPJoin<Children> extends Serializable {
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children or() {
|
||||
return or(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接 OR
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @return children
|
||||
*/
|
||||
Children or(boolean condition);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children apply(String applySql, Object... value) {
|
||||
return apply(true, applySql, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接 sql
|
||||
* <p>!! 会有 sql 注入风险 !!</p>
|
||||
* <p>例1: apply("id = 1")</p>
|
||||
* <p>例2: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")</p>
|
||||
* <p>例3: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", LocalDate.now())</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @return children
|
||||
*/
|
||||
Children apply(boolean condition, String applySql, Object... value);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children last(String lastSql) {
|
||||
return last(true, lastSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 无视优化规则直接拼接到 sql 的最后(有sql注入的风险,请谨慎使用)
|
||||
* <p>例: last("limit 1")</p>
|
||||
* <p>注意只能调用一次,多次调用以最后一次为准</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param lastSql sql语句
|
||||
* @return children
|
||||
*/
|
||||
Children last(boolean condition, String lastSql);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children comment(String comment) {
|
||||
return comment(true, comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* sql 注释(会拼接在 sql 的最后面)
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param comment sql注释
|
||||
* @return children
|
||||
*/
|
||||
Children comment(boolean condition, String comment);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children first(String firstSql) {
|
||||
return first(true, firstSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* sql 起始句(会拼接在SQL语句的起始处)
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param firstSql 起始语句
|
||||
* @return children
|
||||
* @since 3.3.1
|
||||
*/
|
||||
Children first(boolean condition, String firstSql);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children exists(String existsSql) {
|
||||
return exists(true, existsSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接 EXISTS ( sql语句 )
|
||||
* <p>!! sql 注入方法 !!</p>
|
||||
* <p>例: exists("select id from table where age = 1")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param existsSql sql语句
|
||||
* @return children
|
||||
*/
|
||||
Children exists(boolean condition, String existsSql);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children notExists(String existsSql) {
|
||||
return notExists(true, existsSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接 NOT EXISTS ( sql语句 )
|
||||
* <p>!! sql 注入方法 !!</p>
|
||||
* <p>例: notExists("select id from table where age = 1")</p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param existsSql sql语句
|
||||
* @return children
|
||||
*/
|
||||
Children notExists(boolean condition, String existsSql);
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Nested}
|
||||
*/
|
||||
public interface MyNested<Param, Children> extends Serializable {
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children and(Consumer<Param> consumer) {
|
||||
return and(true, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* AND 嵌套
|
||||
* <p>
|
||||
* 例: and(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||
* </p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param consumer 消费函数
|
||||
* @return children
|
||||
*/
|
||||
Children and(boolean condition, Consumer<Param> consumer);
|
||||
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children or(Consumer<Param> consumer) {
|
||||
return or(true, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* OR 嵌套
|
||||
* <p>
|
||||
* 例: or(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||
* </p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param consumer 消费函数
|
||||
* @return children
|
||||
*/
|
||||
Children or(boolean condition, Consumer<Param> consumer);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children nested(Consumer<Param> consumer) {
|
||||
return nested(true, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 正常嵌套 不带 AND 或者 OR
|
||||
* <p>
|
||||
* 例: nested(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||
* </p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param consumer 消费函数
|
||||
* @return children
|
||||
*/
|
||||
Children nested(boolean condition, Consumer<Param> consumer);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default Children not(Consumer<Param> consumer) {
|
||||
return not(true, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* not嵌套
|
||||
* <p>
|
||||
* 例: not(i -> i.eq("name", "李白").ne("status", "活着"))
|
||||
* </p>
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param consumer 消费函数
|
||||
* @return children
|
||||
*/
|
||||
Children not(boolean condition, Consumer<Param> consumer);
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.github.mybatisplus.wrapper.interfaces;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* copy {@link com.baomidou.mybatisplus.core.conditions.query.Query}
|
||||
*/
|
||||
public interface MySFunctionQuery<Children> extends Serializable {
|
||||
|
||||
/**
|
||||
* 设置查询字段
|
||||
*
|
||||
* @param columns 字段数组
|
||||
* @return children
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
<E> Children select(SFunction<E, ?>... columns);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
* <p>注意只有内部有 entity 才能使用该方法</p>
|
||||
*/
|
||||
default Children select(Predicate<TableFieldInfo> predicate) {
|
||||
return select(null, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤查询的字段信息(主键除外!)
|
||||
* <p>例1: 只要 java 字段名以 "test" 开头的 -> select(i -> i.getProperty().startsWith("test"))</p>
|
||||
* <p>例2: 只要 java 字段属性是 CharSequence 类型的 -> select(TableFieldInfo::isCharSequence)</p>
|
||||
* <p>例3: 只要 java 字段没有填充策略的 -> select(i -> i.getFieldFill() == FieldFill.DEFAULT)</p>
|
||||
* <p>例4: 要全部字段 -> select(i -> true)</p>
|
||||
* <p>例5: 只要主键字段 -> select(i -> false)</p>
|
||||
*
|
||||
* @param predicate 过滤方式
|
||||
* @return children
|
||||
*/
|
||||
<E>Children select(Class<E> entityClass, Predicate<TableFieldInfo> predicate);
|
||||
|
||||
/**
|
||||
* 查询条件 SQL 片段
|
||||
*/
|
||||
String getSqlSelect();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user