mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
移除自定义别名
This commit is contained in:
parent
3910a2de79
commit
b55cb534c6
@ -1,66 +0,0 @@
|
|||||||
package com.baomidou.mybatisplus.core.metadata;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
||||||
import com.github.yulichang.annotation.MPJTableAlias;
|
|
||||||
import com.github.yulichang.toolkit.Constant;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 全局表别名控制,默认表别名为 t t1 t2 ... <br>
|
|
||||||
* 可以通过@MPJTableAlias注解指定表别名<br/>
|
|
||||||
* 仅对MPJLambdaWrapper有效
|
|
||||||
*
|
|
||||||
* @author yulichang
|
|
||||||
* @see com.github.yulichang.wrapper.MPJLambdaWrapper
|
|
||||||
* @see MPJTableAlias
|
|
||||||
* @since 1.2.0
|
|
||||||
*/
|
|
||||||
public class MPJTableAliasHelper {
|
|
||||||
|
|
||||||
private static final Map<Class<?>, TableAlias> CACHE = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用于生成别名的序号
|
|
||||||
*/
|
|
||||||
private static final AtomicInteger index = new AtomicInteger(1);
|
|
||||||
|
|
||||||
public static void init(Class<?> clazz) {
|
|
||||||
TableAlias as = CACHE.get(clazz);
|
|
||||||
if (Objects.nonNull(as)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
TableAlias alias = new TableAlias();
|
|
||||||
MPJTableAlias tableAlias = clazz.getAnnotation(MPJTableAlias.class);
|
|
||||||
if (tableAlias != null && StringUtils.isNotBlank(tableAlias.value())) {
|
|
||||||
alias.setAlias(tableAlias.value());
|
|
||||||
} else {
|
|
||||||
alias.setAlias(Constant.TABLE_ALIAS + index.getAndIncrement());
|
|
||||||
}
|
|
||||||
alias.setAliasDOT(alias.getAlias() + StringPool.DOT);
|
|
||||||
CACHE.put(clazz, alias);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TableAlias get(Class<?> clazz) {
|
|
||||||
return CACHE.get(clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class TableAlias {
|
|
||||||
/**
|
|
||||||
* 表别名
|
|
||||||
*/
|
|
||||||
private String alias;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 前缀
|
|
||||||
*/
|
|
||||||
private String aliasDOT;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ package com.baomidou.mybatisplus.core.metadata;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实体类对应的mapper管理
|
* 实体类对应的mapper管理
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
package com.github.yulichang.annotation;
|
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关联查询时的表别名<br/>
|
|
||||||
* 框架默认会随机生成 一般是 t1 t2 t3 ...<br/>
|
|
||||||
* 不要在程序中使用随机别名,运行期间是不变的,但是重启就不一定了<br/>
|
|
||||||
* 如需使用请配合此注解一起使用<br/>
|
|
||||||
* <p>
|
|
||||||
* 这个注解定义的表别名或者随机生成的别名只对MPJLambdaWrapper生效<br/>
|
|
||||||
* 对MPJQueryWrapper不生效,
|
|
||||||
*
|
|
||||||
* @author yulichang
|
|
||||||
* @see com.github.yulichang.wrapper.MPJLambdaWrapper
|
|
||||||
* @see com.github.yulichang.query.MPJQueryWrapper
|
|
||||||
* @since 1.2.0
|
|
||||||
*/
|
|
||||||
@Documented
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
|
|
||||||
public @interface MPJTableAlias {
|
|
||||||
|
|
||||||
String value();
|
|
||||||
}
|
|
@ -1,7 +1,9 @@
|
|||||||
package com.github.yulichang.config;
|
package com.github.yulichang.config;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
import com.github.yulichang.exception.MPJException;
|
import com.github.yulichang.exception.MPJException;
|
||||||
|
import com.github.yulichang.injector.MPJSqlInjector;
|
||||||
import com.github.yulichang.interceptor.MPJInterceptor;
|
import com.github.yulichang.interceptor.MPJInterceptor;
|
||||||
import org.apache.ibatis.logging.Log;
|
import org.apache.ibatis.logging.Log;
|
||||||
import org.apache.ibatis.logging.LogFactory;
|
import org.apache.ibatis.logging.LogFactory;
|
||||||
@ -31,6 +33,8 @@ public class InterceptorConfig implements ApplicationListener<ApplicationReadyEv
|
|||||||
private List<SqlSessionFactory> sqlSessionFactoryList;
|
private List<SqlSessionFactory> sqlSessionFactoryList;
|
||||||
@Autowired
|
@Autowired
|
||||||
private MPJInterceptor mpjInterceptor;
|
private MPJInterceptor mpjInterceptor;
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ISqlInjector iSqlInjector;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -44,16 +48,21 @@ public class InterceptorConfig implements ApplicationListener<ApplicationReadyEv
|
|||||||
Field interceptors = InterceptorChain.class.getDeclaredField("interceptors");
|
Field interceptors = InterceptorChain.class.getDeclaredField("interceptors");
|
||||||
interceptors.setAccessible(true);
|
interceptors.setAccessible(true);
|
||||||
List<Interceptor> list = (List<Interceptor>) interceptors.get(chain);
|
List<Interceptor> list = (List<Interceptor>) interceptors.get(chain);
|
||||||
if (CollectionUtils.isNotEmpty(list) && list.get(list.size() - 1) != mpjInterceptor) {
|
if (CollectionUtils.isNotEmpty(list)) {
|
||||||
list.removeIf(i -> i == mpjInterceptor);
|
if (list.get(list.size() - 1) != mpjInterceptor) {
|
||||||
|
list.removeIf(i -> i == mpjInterceptor);
|
||||||
|
list.add(mpjInterceptor);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
list.add(mpjInterceptor);
|
list.add(mpjInterceptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
throw new MPJException("mpjInterceptor exception");
|
throw new MPJException("mpjInterceptor exception");
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
logger.warn("MPJ not define SqlSessionFactory");
|
if (iSqlInjector != null && !(iSqlInjector instanceof MPJSqlInjector)) {
|
||||||
|
logger.error("sql注入器未继承 MPJSqlInjector -> " + iSqlInjector.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package com.github.yulichang.injector;
|
package com.github.yulichang.injector;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||||
|
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector;
|
||||||
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
|
||||||
|
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||||
import com.baomidou.mybatisplus.core.injector.methods.*;
|
import com.baomidou.mybatisplus.core.injector.methods.*;
|
||||||
import com.baomidou.mybatisplus.core.mapper.Mapper;
|
import com.baomidou.mybatisplus.core.mapper.Mapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
|
import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
|
||||||
@ -24,9 +28,10 @@ import static java.util.stream.Collectors.toList;
|
|||||||
* @author yulichang
|
* @author yulichang
|
||||||
* @see DefaultSqlInjector
|
* @see DefaultSqlInjector
|
||||||
*/
|
*/
|
||||||
@ConditionalOnMissingBean(DefaultSqlInjector.class)
|
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
|
||||||
public class MPJSqlInjector extends DefaultSqlInjector {
|
public class MPJSqlInjector extends DefaultSqlInjector {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容
|
* 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容
|
||||||
*/
|
*/
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package com.github.yulichang.method;
|
package com.github.yulichang.method;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||||
import com.baomidou.mybatisplus.core.metadata.MPJTableAliasHelper;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||||
|
import com.github.yulichang.toolkit.Constant;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -40,7 +40,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
|
|||||||
String[] columns = selectColumns.split(StringPool.COMMA);
|
String[] columns = selectColumns.split(StringPool.COMMA);
|
||||||
List<String> selectColumnList = new ArrayList<>();
|
List<String> selectColumnList = new ArrayList<>();
|
||||||
for (String c : columns) {
|
for (String c : columns) {
|
||||||
selectColumnList.add(MPJTableAliasHelper.get(table.getEntityType()).getAlias() + StringPool.DOT + c);
|
selectColumnList.add(Constant.TABLE_ALIAS + StringPool.DOT + c);
|
||||||
}
|
}
|
||||||
selectColumns = String.join(StringPool.COMMA, selectColumnList);
|
selectColumns = String.join(StringPool.COMMA, selectColumnList);
|
||||||
}
|
}
|
||||||
@ -54,18 +54,16 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
|
|||||||
@Override
|
@Override
|
||||||
protected String sqlCount() {
|
protected String sqlCount() {
|
||||||
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null and %s != ''", WRAPPER,
|
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null and %s != ''", WRAPPER,
|
||||||
Q_WRAPPER_SQL_SELECT, Q_WRAPPER_SQL_SELECT),
|
Q_WRAPPER_SQL_SELECT, Q_WRAPPER_SQL_SELECT),
|
||||||
SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), ASTERISK);
|
SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), ASTERISK);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String sqlAlias(Class<?> modelClass) {
|
protected String sqlAlias() {
|
||||||
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != ''", "ew.autoAlias", "ew.autoAlias"),
|
return SqlScriptUtils.convertIf("${ew.alias}", String.format("%s != null and %s != ''", "ew.alias", "ew.alias"), false);
|
||||||
MPJTableAliasHelper.get(modelClass).getAlias(), "${ew.alias}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String sqlFrom() {
|
protected String sqlFrom() {
|
||||||
return SqlScriptUtils.convertIf("${ew.from}", String.format("%s != null and %s != ''", "ew.from", "ew.from"), false);
|
return SqlScriptUtils.convertIf("${ew.from}", String.format("%s != null and %s != ''", "ew.from", "ew.from"), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class SelectJoinCount extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public class SelectJoinList extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class SelectJoinMap extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class SelectJoinMaps extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class SelectJoinMapsPage extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public class SelectJoinOne extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public class SelectJoinPage extends MPJAbstractMethod {
|
|||||||
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
||||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
|
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
|
||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
|
||||||
tableInfo.getTableName(), sqlAlias(modelClass), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
/**
|
/**
|
||||||
* 主表别名
|
* 主表别名
|
||||||
*/
|
*/
|
||||||
private String alias = Constant.TABLE_ALIAS;
|
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询的列
|
* 查询的列
|
||||||
@ -90,14 +90,6 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
this.ignoreColumns = ignoreColumns;
|
this.ignoreColumns = ignoreColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置表别名(默认为 t 可以调用此方法修改,调用后才生效,所以最好第一个调用)
|
|
||||||
*/
|
|
||||||
public final MPJLambdaQueryWrapper<T> alias(String tableAlias) {
|
|
||||||
this.alias = tableAlias;
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SELECT 部分 SQL 设置
|
* SELECT 部分 SQL 设置
|
||||||
*
|
*
|
||||||
@ -138,7 +130,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
public final MPJLambdaQueryWrapper<T> selectIgnore(SFunction<T, ?>... columns) {
|
public final MPJLambdaQueryWrapper<T> selectIgnore(SFunction<T, ?>... columns) {
|
||||||
if (ArrayUtils.isNotEmpty(columns)) {
|
if (ArrayUtils.isNotEmpty(columns)) {
|
||||||
for (SFunction<T, ?> s : columns) {
|
for (SFunction<T, ?> s : columns) {
|
||||||
ignoreColumns.add(this.alias + StringPool.DOT + super.columnToString(s, false));
|
ignoreColumns.add(Constant.TABLE_ALIAS + StringPool.DOT + getColumnCache(s).getColumn());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
@ -146,7 +138,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
|
protected String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
|
||||||
return this.alias + StringPool.DOT + super.columnToString(column, onlyColumn);
|
return Constant.TABLE_ALIAS + StringPool.DOT + super.columnToString(column, onlyColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MPJLambdaQueryWrapper<T> select(String... columns) {
|
public MPJLambdaQueryWrapper<T> select(String... columns) {
|
||||||
@ -174,7 +166,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||||
Assert.notNull(info, "can not find table info");
|
Assert.notNull(info, "can not find table info");
|
||||||
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
|
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
|
||||||
this.alias + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
|
Constant.TABLE_ALIAS + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +177,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
* @param clazz 主表class
|
* @param clazz 主表class
|
||||||
*/
|
*/
|
||||||
public final MPJLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
public final MPJLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||||
return selectAll(clazz, this.alias);
|
return selectAll(clazz, Constant.TABLE_ALIAS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -194,14 +186,13 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
* @param clazz 表实体
|
* @param clazz 表实体
|
||||||
* @param as 表别名
|
* @param as 表别名
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("DuplicatedCode")
|
|
||||||
public final MPJLambdaQueryWrapper<T> selectAll(Class<?> clazz, String as) {
|
public final MPJLambdaQueryWrapper<T> selectAll(Class<?> clazz, String as) {
|
||||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||||
Assert.notNull(info, "can not find table info");
|
Assert.notNull(info, "can not find table info");
|
||||||
if (info.havePK()) {
|
if (info.havePK()) {
|
||||||
selectColumns.add(as + StringPool.DOT + info.getKeyColumn());
|
selectColumns.add(as + StringPool.DOT + info.getKeyColumn());
|
||||||
}
|
}
|
||||||
selectColumns.addAll(info.getFieldList().stream().filter(TableFieldInfo::isSelect).map(i ->
|
selectColumns.addAll(info.getFieldList().stream().map(i ->
|
||||||
as + StringPool.DOT + i.getColumn()).collect(Collectors.toList()));
|
as + StringPool.DOT + i.getColumn()).collect(Collectors.toList()));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -229,12 +220,9 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
|||||||
return from.getStringValue();
|
return from.getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getAutoAlias() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return alias;
|
return alias.getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,7 +47,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
|||||||
/**
|
/**
|
||||||
* 主表别名
|
* 主表别名
|
||||||
*/
|
*/
|
||||||
private String alias = Constant.TABLE_ALIAS;
|
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询的列
|
* 查询的列
|
||||||
@ -88,14 +88,6 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
|||||||
this.ignoreColumns = ignoreColumns;
|
this.ignoreColumns = ignoreColumns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置表别名(默认为 t 可以调用此方法修改,调用后才生效,所以最好第一个调用)
|
|
||||||
*/
|
|
||||||
public final MPJQueryWrapper<T> alias(String tableAlias) {
|
|
||||||
this.alias = tableAlias;
|
|
||||||
return typedThis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MPJQueryWrapper<T> select(String... columns) {
|
public MPJQueryWrapper<T> select(String... columns) {
|
||||||
if (ArrayUtils.isNotEmpty(columns)) {
|
if (ArrayUtils.isNotEmpty(columns)) {
|
||||||
@ -130,7 +122,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
|||||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||||
Assert.notNull(info, "can not find table info");
|
Assert.notNull(info, "can not find table info");
|
||||||
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
|
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
|
||||||
this.alias + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
|
Constant.TABLE_ALIAS + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,7 +133,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
|||||||
* @param clazz 主表class
|
* @param clazz 主表class
|
||||||
*/
|
*/
|
||||||
public final MPJQueryWrapper<T> selectAll(Class<T> clazz) {
|
public final MPJQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||||
selectAll(clazz, this.alias);
|
selectAll(clazz, Constant.TABLE_ALIAS);
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,7 +149,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
|||||||
if (info.havePK()) {
|
if (info.havePK()) {
|
||||||
selectColumns.add(as + StringPool.DOT + info.getKeyColumn());
|
selectColumns.add(as + StringPool.DOT + info.getKeyColumn());
|
||||||
}
|
}
|
||||||
selectColumns.addAll(info.getFieldList().stream().filter(TableFieldInfo::isSelect).map(i ->
|
selectColumns.addAll(info.getFieldList().stream().map(i ->
|
||||||
as + StringPool.DOT + i.getColumn()).collect(Collectors.toList()));
|
as + StringPool.DOT + i.getColumn()).collect(Collectors.toList()));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -178,18 +170,13 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
|||||||
return from.getStringValue();
|
return from.getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getAutoAlias() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAlias() {
|
public String getAlias() {
|
||||||
return alias;
|
return alias.getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回一个支持 lambda 函数写法的 wrapper
|
* 返回一个支持 lambda 函数写法的 wrapper
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public MPJLambdaQueryWrapper<T> lambda() {
|
public MPJLambdaQueryWrapper<T> lambda() {
|
||||||
return new MPJLambdaQueryWrapper<>(getEntity(), getEntityClass(), from, sqlSelect, paramNameSeq, paramNameValuePairs,
|
return new MPJLambdaQueryWrapper<>(getEntity(), getEntityClass(), from, sqlSelect, paramNameSeq, paramNameValuePairs,
|
||||||
expression, lastSql, sqlComment, sqlFirst, selectColumns, ignoreColumns);
|
expression, lastSql, sqlComment, sqlFirst, selectColumns, ignoreColumns);
|
||||||
|
@ -11,6 +11,8 @@ public interface Constant {
|
|||||||
*/
|
*/
|
||||||
String TABLE_ALIAS = "t";
|
String TABLE_ALIAS = "t";
|
||||||
|
|
||||||
|
String AS = " AS ";
|
||||||
|
|
||||||
String ON = " ON ";
|
String ON = " ON ";
|
||||||
|
|
||||||
String JOIN = "JOIN";
|
String JOIN = "JOIN";
|
||||||
@ -37,4 +39,9 @@ public interface Constant {
|
|||||||
* " INNER JOIN "
|
* " INNER JOIN "
|
||||||
*/
|
*/
|
||||||
String INNER_JOIN = StringPool.SPACE + INNER + StringPool.SPACE + JOIN + StringPool.SPACE;
|
String INNER_JOIN = StringPool.SPACE + INNER + StringPool.SPACE + JOIN + StringPool.SPACE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* " t"
|
||||||
|
*/
|
||||||
|
String SPACE_TABLE_ALIAS = StringPool.SPACE + Constant.TABLE_ALIAS;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package com.github.yulichang.wrapper;
|
package com.github.yulichang.wrapper;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.MPJTableAliasHelper;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
|
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.github.yulichang.toolkit.Constant;
|
||||||
import com.github.yulichang.toolkit.LambdaUtils;
|
import com.github.yulichang.toolkit.LambdaUtils;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import static java.util.stream.Collectors.joining;
|
import static java.util.stream.Collectors.joining;
|
||||||
|
|
||||||
@ -19,6 +20,12 @@ import static java.util.stream.Collectors.joining;
|
|||||||
*/
|
*/
|
||||||
public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLambdaWrapper<T, Children>>
|
public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLambdaWrapper<T, Children>>
|
||||||
extends MPJAbstractWrapper<T, Children> {
|
extends MPJAbstractWrapper<T, Children> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联的表
|
||||||
|
*/
|
||||||
|
protected Map<Class<?>, Integer> subTable = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存字段
|
* 缓存字段
|
||||||
*/
|
*/
|
||||||
@ -26,13 +33,7 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected <X> String columnToString(X column) {
|
protected <X> String columnToString(X column) {
|
||||||
return columnToString((SFunction<?, ?>) column, hasAlias || entityClass !=
|
return columnToString((SFunction<?, ?>) column);
|
||||||
LambdaUtils.getEntityClass((SFunction<?, ?>) column));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected <X> String columnToString(X column, boolean hasAlias) {
|
|
||||||
return columnToString((SFunction<?, ?>) column, hasAlias);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -41,9 +42,9 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
|||||||
return Arrays.stream(columns).map(i -> columnToString((SFunction<?, ?>) i)).collect(joining(StringPool.COMMA));
|
return Arrays.stream(columns).map(i -> columnToString((SFunction<?, ?>) i)).collect(joining(StringPool.COMMA));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String columnToString(SFunction<?, ?> column, boolean hasAlias) {
|
protected String columnToString(SFunction<?, ?> column) {
|
||||||
return (hasAlias ? MPJTableAliasHelper.get(LambdaUtils.getEntityClass(column)).getAliasDOT() :
|
return Constant.TABLE_ALIAS + getDefault(subTable.get(LambdaUtils.getEntityClass(column))) + StringPool.DOT +
|
||||||
StringPool.EMPTY) + getCache(column).getColumn();
|
getCache(column).getColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ColumnCache getCache(SFunction<?, ?> fn) {
|
protected ColumnCache getCache(SFunction<?, ?> fn) {
|
||||||
@ -55,4 +56,12 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
|||||||
}
|
}
|
||||||
return cacheMap.get(LambdaUtils.formatKey(LambdaUtils.getName(fn)));
|
return cacheMap.get(LambdaUtils.formatKey(LambdaUtils.getName(fn)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getDefault(Integer i) {
|
||||||
|
if (Objects.nonNull(i)) {
|
||||||
|
return i.toString();
|
||||||
|
}
|
||||||
|
return StringPool.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import com.baomidou.mybatisplus.core.toolkit.*;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.toolkit.LambdaUtils;
|
|
||||||
import com.github.yulichang.toolkit.sql.SqlScriptUtils;
|
import com.github.yulichang.toolkit.sql.SqlScriptUtils;
|
||||||
import com.github.yulichang.wrapper.interfaces.Compare;
|
import com.github.yulichang.wrapper.interfaces.Compare;
|
||||||
import com.github.yulichang.wrapper.interfaces.Func;
|
import com.github.yulichang.wrapper.interfaces.Func;
|
||||||
@ -36,17 +35,10 @@ import static java.util.stream.Collectors.joining;
|
|||||||
*
|
*
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
*/
|
*/
|
||||||
//@SuppressWarnings("ALL")
|
@SuppressWarnings("ALL")
|
||||||
@SuppressWarnings({"unchecked", "unused"})
|
|
||||||
public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<T, Children>> extends Wrapper<T>
|
public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<T, Children>> extends Wrapper<T>
|
||||||
implements Compare<Children>, Nested<Children, Children>, Join<Children>, Func<Children>, OnCompare<Children> {
|
implements Compare<Children>, Nested<Children, Children>, Join<Children>, Func<Children>, OnCompare<Children> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否使用别名
|
|
||||||
*/
|
|
||||||
protected boolean hasAlias;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 占位符
|
* 占位符
|
||||||
*/
|
*/
|
||||||
@ -59,7 +51,7 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
/**
|
/**
|
||||||
* 其他
|
* 其他
|
||||||
*/
|
*/
|
||||||
/* mybatis plus 3.4.3新增 这个是wrapper的别名 不是MPJ的别名 */
|
/* mybatis plus 3.4.3新增 这个时wrapper的别名 不是MPJ的别名 */
|
||||||
protected SharedString paramAlias;
|
protected SharedString paramAlias;
|
||||||
protected SharedString lastSql;
|
protected SharedString lastSql;
|
||||||
/**
|
/**
|
||||||
@ -79,13 +71,32 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
/**
|
/**
|
||||||
* 实体类型(主要用于确定泛型以及取TableInfo缓存)
|
* 实体类型(主要用于确定泛型以及取TableInfo缓存)
|
||||||
*/
|
*/
|
||||||
protected Class<?> entityClass;
|
private Class<T> entityClass;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getEntity() {
|
public T getEntity() {
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Children setEntity(T entity) {
|
||||||
|
this.entity = entity;
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<T> getEntityClass() {
|
||||||
|
if (entityClass == null && entity != null) {
|
||||||
|
entityClass = (Class<T>) entity.getClass();
|
||||||
|
}
|
||||||
|
return entityClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Children setEntityClass(Class<T> entityClass) {
|
||||||
|
if (entityClass != null) {
|
||||||
|
this.entityClass = entityClass;
|
||||||
|
}
|
||||||
|
return typedThis;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <X, V> Children allEq(boolean condition, Map<SFunction<X, ?>, V> params, boolean null2IsNull) {
|
public <X, V> Children allEq(boolean condition, Map<SFunction<X, ?>, V> params, boolean null2IsNull) {
|
||||||
if (condition && CollectionUtils.isNotEmpty(params)) {
|
if (condition && CollectionUtils.isNotEmpty(params)) {
|
||||||
@ -318,6 +329,7 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
* 字段 SQL 注入过滤处理,子类重写实现过滤逻辑
|
* 字段 SQL 注入过滤处理,子类重写实现过滤逻辑
|
||||||
*
|
*
|
||||||
* @param column 字段内容
|
* @param column 字段内容
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
protected <X> SFunction<X, ?> columnSqlInjectFilter(SFunction<X, ?> column) {
|
protected <X> SFunction<X, ?> columnSqlInjectFilter(SFunction<X, ?> column) {
|
||||||
return column;
|
return column;
|
||||||
@ -372,11 +384,9 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
() -> formatParam(null, val)));
|
() -> formatParam(null, val)));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <X, S> Children addCondition(boolean condition, SFunction<X, ?> column, SqlKeyword sqlKeyword,
|
protected <X, S> Children addCondition(boolean condition, SFunction<X, ?> column, SqlKeyword sqlKeyword, SFunction<S, ?> val) {
|
||||||
SFunction<S, ?> val) {
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), sqlKeyword,
|
||||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, hasAlias || entityClass !=
|
columnToSqlSegment(val)));
|
||||||
LambdaUtils.getEntityClass(column)), sqlKeyword, columnToSqlSegment(val, hasAlias ||
|
|
||||||
entityClass != LambdaUtils.getEntityClass(val))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -407,7 +417,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
* @param params 参数
|
* @param params 参数
|
||||||
* @return sql片段
|
* @return sql片段
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("SameParameterValue")
|
|
||||||
protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
|
protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
|
||||||
if (StringUtils.isBlank(sqlStr)) {
|
if (StringUtils.isBlank(sqlStr)) {
|
||||||
// todo 何时会这样?
|
// todo 何时会这样?
|
||||||
@ -563,10 +572,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
return () -> columnToString(column);
|
return () -> columnToString(column);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final <X> ISqlSegment columnToSqlSegment(SFunction<X, ?> column, boolean hasAlias) {
|
|
||||||
return () -> columnToString(column, hasAlias);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 columnName
|
* 获取 columnName
|
||||||
*/
|
*/
|
||||||
@ -574,10 +579,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
return (String) column;
|
return (String) column;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <X> String columnToString(X column, boolean hasAlias) {
|
|
||||||
return (String) column;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多字段转换为逗号 "," 分割字符串
|
* 多字段转换为逗号 "," 分割字符串
|
||||||
*
|
*
|
||||||
@ -633,4 +634,36 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
|||||||
public <R, S> Children le(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val) {
|
public <R, S> Children le(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val) {
|
||||||
return addCondition(condition, column, LE, val);
|
return addCondition(condition, column, LE, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R, S, U> Children between(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<U, ?> val2) {
|
||||||
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN,
|
||||||
|
columnToSqlSegment(val1), AND, columnToSqlSegment(val2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <R, S> Children between(boolean condition, SFunction<R, ?> column, Object val1, SFunction<S, ?> val2) {
|
||||||
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN,
|
||||||
|
() -> formatParam(null, val1), AND, columnToSqlSegment(val2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <R, S> Children between(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, Object val2) {
|
||||||
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN,
|
||||||
|
columnToSqlSegment(val1), AND, () -> formatParam(null, val2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R, S, U> Children notBetween(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<U, ?> val2) {
|
||||||
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN,
|
||||||
|
columnToSqlSegment(val1), AND, columnToSqlSegment(val2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <R, U> Children notBetween(boolean condition, SFunction<R, ?> column, Object val1, SFunction<U, ?> val2) {
|
||||||
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN,
|
||||||
|
() -> formatParam(null, val1), AND, columnToSqlSegment(val2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <R, S> Children notBetween(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, Object val2) {
|
||||||
|
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN,
|
||||||
|
columnToSqlSegment(val1), AND, () -> formatParam(null, val2)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@ package com.github.yulichang.wrapper;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||||
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
||||||
import com.baomidou.mybatisplus.core.metadata.*;
|
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.*;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.toolkit.Constant;
|
import com.github.yulichang.toolkit.Constant;
|
||||||
@ -11,10 +13,13 @@ import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
|||||||
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
||||||
import com.github.yulichang.wrapper.interfaces.Query;
|
import com.github.yulichang.wrapper.interfaces.Query;
|
||||||
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -28,40 +33,61 @@ import java.util.stream.Collectors;
|
|||||||
* @author yulichang
|
* @author yulichang
|
||||||
* @see com.github.yulichang.toolkit.Wrappers
|
* @see com.github.yulichang.toolkit.Wrappers
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("all")
|
||||||
public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWrapper<T>>
|
public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWrapper<T>>
|
||||||
implements Query<MPJLambdaWrapper<T>>, LambdaJoin<MPJLambdaWrapper<T>> {
|
implements Query<MPJLambdaWrapper<T>>, LambdaJoin<MPJLambdaWrapper<T>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询字段 sql
|
* 查询字段 sql
|
||||||
*/
|
*/
|
||||||
private final SharedString sqlSelect = new SharedString();
|
private SharedString sqlSelect = new SharedString();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询表
|
* 查询表
|
||||||
*/
|
*/
|
||||||
private final SharedString from = new SharedString();
|
private final SharedString from = new SharedString();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 主表别名
|
||||||
|
*/
|
||||||
|
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询的字段
|
* 查询的字段
|
||||||
*/
|
*/
|
||||||
private final List<String> selectColumns = new ArrayList<>();
|
private final List<SelectColumn> selectColumns = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 忽略查询的字段
|
* 忽略查询的字段
|
||||||
*/
|
*/
|
||||||
private final List<String> ignoreColumns = new ArrayList<>();
|
private final List<SelectColumn> ignoreColumns = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表序号
|
||||||
|
*/
|
||||||
|
private int tableIndex = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ON sql wrapper集合
|
* ON sql wrapper集合
|
||||||
*/
|
*/
|
||||||
private final List<String> joinSql = new ArrayList<>();
|
private final List<MPJLambdaWrapper<?>> onWrappers = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表关键字 on 条件 func 使用
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
private String keyWord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表实体类 on 条件 func 使用
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
private Class<?> joinClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery()
|
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery()
|
||||||
*/
|
*/
|
||||||
public MPJLambdaWrapper() {
|
public MPJLambdaWrapper() {
|
||||||
this.hasAlias = true;
|
|
||||||
super.initNeed();
|
super.initNeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,27 +95,22 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
/**
|
/**
|
||||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(...)
|
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(...)
|
||||||
*/
|
*/
|
||||||
MPJLambdaWrapper(Class<?> entityClass, AtomicInteger paramNameSeq,
|
MPJLambdaWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||||
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, boolean hasAlias) {
|
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
|
||||||
this.entityClass = entityClass;
|
Map<Class<?>, Integer> subTable, String keyWord, Class<?> joinClass) {
|
||||||
|
super.setEntity(entity);
|
||||||
|
super.setEntityClass(entityClass);
|
||||||
this.paramNameSeq = paramNameSeq;
|
this.paramNameSeq = paramNameSeq;
|
||||||
this.paramNameValuePairs = paramNameValuePairs;
|
this.paramNameValuePairs = paramNameValuePairs;
|
||||||
this.expression = mergeSegments;
|
this.expression = mergeSegments;
|
||||||
|
this.sqlSelect = sqlSelect;
|
||||||
this.lastSql = lastSql;
|
this.lastSql = lastSql;
|
||||||
this.sqlComment = sqlComment;
|
this.sqlComment = sqlComment;
|
||||||
this.sqlFirst = sqlFirst;
|
this.sqlFirst = sqlFirst;
|
||||||
this.hasAlias = hasAlias;
|
this.subTable = subTable;
|
||||||
}
|
this.keyWord = keyWord;
|
||||||
|
this.joinClass = joinClass;
|
||||||
@Override
|
|
||||||
protected MPJLambdaWrapper<T> instance() {
|
|
||||||
return instance(true, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected MPJLambdaWrapper<T> instance(boolean hasAlias, Class<?> entityClass) {
|
|
||||||
return new MPJLambdaWrapper<>(entityClass, paramNameSeq, paramNameValuePairs, new MergeSegments(),
|
|
||||||
SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), hasAlias);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -97,7 +118,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
public final <S> MPJLambdaWrapper<T> select(SFunction<S, ?>... columns) {
|
public final <S> MPJLambdaWrapper<T> select(SFunction<S, ?>... columns) {
|
||||||
if (ArrayUtils.isNotEmpty(columns)) {
|
if (ArrayUtils.isNotEmpty(columns)) {
|
||||||
for (SFunction<S, ?> s : columns) {
|
for (SFunction<S, ?> s : columns) {
|
||||||
selectColumns.add(getThisAlias(s) + getCache(s).getColumn());
|
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), getCache(s).getColumn()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
@ -107,23 +128,20 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
public <E> MPJLambdaWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
|
public <E> MPJLambdaWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||||
Assert.notNull(info, "table can not be find");
|
Assert.notNull(info, "table can not be find");
|
||||||
MPJTableAliasHelper.TableAlias alias = MPJTableAliasHelper.get(entityClass);
|
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
|
||||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(i ->
|
i -> selectColumns.add(SelectColumn.of(entityClass, i.getColumn())));
|
||||||
selectColumns.add((hasAlias ? alias.getAliasDOT() : StringPool.EMPTY) + i.getColumn()));
|
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
|
public <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
|
||||||
selectColumns.add(getThisAlias(column) + getCache(column).getColumn() + Constants.AS + alias);
|
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column,
|
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
|
||||||
String alias) {
|
|
||||||
if (condition) {
|
if (condition) {
|
||||||
selectColumns.add(String.format(funcEnum.getSql(), getThisAlias(column) + getCache(column).getColumn())
|
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias, funcEnum));
|
||||||
+ Constants.AS + alias);
|
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -131,7 +149,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
@Override
|
@Override
|
||||||
public MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
|
public MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
|
||||||
if (condition) {
|
if (condition) {
|
||||||
selectColumns.add(String.format(funcEnum.getSql(), column.toString()) + Constants.AS + alias);
|
selectColumns.add(SelectColumn.of(null, column.toString(), alias, funcEnum));
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
@ -139,12 +157,11 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
public final MPJLambdaWrapper<T> selectAll(Class<?> clazz) {
|
public final MPJLambdaWrapper<T> selectAll(Class<?> clazz) {
|
||||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||||
Assert.notNull(info, "table can not be find -> %s", clazz);
|
Assert.notNull(info, "table can not be find -> %s", clazz);
|
||||||
String dot = hasAlias ? MPJTableAliasHelper.get(clazz).getAliasDOT() : StringPool.EMPTY;
|
|
||||||
if (info.havePK()) {
|
if (info.havePK()) {
|
||||||
selectColumns.add(dot + info.getKeyColumn());
|
selectColumns.add(SelectColumn.of(clazz, info.getKeyColumn()));
|
||||||
}
|
}
|
||||||
info.getFieldList().stream().filter(TableFieldInfo::isSelect).forEach(c ->
|
info.getFieldList().forEach(c ->
|
||||||
selectColumns.add(dot + c.getColumn()));
|
selectColumns.add(SelectColumn.of(clazz, c.getColumn())));
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,21 +170,12 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
public final <S> MPJLambdaWrapper<T> selectIgnore(SFunction<S, ?>... columns) {
|
public final <S> MPJLambdaWrapper<T> selectIgnore(SFunction<S, ?>... columns) {
|
||||||
if (ArrayUtils.isNotEmpty(columns)) {
|
if (ArrayUtils.isNotEmpty(columns)) {
|
||||||
for (SFunction<S, ?> s : columns) {
|
for (SFunction<S, ?> s : columns) {
|
||||||
ignoreColumns.add(getThisAlias(s) + getCache(s).getColumn());
|
ignoreColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), getCache(s).getColumn()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MPJLambdaWrapper<T> selectQuery(Class<?> clazz, OnFunction function, String alias) {
|
|
||||||
MPJLambdaWrapper<?> apply = function.apply(instance(false, clazz));
|
|
||||||
selectColumns.add(String.format("(SELECT %s FROM %s %s)", apply.getSqlSelect(),
|
|
||||||
TableInfoHelper.getTableInfo(clazz).getTableName(), apply.getCustomSqlSegment()) + Constants.AS + alias);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询条件 SQL 片段
|
* 查询条件 SQL 片段
|
||||||
*/
|
*/
|
||||||
@ -175,9 +183,15 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
public String getSqlSelect() {
|
public String getSqlSelect() {
|
||||||
if (StringUtils.isBlank(sqlSelect.getStringValue())) {
|
if (StringUtils.isBlank(sqlSelect.getStringValue())) {
|
||||||
if (CollectionUtils.isNotEmpty(ignoreColumns)) {
|
if (CollectionUtils.isNotEmpty(ignoreColumns)) {
|
||||||
selectColumns.removeIf(ignoreColumns::contains);
|
selectColumns.removeIf(c -> c.getFuncEnum() == null && ignoreColumns.stream().anyMatch(i ->
|
||||||
|
i.getClazz() == c.getClazz() && Objects.equals(c.getColumnName(), i.getColumnName())));
|
||||||
}
|
}
|
||||||
sqlSelect.setStringValue(String.join(StringPool.COMMA, selectColumns));
|
String s = selectColumns.stream().map(i -> {
|
||||||
|
String str = Constant.TABLE_ALIAS + getDefault(subTable.get(i.getClazz())) + StringPool.DOT + i.getColumnName();
|
||||||
|
return (i.getFuncEnum() == null ? str : String.format(i.getFuncEnum().getSql(), str)) +
|
||||||
|
(StringUtils.isBlank(i.getAlias()) ? StringPool.EMPTY : (Constant.AS + i.getAlias()));
|
||||||
|
}).collect(Collectors.joining(StringPool.COMMA));
|
||||||
|
sqlSelect.setStringValue(s);
|
||||||
}
|
}
|
||||||
return sqlSelect.getStringValue();
|
return sqlSelect.getStringValue();
|
||||||
}
|
}
|
||||||
@ -187,15 +201,39 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
*/
|
*/
|
||||||
public String getFrom() {
|
public String getFrom() {
|
||||||
if (StringUtils.isBlank(from.getStringValue())) {
|
if (StringUtils.isBlank(from.getStringValue())) {
|
||||||
from.setStringValue(String.join(StringPool.SPACE, joinSql));
|
StringBuilder value = new StringBuilder();
|
||||||
|
for (MPJLambdaWrapper<?> wrapper : onWrappers) {
|
||||||
|
String tableName = TableInfoHelper.getTableInfo(wrapper.getJoinClass()).getTableName();
|
||||||
|
value.append(wrapper.getKeyWord())
|
||||||
|
.append(tableName)
|
||||||
|
.append(Constant.SPACE_TABLE_ALIAS)
|
||||||
|
.append(subTable.get(wrapper.getJoinClass()))
|
||||||
|
.append(Constant.ON)
|
||||||
|
.append(wrapper.getExpression().getNormal().getSqlSegment());
|
||||||
|
}
|
||||||
|
from.setStringValue(value.toString());
|
||||||
}
|
}
|
||||||
return from.getStringValue();
|
return from.getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getAutoAlias() {
|
public String getAlias() {
|
||||||
return true;
|
return alias.getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于生成嵌套 sql
|
||||||
|
* <p>故 sqlSelect 不向下传递</p>
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected MPJLambdaWrapper<T> instance() {
|
||||||
|
return instance(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected MPJLambdaWrapper<T> instance(String keyWord, Class<?> joinClass) {
|
||||||
|
return new MPJLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||||
|
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
|
||||||
|
this.subTable, keyWord, joinClass);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
@ -204,21 +242,68 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
|||||||
from.toNull();
|
from.toNull();
|
||||||
selectColumns.clear();
|
selectColumns.clear();
|
||||||
ignoreColumns.clear();
|
ignoreColumns.clear();
|
||||||
joinSql.clear();
|
subTable.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <R> MPJLambdaWrapper<T> join(String keyWord, boolean condition, Class<R> clazz, OnFunction function) {
|
public <R> MPJLambdaWrapper<T> join(String keyWord, boolean condition, Class<R> clazz, OnFunction function) {
|
||||||
if (condition) {
|
if (condition) {
|
||||||
joinSql.add(keyWord + TableInfoHelper.getTableInfo(clazz).getTableName() +
|
MPJLambdaWrapper<?> apply = function.apply(instance(keyWord, clazz));
|
||||||
Constants.SPACE + MPJTableAliasHelper.get(clazz).getAlias() +
|
onWrappers.add(apply);
|
||||||
Constant.ON + function.apply(instance()).getExpression().getNormal().getSqlSegment());
|
subTable.put(clazz, tableIndex);
|
||||||
|
tableIndex++;
|
||||||
}
|
}
|
||||||
return typedThis;
|
return typedThis;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getThisAlias(SFunction<?, ?> function) {
|
/**
|
||||||
return hasAlias ? MPJTableAliasHelper.get(LambdaUtils.getEntityClass(function)).getAliasDOT() :
|
* select字段
|
||||||
StringPool.EMPTY;
|
*/
|
||||||
|
@Data
|
||||||
|
public static class SelectColumn {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段实体类
|
||||||
|
*/
|
||||||
|
private Class<?> clazz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库字段名
|
||||||
|
*/
|
||||||
|
private String columnName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段别名
|
||||||
|
*/
|
||||||
|
private String alias;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字段函数
|
||||||
|
*/
|
||||||
|
private BaseFuncEnum funcEnum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义函数填充参数
|
||||||
|
*/
|
||||||
|
private List<SFunction<?, ?>> funcArgs;
|
||||||
|
|
||||||
|
private SelectColumn(Class<?> clazz, String columnName, String alias, BaseFuncEnum funcEnum) {
|
||||||
|
this.clazz = clazz;
|
||||||
|
this.columnName = columnName;
|
||||||
|
this.alias = alias;
|
||||||
|
this.funcEnum = funcEnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SelectColumn of(Class<?> clazz, String columnName) {
|
||||||
|
return new SelectColumn(clazz, columnName, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SelectColumn of(Class<?> clazz, String columnName, String alias) {
|
||||||
|
return new SelectColumn(clazz, columnName, alias, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SelectColumn of(Class<?> clazz, String columnName, String alias, BaseFuncEnum funcEnum) {
|
||||||
|
return new SelectColumn(clazz, columnName, alias, funcEnum);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|||||||
import com.github.yulichang.toolkit.LambdaUtils;
|
import com.github.yulichang.toolkit.LambdaUtils;
|
||||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||||
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
|
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
|
||||||
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@ -52,22 +51,6 @@ public interface Query<Children> extends Serializable {
|
|||||||
*/
|
*/
|
||||||
<S> Children selectAs(SFunction<S, ?> column, String alias);
|
<S> Children selectAs(SFunction<S, ?> column, String alias);
|
||||||
|
|
||||||
/**
|
|
||||||
* 子查询
|
|
||||||
*
|
|
||||||
* @param clazz 查询的类
|
|
||||||
* @param function 查询lambda
|
|
||||||
* @param alias 别名
|
|
||||||
*/
|
|
||||||
Children selectQuery(Class<?> clazz, OnFunction function, String alias);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ignore
|
|
||||||
*/
|
|
||||||
default <S> Children selectQuery(Class<?> clazz, OnFunction function, SFunction<S, ?> alias) {
|
|
||||||
return selectQuery(clazz, function, LambdaUtils.getName(alias));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 聚合函数查询
|
* 聚合函数查询
|
||||||
*
|
*
|
||||||
|
@ -115,4 +115,40 @@ public interface OnCompare<Children> extends Serializable {
|
|||||||
* @return children
|
* @return children
|
||||||
*/
|
*/
|
||||||
<R, S> Children le(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val);
|
<R, S> Children le(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default <R, S, T> Children between(SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2) {
|
||||||
|
return between(true, column, val1, val2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BETWEEN 值1 AND 值2
|
||||||
|
*
|
||||||
|
* @param condition 执行条件
|
||||||
|
* @param column 字段
|
||||||
|
* @param val1 值1
|
||||||
|
* @param val2 值2
|
||||||
|
* @return children
|
||||||
|
*/
|
||||||
|
<R, S, T> Children between(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore
|
||||||
|
*/
|
||||||
|
default <R, S, T> Children notBetween(SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> 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, S, T> Children notBetween(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user