mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
fix MP多租户sqlParse解析异常 https://gitee.com/best_handsome/mybatis-plus-join/issues/I8CAGY
This commit is contained in:
parent
51f2231e9d
commit
fbb7f05fcb
@ -30,7 +30,7 @@ public class MPJInterceptorConfig {
|
||||
System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" +
|
||||
"| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" +
|
||||
" / | /\n" +
|
||||
" 1.4.7");
|
||||
" 1.4.8");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,9 +6,11 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.adapter.v352.AbstractMethod352;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import org.apache.ibatis.builder.SqlSourceBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* @author yulichang
|
||||
@ -82,4 +84,22 @@ public abstract class MPJAbstractMethod extends AbstractMethod352 implements MPJ
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public String removeExtraWhitespaces(String sql) {
|
||||
try {
|
||||
return SqlSourceBuilder.removeExtraWhitespaces(sql);
|
||||
} catch (Throwable t) {
|
||||
StringTokenizer tokenizer = new StringTokenizer(sql);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
boolean hasMoreTokens = tokenizer.hasMoreTokens();
|
||||
while (hasMoreTokens) {
|
||||
builder.append(tokenizer.nextToken());
|
||||
hasMoreTokens = tokenizer.hasMoreTokens();
|
||||
if (hasMoreTokens) {
|
||||
builder.append(' ');
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class SelectJoinCount extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Long.class);
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class SelectJoinList extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ public class SelectJoinMap extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class SelectJoinMaps extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ public class SelectJoinMapsPage extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ public class SelectJoinOne extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ public class SelectJoinPage extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ public class UpdateJoin extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.UPDATE_JOIN;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), mpjTableName(tableInfo), sqlAlias(), sqlFrom(),
|
||||
mpjSqlSet(true, true, tableInfo, true, ENTITY, ENTITY_DOT), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ public class UpdateJoinAndNull extends MPJAbstractMethod {
|
||||
SqlMethod sqlMethod = SqlMethod.UPDATE_JOIN_AND_NULL;
|
||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), mpjTableName(tableInfo), sqlAlias(), sqlFrom(),
|
||||
mpjSqlSet(true, true, tableInfo, true, ENTITY, ENTITY_DOT), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
|
||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
||||
return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.method.mp;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -35,8 +36,12 @@ public class SelectList extends com.baomidou.mybatisplus.core.injector.methods.S
|
||||
|
||||
@Override
|
||||
protected String sqlOrderBy(TableInfo table) {
|
||||
String orderBy = super.sqlOrderBy(table);
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
return orderBy;
|
||||
}
|
||||
return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()),
|
||||
super.sqlOrderBy(table), mpjSqlOrderBy(table));
|
||||
orderBy, mpjSqlOrderBy(table));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.method.mp;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -35,6 +36,10 @@ public class SelectMaps extends com.baomidou.mybatisplus.core.injector.methods.S
|
||||
|
||||
@Override
|
||||
protected String sqlOrderBy(TableInfo table) {
|
||||
String orderBy = super.sqlOrderBy(table);
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
return orderBy;
|
||||
}
|
||||
return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()),
|
||||
super.sqlOrderBy(table), mpjSqlOrderBy(table));
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.method.mp;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -35,6 +36,10 @@ public class SelectMapsPage extends com.baomidou.mybatisplus.core.injector.metho
|
||||
|
||||
@Override
|
||||
protected String sqlOrderBy(TableInfo table) {
|
||||
String orderBy = super.sqlOrderBy(table);
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
return orderBy;
|
||||
}
|
||||
return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()),
|
||||
super.sqlOrderBy(table), mpjSqlOrderBy(table));
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.method.mp;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -48,6 +49,16 @@ public class SelectObjs extends com.baomidou.mybatisplus.core.injector.methods.S
|
||||
selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String sqlOrderBy(TableInfo table) {
|
||||
String orderBy = super.sqlOrderBy(table);
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
return orderBy;
|
||||
}
|
||||
return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()),
|
||||
super.sqlOrderBy(table), mpjSqlOrderBy(table));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String sqlComment() {
|
||||
return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)",
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.method.mp;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -45,6 +46,16 @@ public class SelectOne extends com.baomidou.mybatisplus.core.injector.methods.Se
|
||||
selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String sqlOrderBy(TableInfo table) {
|
||||
String orderBy = super.sqlOrderBy(table);
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
return orderBy;
|
||||
}
|
||||
return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()),
|
||||
super.sqlOrderBy(table), mpjSqlOrderBy(table));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String sqlComment() {
|
||||
return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)",
|
||||
|
@ -3,6 +3,7 @@ package com.github.yulichang.method.mp;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -35,6 +36,10 @@ public class SelectPage extends com.baomidou.mybatisplus.core.injector.methods.S
|
||||
|
||||
@Override
|
||||
protected String sqlOrderBy(TableInfo table) {
|
||||
String orderBy = super.sqlOrderBy(table);
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
return orderBy;
|
||||
}
|
||||
return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()),
|
||||
super.sqlOrderBy(table), mpjSqlOrderBy(table));
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.github.yulichang.query;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisPlusVersion;
|
||||
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.SharedString;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.Query;
|
||||
@ -8,6 +9,7 @@ import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||
import com.github.yulichang.adapter.base.tookit.VersionUtils;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.query.interfaces.StringJoin;
|
||||
import com.github.yulichang.toolkit.Asserts;
|
||||
@ -89,7 +91,13 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
}
|
||||
|
||||
public MPJQueryWrapper(Class<T> clazz) {
|
||||
super.setEntityClass(clazz);
|
||||
try {
|
||||
super.setEntityClass(clazz);
|
||||
} catch (NoSuchMethodError error) {
|
||||
if (VersionUtils.compare(MybatisPlusVersion.getVersion(), "3.3.0") > 0) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
|
@ -8,11 +8,15 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.github.yulichang.injector.MPJSqlInjector;
|
||||
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||
import lombok.SneakyThrows;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import org.apache.ibatis.builder.SqlSourceBuilder;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
@ -44,6 +48,17 @@ public class MybatisPlusConfig {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
PaginationInnerInterceptor page = new PaginationInnerInterceptor(DbType.H2);
|
||||
page.setOptimizeJoin(false);
|
||||
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
return new LongValue(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
return !"user_tenant".equals(tableName);
|
||||
}
|
||||
}));
|
||||
interceptor.addInnerInterceptor(page);
|
||||
interceptor.addInnerInterceptor(new SqlInterceptor());
|
||||
return interceptor;
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.github.yulichang.test.join.entity;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@FieldNameConstants
|
||||
@TableName(value = "user_tenant")
|
||||
public class UserTenantDO {
|
||||
|
||||
@TableId
|
||||
private Integer id;
|
||||
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private Integer tenantId;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.github.yulichang.test.join.mapper;
|
||||
|
||||
import com.github.yulichang.test.join.entity.UserTenantDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserTenantMapper extends MyBaseMapper<UserTenantDO> {
|
||||
|
||||
|
||||
}
|
@ -99,4 +99,15 @@ INSERT INTO order_t (id, user_id, age, name) VALUES
|
||||
(5, 5, 4, '5,4'),
|
||||
(6, 6, 3, '6,3'),
|
||||
(7, 7, 2, '7,2'),
|
||||
(8, 8, 1, '8,1');
|
||||
(8, 8, 1, '8,1');
|
||||
|
||||
DELETE FROM user_tenant;
|
||||
INSERT INTO user_tenant (id,user_id, tenant_id) VALUES
|
||||
(1, 1, 1),
|
||||
(2, 2, 1),
|
||||
(3, 3, 1),
|
||||
(4, 4, 1),
|
||||
(5, 5, 1),
|
||||
(6, 6, 2),
|
||||
(7, 7, 2),
|
||||
(8, 8, 2);
|
@ -63,4 +63,13 @@ create table order_t
|
||||
user_id int null,
|
||||
age int null,
|
||||
name varchar(255) null
|
||||
);
|
||||
|
||||
DROP TABLE IF EXISTS user_tenant;
|
||||
create table user_tenant
|
||||
(
|
||||
id int auto_increment
|
||||
primary key,
|
||||
user_id int null,
|
||||
tenant_id int null
|
||||
);
|
@ -680,7 +680,9 @@ class LambdaWrapperTest {
|
||||
.eq(UserDO::getName, "ref");
|
||||
userMapper.selectList(wrapper);
|
||||
try {
|
||||
userMapper.insertBatchSomeColumn(new ArrayList<>());
|
||||
userMapper.insertBatchSomeColumn(new ArrayList<UserDO>() {{
|
||||
add(new UserDO());
|
||||
}});
|
||||
} catch (BadSqlGrammarException ignored) {
|
||||
}
|
||||
}
|
||||
@ -774,7 +776,7 @@ class LambdaWrapperTest {
|
||||
*/
|
||||
@Test
|
||||
void testTable() {
|
||||
ThreadLocalUtils.set("SELECT t.id FROM `user`bbbbbbb t LEFT JOIN addressaaaaaaaaaa t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) WHERE t.del=false AND t1.del=false AND t2.del=false AND (t.id <= ?) ORDER BY t.id DESC");
|
||||
ThreadLocalUtils.set("SELECT t.id FROM bbbbbbb t LEFT JOIN addressaaaaaaaaaa t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) WHERE t.del=false AND t1.del=false AND t2.del=false AND (t.id <= ?) ORDER BY t.id DESC");
|
||||
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
|
||||
.select(UserDO::getId)
|
||||
.leftJoin(AddressDO.class, on -> on
|
||||
@ -783,10 +785,11 @@ class LambdaWrapperTest {
|
||||
.leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId)
|
||||
.le(UserDO::getId, 10000)
|
||||
.orderByDesc(UserDO::getId)
|
||||
.setTableName(name -> name + "bbbbbbb");
|
||||
.setTableName(name -> "bbbbbbb");
|
||||
try {
|
||||
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
|
||||
} catch (BadSqlGrammarException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1050,7 +1053,7 @@ class LambdaWrapperTest {
|
||||
*/
|
||||
@Test
|
||||
void sub() {
|
||||
ThreadLocalUtils.set("SELECT ( SELECT st.id FROM `user` st WHERE st.del=false AND (st.id = t.id) limit 1 ) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del=false AND t1.del=false AND (t.id <= ?)");
|
||||
ThreadLocalUtils.set("SELECT (SELECT st.id FROM `user` st WHERE st.del = false AND (st.id = t.id) LIMIT 1) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del = false AND t1.del = false AND (t.id <= ?)");
|
||||
MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class)
|
||||
.selectSub(UserDO.class, w -> w.select(UserDO::getId)
|
||||
.eq(UserDO::getId, UserDO::getId)
|
||||
@ -1059,7 +1062,7 @@ class LambdaWrapperTest {
|
||||
.le(UserDO::getId, 100);
|
||||
wrapper.list();
|
||||
|
||||
ThreadLocalUtils.set("SELECT ( SELECT st.id FROM area st WHERE st.del=false AND (st.id = t1.id) limit 1 ) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del=false AND t1.del=false AND (t.id <= ?)");
|
||||
ThreadLocalUtils.set("SELECT (SELECT st.id FROM area st WHERE st.del = false AND (st.id = t1.id) LIMIT 1) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del = false AND t1.del = false AND (t.id <= ?)");
|
||||
MPJLambdaWrapper<UserDO> wrapper1 = JoinWrappers.lambda(UserDO.class)
|
||||
.selectSub(AreaDO.class, w -> w.select(AreaDO::getId)
|
||||
.eq(AreaDO::getId, AddressDO::getId)
|
||||
|
@ -5,8 +5,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.github.yulichang.query.MPJQueryWrapper;
|
||||
import com.github.yulichang.test.join.dto.UserDTO;
|
||||
import com.github.yulichang.test.join.entity.UserDO;
|
||||
import com.github.yulichang.test.join.entity.UserTenantDO;
|
||||
import com.github.yulichang.test.join.mapper.UserMapper;
|
||||
import com.github.yulichang.test.join.mapper.UserTenantMapper;
|
||||
import com.github.yulichang.test.util.ThreadLocalUtils;
|
||||
import com.github.yulichang.toolkit.JoinWrappers;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
@ -20,6 +23,10 @@ class QueryWrapperTest {
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Resource
|
||||
private UserTenantMapper userTenantMapper;
|
||||
|
||||
|
||||
/**
|
||||
* 链表查询
|
||||
*/
|
||||
@ -30,6 +37,16 @@ class QueryWrapperTest {
|
||||
.select("name AS nameName")
|
||||
.last("LIMIT 1"));
|
||||
System.out.println(dto);
|
||||
|
||||
|
||||
IPage<UserDTO> iPage1 = userTenantMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class,
|
||||
JoinWrappers.query(UserTenantDO.class)
|
||||
.selectAll(UserTenantDO.class)
|
||||
.select("t1.name as PName")
|
||||
.leftJoin("(select * from `user` where id <> -1) t1 on t1.id = t.user_id")
|
||||
.apply("t.id <> -1"));
|
||||
|
||||
iPage1.getRecords().forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,11 +54,11 @@ class QueryWrapperTest {
|
||||
*/
|
||||
@Test
|
||||
void table() {
|
||||
ThreadLocalUtils.set("SELECT t.id,t.pid,t.`name`,t.`json`,t.sex,t.head_img,t.create_time,t.address_id,t.address_id2,t.del,t.create_by,t.update_by,name AS nameName FROM `user`fwear t WHERE t.del=false LIMIT 1",
|
||||
"SELECT t.id,t.pid,t.`name`,t.`json`,t.sex,t.head_img AS img,t.create_time,t.address_id,t.address_id2,t.del,t.create_by,t.update_by,name AS nameName FROM `user`fwear t WHERE t.del=false LIMIT 1");
|
||||
ThreadLocalUtils.set("SELECT t.id,t.pid,t.`name`,t.`json`,t.sex,t.head_img,t.create_time,t.address_id,t.address_id2,t.del,t.create_by,t.update_by,name AS nameName FROM fwear t WHERE t.del=false LIMIT 1",
|
||||
"SELECT t.id,t.pid,t.`name`,t.`json`,t.sex,t.head_img AS img,t.create_time,t.address_id,t.address_id2,t.del,t.create_by,t.update_by,name AS nameName FROM fwear t WHERE t.del=false LIMIT 1");
|
||||
MPJQueryWrapper<UserDO> wrapper = new MPJQueryWrapper<UserDO>()
|
||||
.selectAll(UserDO.class)
|
||||
.setTableName(name -> name + "fwear")
|
||||
.setTableName(name -> "fwear")
|
||||
.select("name AS nameName")
|
||||
.last("LIMIT 1");
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user