优化代码

This commit is contained in:
admin 2021-03-12 11:48:47 +08:00
parent 1debe1d825
commit bcb8c345e6
14 changed files with 94 additions and 27 deletions

View File

@ -87,10 +87,10 @@ class MpJoinTest {
@Test
void testS() {
UserDTO dto = userMapper.userLeftJoin(new QueryWrapper<UserDO>()
.eq(S.a(UserDO::getId), "1")
.gt(S.a(UserDO::getSex), "3")
.eq(S.b(UserAddressDO::getTel), "10086")
.like(S.b(UserAddressDO::getAddress), "北京"));
.eq(S.a(UserDO::getId), "1")//a.id
.gt(S.a(UserDO::getSex), "3")//a.sex
.eq(S.b(UserAddressDO::getTel), "10086")//b.tel
.like(S.b(UserAddressDO::getAddress), "北京"));//b.address
}
/**
@ -99,10 +99,10 @@ class MpJoinTest {
@Test
void testF() {
UserDTO dto = userMapper.userLeftJoin(new QueryWrapper<UserDO>()
.eq(F.s(UserDO::getId), "1")
.gt(F.s(UserDO::getSex), "3")
.eq(F.s(UserAddressDO::getTel), "10086")
.like(F.s(UserAddressDO::getAddress), "北京"));
.eq(F.s(UserDO::getId), "1")//user.id
.gt(F.s(UserDO::getSex), "3")//user.sex
.eq(F.s(UserAddressDO::getTel), "10086")//user_address.tel
.like(F.s(UserAddressDO::getAddress), "北京"));//user_address.address
}
}
```

View File

@ -33,4 +33,14 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), selectColumns);
}
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);
}
}

View File

@ -6,14 +6,16 @@ import org.apache.ibatis.mapping.SqlSource;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
*
* @author yulichang
*/
public class SelectJoinList extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Object.class);
}

View File

@ -8,14 +8,16 @@ import java.util.Map;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
*
* @author yulichang
*/
public class SelectJoinMap extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
}

View File

@ -8,14 +8,16 @@ import java.util.Map;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
*
* @author yulichang
*/
public class SelectJoinMaps extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
}

View File

@ -8,14 +8,16 @@ import java.util.Map;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
*
* @author yulichang
*/
public class SelectJoinMapsPage extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class);
}

View File

@ -6,14 +6,16 @@ import org.apache.ibatis.mapping.SqlSource;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
*
* @author yulichang
*/
public class SelectJoinOne extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Object.class);
}

View File

@ -6,14 +6,16 @@ import org.apache.ibatis.mapping.SqlSource;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectMaps}
*
* @author yulichang
*/
public class SelectJoinPage extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE;
String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Object.class);
}

View File

@ -15,22 +15,22 @@ public enum SqlMethod {
* 连表查询
*/
SELECT_JOIN_ONE("selectJoinOne", "返回一条记录",
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>"),
SELECT_JOIN_LIST("selectJoinList", "返回List集合",
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>"),
SELECT_JOIN_MAP("selectJoinMap", "返回一个Map",
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>"),
SELECT_JOIN_MAPS("selectJoinMaps", "返回Map集合",
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>"),
SELECT_JOIN_PAGE("selectJoinPage", "连表查询并分页",
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>"),
SELECT_JOIN_MAPS_PAGE("selectJoinMapsPage", "返回Map集合并分页",
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>");
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>");
private final String method;
private final String desc;

View File

@ -27,6 +27,8 @@ import java.util.stream.Collectors;
* copy {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper}
* <p>
* sqlSelect 由覆盖改为追加
*
* @author yulichang
*/
@SuppressWarnings("all")
public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambdaQueryWrapper<T>>

View File

@ -22,6 +22,8 @@ import java.util.stream.Collectors;
/**
* copy {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
*
* @author yulichang
*/
@SuppressWarnings("serial")
public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapper<T>>

View File

@ -13,7 +13,6 @@ import java.util.Objects;
* @see com.baomidou.mybatisplus.core.toolkit.LambdaUtils
* @see org.apache.ibatis.reflection.property.PropertyNamer
*/
public final class LambdaUtils {
/**

View File

@ -0,0 +1,40 @@
package com.github.yulichang.toolkit;
import com.github.yulichang.common.JoinLambdaWrapper;
import com.github.yulichang.common.support.alias.AliasLambdaQueryWrapper;
import com.github.yulichang.common.support.alias.AliasQueryWrapper;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJJoinLambdaQueryWrapper;
/**
* Wrapper 条件构造
*
* @author yulichang
*/
public class Wrappers {
public static <T> MPJQueryWrapper<T> queryJoin() {
return new MPJQueryWrapper<>();
}
public static <T> MPJLambdaQueryWrapper<T> lambdaJoin() {
return new MPJLambdaQueryWrapper<>();
}
public static <T> MPJJoinLambdaQueryWrapper<T> lambdaJoinWrapper() {
return new MPJJoinLambdaQueryWrapper<>();
}
public static <T> JoinLambdaWrapper<T> commonJoin() {
return new JoinLambdaWrapper<>();
}
public static <T> AliasQueryWrapper<T> aliasQueryJoin() {
return new AliasQueryWrapper<>();
}
public static <T> AliasLambdaQueryWrapper<T> aliasLambdaJoin() {
return new AliasLambdaQueryWrapper<>();
}
}

View File

@ -24,6 +24,8 @@ import java.util.stream.Collectors;
/**
* copy {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper}
*
* @author yulichang
*/
@SuppressWarnings("all")
public class MPJJoinLambdaQueryWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJJoinLambdaQueryWrapper<T>>