yulichang 2024-03-20 17:37:28 +08:00
parent 86d94a0264
commit 5c57a0aa6e
5 changed files with 27 additions and 35 deletions

View File

@ -107,7 +107,7 @@ public interface MPJBaseMethod extends Constants {
return filedSqlScript;
}
String newKeyProperty = newPrefix + tableInfo.getKeyProperty();
String keySqlScript = ConfigProperties.tableAlias + DOT + tableInfo.getKeyColumn() + EQUALS +
String keySqlScript = "${ew.alias}" + DOT + tableInfo.getKeyColumn() + EQUALS +
SqlScriptUtils.safeParam(newKeyProperty);
return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false)
+ NEWLINE + filedSqlScript;
@ -116,7 +116,7 @@ public interface MPJBaseMethod extends Constants {
default String getSqlWhere(TableFieldInfo tableFieldInfo, final String prefix) {
final String newPrefix = prefix == null ? EMPTY : prefix;
// 默认: AND column=#{prefix + el}
String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), ConfigProperties.tableAlias + DOT +
String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), "${ew.alias}" + DOT +
tableFieldInfo.getColumn(), newPrefix + tableFieldInfo.getEl());
// 查询的时候只判非空
return convertIf(tableFieldInfo, sqlScript, convertIfProperty(newPrefix, tableFieldInfo.getProperty()),

View File

@ -1,6 +1,5 @@
package com.github.yulichang.toolkit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.yulichang.base.MPJBaseMapper;
@ -12,17 +11,10 @@ import java.util.function.Function;
* @since 1.4.4
*/
@SuppressWarnings("unchecked")
public class SqlHelper {
public final class SqlHelper {
public static <R, T> R exec(Class<T> entityClass, Function<BaseMapper<T>, R> function) {
Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
Object mapper = SpringContentUtils.getMapper(entityClass);
Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName());
return function.apply((BaseMapper<T>) mapper);
}
public static <R, T> R execJoin(Class<T> entityClass, Function<MPJBaseMapper<T>, R> function) {
Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
public static <R, T> R exec(Class<T> entityClass, Function<MPJBaseMapper<T>, R> function) {
Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
Object mapper = SpringContentUtils.getMapper(entityClass);
Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName());
Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> not extends MPJBaseMapper ", entityClass.getSimpleName());

View File

@ -1,10 +1,6 @@
package com.github.yulichang.wrapper.interfaces;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.toolkit.SqlHelper;
@ -33,10 +29,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default Long count() {
return SqlHelper.exec(getEntityClass(), mapper -> {
Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> is not extends MPJBaseMapper", mapper.getClass().getSimpleName());
return ((MPJBaseMapper<T>) mapper).selectJoinCount((MPJBaseJoin<T>) this);
});
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinCount((MPJBaseJoin<T>) this));
}
/**
@ -47,7 +40,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default T one() {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectOne((Wrapper<T>) this));
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinOne(getEntityClass(), (MPJBaseJoin<T>) this));
}
/**
@ -58,7 +51,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default <R> R one(Class<R> resultType) {
return SqlHelper.execJoin(getEntityClass(), mapper -> mapper.selectJoinOne(resultType, (MPJBaseJoin<T>) this));
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinOne(resultType, (MPJBaseJoin<T>) this));
}
/**
@ -69,8 +62,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default T first() {
List<T> list = list();
return CollectionUtils.isEmpty(list) ? null : list.get(0);
return list().stream().findFirst().orElse(null);
}
/**
@ -81,8 +73,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default <R> R first(Class<R> resultType) {
List<R> list = list(resultType);
return CollectionUtils.isEmpty(list) ? null : list.get(0);
return list(resultType).stream().findFirst().orElse(null);
}
/**
@ -92,7 +83,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default List<T> list() {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectList((Wrapper<T>) this));
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinList(getEntityClass(), (MPJBaseJoin<T>) this));
}
/**
@ -102,7 +93,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default <R> List<R> list(Class<R> resultType) {
return SqlHelper.execJoin(getEntityClass(), mapper -> mapper.selectJoinList(resultType, (MPJBaseJoin<T>) this));
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinList(resultType, (MPJBaseJoin<T>) this));
}
/**
@ -112,7 +103,7 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default <P extends IPage<T>> P page(P page) {
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectPage(page, (Wrapper<T>) this));
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinPage(page, getEntityClass(), (MPJBaseJoin<T>) this));
}
/**
@ -122,6 +113,6 @@ public interface Chain<T> {
* JoinWrappers.lambda(User.class)<br />
*/
default <R, P extends IPage<R>> P page(P page, Class<R> resultType) {
return SqlHelper.execJoin(getEntityClass(), mapper -> mapper.selectJoinPage(page, resultType, (MPJBaseJoin<T>) this));
return SqlHelper.exec(getEntityClass(), mapper -> mapper.selectJoinPage(page, resultType, (MPJBaseJoin<T>) this));
}
}

View File

@ -84,4 +84,14 @@ public class TableAliasTest {
List<UserDO> dos = userMapper.selectJoinList(UserDO.class, wrapper);
dos.forEach(System.out::println);
}
@Test
void tableAlias3() {
ThreadLocalUtils.set("SELECT aaa.id, aaa.pid, aaa.`name`, aaa.`json`, aaa.sex, aaa.head_img, aaa.create_time, " +
"aaa.address_id, aaa.address_id2, aaa.del, aaa.create_by, aaa.update_by FROM `user` aaa WHERE aaa.`name` = ? AND aaa.del = false");
UserDO userDO = new UserDO();
userDO.setName("aaa");
MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda("aaa", userDO);
wrapper.list();
}
}

View File

@ -62,20 +62,20 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.30</version>
<version>5.3.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.9.0</version>
<version>1.9.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.7.17</version>
<version>2.7.18</version>
</dependency>
</dependencies>
</dependencyManagement>
@ -87,7 +87,6 @@
<artifactId>flatten-maven-plugin</artifactId>
<version>1.5.0</version>
<configuration>
<!-- 是否更新pom文件此处还有更高级的用法 -->
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>