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.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 实体类对应的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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.github.yulichang.exception.MPJException;
|
||||
import com.github.yulichang.injector.MPJSqlInjector;
|
||||
import com.github.yulichang.interceptor.MPJInterceptor;
|
||||
import org.apache.ibatis.logging.Log;
|
||||
import org.apache.ibatis.logging.LogFactory;
|
||||
@ -31,6 +33,8 @@ public class InterceptorConfig implements ApplicationListener<ApplicationReadyEv
|
||||
private List<SqlSessionFactory> sqlSessionFactoryList;
|
||||
@Autowired
|
||||
private MPJInterceptor mpjInterceptor;
|
||||
@Autowired(required = false)
|
||||
private ISqlInjector iSqlInjector;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -44,16 +48,21 @@ public class InterceptorConfig implements ApplicationListener<ApplicationReadyEv
|
||||
Field interceptors = InterceptorChain.class.getDeclaredField("interceptors");
|
||||
interceptors.setAccessible(true);
|
||||
List<Interceptor> list = (List<Interceptor>) interceptors.get(chain);
|
||||
if (CollectionUtils.isNotEmpty(list) && list.get(list.size() - 1) != mpjInterceptor) {
|
||||
list.removeIf(i -> i == mpjInterceptor);
|
||||
if (CollectionUtils.isNotEmpty(list)) {
|
||||
if (list.get(list.size() - 1) != mpjInterceptor) {
|
||||
list.removeIf(i -> i == mpjInterceptor);
|
||||
list.add(mpjInterceptor);
|
||||
}
|
||||
} else {
|
||||
list.add(mpjInterceptor);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
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;
|
||||
|
||||
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.AbstractSqlInjector;
|
||||
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.mapper.Mapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
|
||||
@ -24,9 +28,10 @@ import static java.util.stream.Collectors.toList;
|
||||
* @author yulichang
|
||||
* @see DefaultSqlInjector
|
||||
*/
|
||||
@ConditionalOnMissingBean(DefaultSqlInjector.class)
|
||||
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
|
||||
public class MPJSqlInjector extends DefaultSqlInjector {
|
||||
|
||||
|
||||
/**
|
||||
* 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容
|
||||
*/
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.github.yulichang.method;
|
||||
|
||||
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.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -40,7 +40,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
|
||||
String[] columns = selectColumns.split(StringPool.COMMA);
|
||||
List<String> selectColumnList = new ArrayList<>();
|
||||
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);
|
||||
}
|
||||
@ -54,18 +54,16 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
|
||||
@Override
|
||||
protected String sqlCount() {
|
||||
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);
|
||||
}
|
||||
|
||||
protected String sqlAlias(Class<?> modelClass) {
|
||||
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != ''", "ew.autoAlias", "ew.autoAlias"),
|
||||
MPJTableAliasHelper.get(modelClass).getAlias(), "${ew.alias}");
|
||||
protected String sqlAlias() {
|
||||
return SqlScriptUtils.convertIf("${ew.alias}", String.format("%s != null and %s != ''", "ew.alias", "ew.alias"), false);
|
||||
}
|
||||
|
||||
protected String sqlFrom() {
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT;
|
||||
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);
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
|
||||
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);
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
|
||||
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);
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
|
||||
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);
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
|
||||
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);
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
|
||||
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);
|
||||
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) {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置表别名(默认为 t 可以调用此方法修改,调用后才生效,所以最好第一个调用)
|
||||
*/
|
||||
public final MPJLambdaQueryWrapper<T> alias(String tableAlias) {
|
||||
this.alias = tableAlias;
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* SELECT 部分 SQL 设置
|
||||
*
|
||||
@ -138,7 +130,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
public final MPJLambdaQueryWrapper<T> selectIgnore(SFunction<T, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(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;
|
||||
@ -146,7 +138,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
|
||||
@Override
|
||||
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) {
|
||||
@ -174,7 +166,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(info, "can not find table info");
|
||||
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;
|
||||
}
|
||||
|
||||
@ -185,7 +177,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
* @param clazz 主表class
|
||||
*/
|
||||
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 as 表别名
|
||||
*/
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public final MPJLambdaQueryWrapper<T> selectAll(Class<?> clazz, String as) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(info, "can not find table info");
|
||||
if (info.havePK()) {
|
||||
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()));
|
||||
return typedThis;
|
||||
}
|
||||
@ -229,12 +220,9 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
return from.getStringValue();
|
||||
}
|
||||
|
||||
public boolean getAutoAlias() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
return alias.getStringValue();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -263,4 +251,4 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置表别名(默认为 t 可以调用此方法修改,调用后才生效,所以最好第一个调用)
|
||||
*/
|
||||
public final MPJQueryWrapper<T> alias(String tableAlias) {
|
||||
this.alias = tableAlias;
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MPJQueryWrapper<T> select(String... columns) {
|
||||
if (ArrayUtils.isNotEmpty(columns)) {
|
||||
@ -130,7 +122,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(info, "can not find table info");
|
||||
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;
|
||||
}
|
||||
|
||||
@ -141,7 +133,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
* @param clazz 主表class
|
||||
*/
|
||||
public final MPJQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||
selectAll(clazz, this.alias);
|
||||
selectAll(clazz, Constant.TABLE_ALIAS);
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@ -157,7 +149,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
if (info.havePK()) {
|
||||
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()));
|
||||
return typedThis;
|
||||
}
|
||||
@ -178,18 +170,13 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
return from.getStringValue();
|
||||
}
|
||||
|
||||
public boolean getAutoAlias() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
return alias.getStringValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个支持 lambda 函数写法的 wrapper
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public MPJLambdaQueryWrapper<T> lambda() {
|
||||
return new MPJLambdaQueryWrapper<>(getEntity(), getEntityClass(), from, sqlSelect, paramNameSeq, paramNameValuePairs,
|
||||
expression, lastSql, sqlComment, sqlFirst, selectColumns, ignoreColumns);
|
||||
|
@ -11,6 +11,8 @@ public interface Constant {
|
||||
*/
|
||||
String TABLE_ALIAS = "t";
|
||||
|
||||
String AS = " AS ";
|
||||
|
||||
String ON = " ON ";
|
||||
|
||||
String JOIN = "JOIN";
|
||||
@ -37,4 +39,9 @@ public interface Constant {
|
||||
* " INNER JOIN "
|
||||
*/
|
||||
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;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.MPJTableAliasHelper;
|
||||
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.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
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>>
|
||||
extends MPJAbstractWrapper<T, Children> {
|
||||
|
||||
/**
|
||||
* 关联的表
|
||||
*/
|
||||
protected Map<Class<?>, Integer> subTable = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 缓存字段
|
||||
*/
|
||||
@ -26,13 +33,7 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
|
||||
@Override
|
||||
protected <X> String columnToString(X column) {
|
||||
return columnToString((SFunction<?, ?>) column, hasAlias || entityClass !=
|
||||
LambdaUtils.getEntityClass((SFunction<?, ?>) column));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <X> String columnToString(X column, boolean hasAlias) {
|
||||
return columnToString((SFunction<?, ?>) column, hasAlias);
|
||||
return columnToString((SFunction<?, ?>) column);
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
protected String columnToString(SFunction<?, ?> column, boolean hasAlias) {
|
||||
return (hasAlias ? MPJTableAliasHelper.get(LambdaUtils.getEntityClass(column)).getAliasDOT() :
|
||||
StringPool.EMPTY) + getCache(column).getColumn();
|
||||
protected String columnToString(SFunction<?, ?> column) {
|
||||
return Constant.TABLE_ALIAS + getDefault(subTable.get(LambdaUtils.getEntityClass(column))) + StringPool.DOT +
|
||||
getCache(column).getColumn();
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
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.StringEscape;
|
||||
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.wrapper.interfaces.Compare;
|
||||
import com.github.yulichang.wrapper.interfaces.Func;
|
||||
@ -36,17 +35,10 @@ import static java.util.stream.Collectors.joining;
|
||||
*
|
||||
* @author yulichang
|
||||
*/
|
||||
//@SuppressWarnings("ALL")
|
||||
@SuppressWarnings({"unchecked", "unused"})
|
||||
@SuppressWarnings("ALL")
|
||||
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> {
|
||||
|
||||
|
||||
/**
|
||||
* 是否使用别名
|
||||
*/
|
||||
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 lastSql;
|
||||
/**
|
||||
@ -79,13 +71,32 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
/**
|
||||
* 实体类型(主要用于确定泛型以及取TableInfo缓存)
|
||||
*/
|
||||
protected Class<?> entityClass;
|
||||
private Class<T> entityClass;
|
||||
|
||||
@Override
|
||||
public T getEntity() {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Children setEntity(T entity) {
|
||||
this.entity = entity;
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
public Class<T> getEntityClass() {
|
||||
if (entityClass == null && entity != null) {
|
||||
entityClass = (Class<T>) entity.getClass();
|
||||
}
|
||||
return entityClass;
|
||||
}
|
||||
|
||||
public Children setEntityClass(Class<T> entityClass) {
|
||||
if (entityClass != null) {
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X, V> Children allEq(boolean condition, Map<SFunction<X, ?>, V> params, boolean null2IsNull) {
|
||||
if (condition && CollectionUtils.isNotEmpty(params)) {
|
||||
@ -318,6 +329,7 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
* 字段 SQL 注入过滤处理,子类重写实现过滤逻辑
|
||||
*
|
||||
* @param column 字段内容
|
||||
* @return
|
||||
*/
|
||||
protected <X> SFunction<X, ?> columnSqlInjectFilter(SFunction<X, ?> column) {
|
||||
return column;
|
||||
@ -372,11 +384,9 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
() -> formatParam(null, val)));
|
||||
}
|
||||
|
||||
protected <X, S> Children addCondition(boolean condition, SFunction<X, ?> column, SqlKeyword sqlKeyword,
|
||||
SFunction<S, ?> val) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, hasAlias || entityClass !=
|
||||
LambdaUtils.getEntityClass(column)), sqlKeyword, columnToSqlSegment(val, hasAlias ||
|
||||
entityClass != LambdaUtils.getEntityClass(val))));
|
||||
protected <X, S> Children addCondition(boolean condition, SFunction<X, ?> column, SqlKeyword sqlKeyword, SFunction<S, ?> val) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), sqlKeyword,
|
||||
columnToSqlSegment(val)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -407,7 +417,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
* @param params 参数
|
||||
* @return sql片段
|
||||
*/
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
|
||||
if (StringUtils.isBlank(sqlStr)) {
|
||||
// todo 何时会这样?
|
||||
@ -563,10 +572,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
return () -> columnToString(column);
|
||||
}
|
||||
|
||||
protected final <X> ISqlSegment columnToSqlSegment(SFunction<X, ?> column, boolean hasAlias) {
|
||||
return () -> columnToString(column, hasAlias);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 columnName
|
||||
*/
|
||||
@ -574,10 +579,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
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) {
|
||||
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.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.support.SFunction;
|
||||
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.Query;
|
||||
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
@ -28,40 +33,61 @@ import java.util.stream.Collectors;
|
||||
* @author yulichang
|
||||
* @see com.github.yulichang.toolkit.Wrappers
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@SuppressWarnings("all")
|
||||
public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWrapper<T>>
|
||||
implements Query<MPJLambdaWrapper<T>>, LambdaJoin<MPJLambdaWrapper<T>> {
|
||||
|
||||
/**
|
||||
* 查询字段 sql
|
||||
*/
|
||||
private final SharedString sqlSelect = new SharedString();
|
||||
private SharedString sqlSelect = 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集合
|
||||
*/
|
||||
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()
|
||||
*/
|
||||
public MPJLambdaWrapper() {
|
||||
this.hasAlias = true;
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
@ -69,27 +95,22 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(...)
|
||||
*/
|
||||
MPJLambdaWrapper(Class<?> entityClass, AtomicInteger paramNameSeq,
|
||||
MPJLambdaWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, boolean hasAlias) {
|
||||
this.entityClass = entityClass;
|
||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
|
||||
Map<Class<?>, Integer> subTable, String keyWord, Class<?> joinClass) {
|
||||
super.setEntity(entity);
|
||||
super.setEntityClass(entityClass);
|
||||
this.paramNameSeq = paramNameSeq;
|
||||
this.paramNameValuePairs = paramNameValuePairs;
|
||||
this.expression = mergeSegments;
|
||||
this.sqlSelect = sqlSelect;
|
||||
this.lastSql = lastSql;
|
||||
this.sqlComment = sqlComment;
|
||||
this.sqlFirst = sqlFirst;
|
||||
this.hasAlias = hasAlias;
|
||||
}
|
||||
|
||||
@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);
|
||||
this.subTable = subTable;
|
||||
this.keyWord = keyWord;
|
||||
this.joinClass = joinClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -97,7 +118,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public final <S> MPJLambdaWrapper<T> select(SFunction<S, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(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;
|
||||
@ -107,23 +128,20 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public <E> MPJLambdaWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(info, "table can not be find");
|
||||
MPJTableAliasHelper.TableAlias alias = MPJTableAliasHelper.get(entityClass);
|
||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(i ->
|
||||
selectColumns.add((hasAlias ? alias.getAliasDOT() : StringPool.EMPTY) + i.getColumn()));
|
||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
|
||||
i -> selectColumns.add(SelectColumn.of(entityClass, i.getColumn())));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column,
|
||||
String alias) {
|
||||
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
|
||||
if (condition) {
|
||||
selectColumns.add(String.format(funcEnum.getSql(), getThisAlias(column) + getCache(column).getColumn())
|
||||
+ Constants.AS + alias);
|
||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias, funcEnum));
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
@ -131,7 +149,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
@Override
|
||||
public MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
|
||||
if (condition) {
|
||||
selectColumns.add(String.format(funcEnum.getSql(), column.toString()) + Constants.AS + alias);
|
||||
selectColumns.add(SelectColumn.of(null, column.toString(), alias, funcEnum));
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
@ -139,12 +157,11 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public final MPJLambdaWrapper<T> selectAll(Class<?> clazz) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(info, "table can not be find -> %s", clazz);
|
||||
String dot = hasAlias ? MPJTableAliasHelper.get(clazz).getAliasDOT() : StringPool.EMPTY;
|
||||
if (info.havePK()) {
|
||||
selectColumns.add(dot + info.getKeyColumn());
|
||||
selectColumns.add(SelectColumn.of(clazz, info.getKeyColumn()));
|
||||
}
|
||||
info.getFieldList().stream().filter(TableFieldInfo::isSelect).forEach(c ->
|
||||
selectColumns.add(dot + c.getColumn()));
|
||||
info.getFieldList().forEach(c ->
|
||||
selectColumns.add(SelectColumn.of(clazz, c.getColumn())));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@ -153,21 +170,12 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public final <S> MPJLambdaWrapper<T> selectIgnore(SFunction<S, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(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;
|
||||
}
|
||||
|
||||
|
||||
@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 片段
|
||||
*/
|
||||
@ -175,9 +183,15 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public String getSqlSelect() {
|
||||
if (StringUtils.isBlank(sqlSelect.getStringValue())) {
|
||||
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();
|
||||
}
|
||||
@ -187,15 +201,39 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
*/
|
||||
public String getFrom() {
|
||||
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();
|
||||
}
|
||||
|
||||
public boolean getAutoAlias() {
|
||||
return true;
|
||||
public String getAlias() {
|
||||
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
|
||||
public void clear() {
|
||||
@ -204,21 +242,68 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
from.toNull();
|
||||
selectColumns.clear();
|
||||
ignoreColumns.clear();
|
||||
joinSql.clear();
|
||||
subTable.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> MPJLambdaWrapper<T> join(String keyWord, boolean condition, Class<R> clazz, OnFunction function) {
|
||||
if (condition) {
|
||||
joinSql.add(keyWord + TableInfoHelper.getTableInfo(clazz).getTableName() +
|
||||
Constants.SPACE + MPJTableAliasHelper.get(clazz).getAlias() +
|
||||
Constant.ON + function.apply(instance()).getExpression().getNormal().getSqlSegment());
|
||||
MPJLambdaWrapper<?> apply = function.apply(instance(keyWord, clazz));
|
||||
onWrappers.add(apply);
|
||||
subTable.put(clazz, tableIndex);
|
||||
tableIndex++;
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
private String getThisAlias(SFunction<?, ?> function) {
|
||||
return hasAlias ? MPJTableAliasHelper.get(LambdaUtils.getEntityClass(function)).getAliasDOT() :
|
||||
StringPool.EMPTY;
|
||||
/**
|
||||
* select字段
|
||||
*/
|
||||
@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.wrapper.enums.BaseFuncEnum;
|
||||
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Predicate;
|
||||
@ -52,22 +51,6 @@ public interface Query<Children> extends Serializable {
|
||||
*/
|
||||
<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
|
||||
*/
|
||||
<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