添加返回MP实体的join方法

This commit is contained in:
yulichang 2024-09-20 12:23:24 +08:00
parent 31662a6e9e
commit 0519bd47ee
7 changed files with 82 additions and 16 deletions

View File

@ -46,6 +46,16 @@ public interface MPJBaseMapper<T> extends BaseMapper<T> {
*/
Long selectJoinCount(@Param(Constants.WRAPPER) MPJBaseJoin<T> wrapper);
/**
* 连表查询返回一条记录
*
* @param wrapper joinWrapper
* @return T
*/
default T selectJoinOne(@Param(Constants.WRAPPER) MPJBaseJoin<T> wrapper) {
return selectJoinOne(null, wrapper);
}
/**
* 连表查询返回一条记录
*
@ -65,6 +75,16 @@ public interface MPJBaseMapper<T> extends BaseMapper<T> {
return selectJoinOne(Map.class, wrapper);
}
/**
* 连表查询返回记录集合
*
* @param wrapper joinWrapper
* @return List&lt;T&gt;
*/
default List<T> selectJoinList(@Param(Constants.WRAPPER) MPJBaseJoin<T> wrapper) {
return selectJoinList(null, wrapper);
}
/**
* 连表查询返回记录集合
*
@ -84,6 +104,15 @@ public interface MPJBaseMapper<T> extends BaseMapper<T> {
return (List<Map<String, Object>>) ((Object) selectJoinList(Map.class, wrapper));
}
/**
* 连表查询返回记录集合并分页
*
* @param wrapper joinWrapper
*/
default <P extends IPage<T>> P selectJoinPage(P page, @Param(Constants.WRAPPER) MPJBaseJoin<T> wrapper) {
return selectJoinPage(page, null, wrapper);
}
/**
* 连表查询返回记录集合并分页
*

View File

@ -66,14 +66,14 @@ public class MPJInterceptor implements Interceptor {
if (Objects.nonNull(ew) && ew instanceof MPJBaseJoin) {
if (CollectionUtils.isNotEmpty(map)) {
Class<?> rt = null;
if (map.containsKey(Constant.CLAZZ)) {
if (map.containsKey(Constant.CLAZZ) && map.get(Constant.CLAZZ) != null) {
rt = (Class<?>) map.get(Constant.CLAZZ);
} else {
if (CollectionUtils.isNotEmpty(ms.getResultMaps())) {
Class<?> entity = MPJTableMapperHelper.getEntity(getMapper(ms.getId(), ms.getResource()));
Class<?> type = ms.getResultMaps().get(0).getType();
if (Objects.nonNull(entity) && Objects.nonNull(type)
&& !MPJReflectionKit.isPrimitiveOrWrapper(type) && entity == type) {
&& !MPJReflectionKit.isPrimitiveOrWrapper(type) && (entity == type)) {
rt = type;
}
}

View File

@ -1,11 +0,0 @@
package com.github.yulichang.method;
import java.io.Serializable;
/**
* result type 标识类
*
* @author yulichang
*/
public class MPJResultType implements Serializable {
}

View File

@ -32,7 +32,7 @@ public class SelectJoinList extends MPJAbstractMethod {
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, removeExtraWhitespaces(sql), modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo.getEntityType());
}
@Override

View File

@ -31,7 +31,7 @@ public class SelectJoinOne extends MPJAbstractMethod {
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo.getEntityType());
}
@Override

View File

@ -32,7 +32,7 @@ public class SelectJoinPage extends MPJAbstractMethod {
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, removeExtraWhitespaces(sql), modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo.getEntityType());
}
@Override

View File

@ -0,0 +1,48 @@
package com.github.yulichang.test.join.unit;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.test.join.entity.UserDO;
import com.github.yulichang.test.join.mapper.UserMapper;
import com.github.yulichang.test.util.Reset;
import com.github.yulichang.toolkit.JoinWrappers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
@SuppressWarnings("NewClassNamingConvention")
public class SelectJoinT_Test {
@Autowired
private UserMapper userMapper;
@BeforeEach
void setUp() {
Reset.reset();
}
@Test
void selectJoinT() {
UserDO userDO = userMapper.selectJoinOne(JoinWrappers.<UserDO>lambda().eq(UserDO::getId, 1));
assert userDO != null;
System.out.println(userDO);
List<UserDO> list = userMapper.selectJoinList(JoinWrappers.lambda());
assert !list.isEmpty();
list.forEach(i -> {
assert i != null;
System.out.println(i);
});
Page<UserDO> page = userMapper.selectJoinPage(new Page<>(1, 10), JoinWrappers.lambda());
assert !page.getRecords().isEmpty();
page.getRecords().forEach(i -> {
assert i != null;
System.out.println(i);
});
}
}