mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
添加返回MP实体的join方法
This commit is contained in:
parent
31662a6e9e
commit
0519bd47ee
@ -46,6 +46,16 @@ public interface MPJBaseMapper<T> extends BaseMapper<T> {
|
|||||||
*/
|
*/
|
||||||
Long selectJoinCount(@Param(Constants.WRAPPER) MPJBaseJoin<T> wrapper);
|
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);
|
return selectJoinOne(Map.class, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回记录集合
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
* @return List<T>
|
||||||
|
*/
|
||||||
|
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));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 连表查询返回记录集合并分页
|
* 连表查询返回记录集合并分页
|
||||||
*
|
*
|
||||||
|
@ -66,14 +66,14 @@ public class MPJInterceptor implements Interceptor {
|
|||||||
if (Objects.nonNull(ew) && ew instanceof MPJBaseJoin) {
|
if (Objects.nonNull(ew) && ew instanceof MPJBaseJoin) {
|
||||||
if (CollectionUtils.isNotEmpty(map)) {
|
if (CollectionUtils.isNotEmpty(map)) {
|
||||||
Class<?> rt = null;
|
Class<?> rt = null;
|
||||||
if (map.containsKey(Constant.CLAZZ)) {
|
if (map.containsKey(Constant.CLAZZ) && map.get(Constant.CLAZZ) != null) {
|
||||||
rt = (Class<?>) map.get(Constant.CLAZZ);
|
rt = (Class<?>) map.get(Constant.CLAZZ);
|
||||||
} else {
|
} else {
|
||||||
if (CollectionUtils.isNotEmpty(ms.getResultMaps())) {
|
if (CollectionUtils.isNotEmpty(ms.getResultMaps())) {
|
||||||
Class<?> entity = MPJTableMapperHelper.getEntity(getMapper(ms.getId(), ms.getResource()));
|
Class<?> entity = MPJTableMapperHelper.getEntity(getMapper(ms.getId(), ms.getResource()));
|
||||||
Class<?> type = ms.getResultMaps().get(0).getType();
|
Class<?> type = ms.getResultMaps().get(0).getType();
|
||||||
if (Objects.nonNull(entity) && Objects.nonNull(type)
|
if (Objects.nonNull(entity) && Objects.nonNull(type)
|
||||||
&& !MPJReflectionKit.isPrimitiveOrWrapper(type) && entity == type) {
|
&& !MPJReflectionKit.isPrimitiveOrWrapper(type) && (entity == type)) {
|
||||||
rt = type;
|
rt = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package com.github.yulichang.method;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* result type 标识类
|
|
||||||
*
|
|
||||||
* @author yulichang
|
|
||||||
*/
|
|
||||||
public class MPJResultType implements Serializable {
|
|
||||||
}
|
|
@ -32,7 +32,7 @@ public class SelectJoinList extends MPJAbstractMethod {
|
|||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
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
|
@Override
|
||||||
|
@ -31,7 +31,7 @@ public class SelectJoinOne extends MPJAbstractMethod {
|
|||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
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
|
@Override
|
||||||
|
@ -32,7 +32,7 @@ public class SelectJoinPage extends MPJAbstractMethod {
|
|||||||
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true),
|
||||||
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment());
|
||||||
SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass);
|
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
|
@Override
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user