mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
mapper 支持关系映射
This commit is contained in:
parent
e14f9b90d1
commit
5eaef38076
18
MAPPING.md
18
MAPPING.md
@ -30,8 +30,8 @@ QQ群:1022221898
|
|||||||
### 使用
|
### 使用
|
||||||
|
|
||||||
* mapper继承MPJBaseMapper
|
* mapper继承MPJBaseMapper
|
||||||
* service继承MPJBaseService
|
* service继承MPJBaseService (可选)
|
||||||
* serviceImpl继承MPJBaseServiceImpl
|
* serviceImpl继承MPJBaseServiceImpl (可选)
|
||||||
|
|
||||||
#### @MPJMapping注解
|
#### @MPJMapping注解
|
||||||
|
|
||||||
@ -67,18 +67,17 @@ public class UserDO {
|
|||||||
使用
|
使用
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 一对一,一对多关系映射查询
|
* 一对一,一对多关系映射查询
|
||||||
* 映射只对MPJBaseDeepService中的方法有效果 ,一般以Deep结尾,比如 getByIdDeep listByIdsDeep 等
|
* 映射只对MPJBaseDeepService中的方法有效果 ,一般以Deep结尾,比如 getByIdDeep listByIdsDeep 等
|
||||||
* 如果不需要关系映射就使用mybatis plus原生方法即可,比如 getById listByIds 等
|
* 如果不需要关系映射就使用mybatis plus原生方法即可,比如 getById listByIds 等
|
||||||
*
|
*
|
||||||
* @see com.github.yulichang.base.MPJBaseDeepService
|
* @see com.github.yulichang.base.service.MPJDeepService
|
||||||
*/
|
*/
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class MappingTest {
|
class MappingTest {
|
||||||
@Resource
|
@Resource
|
||||||
private UserService userService;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id查询
|
* 根据id查询
|
||||||
@ -88,10 +87,11 @@ class MappingTest {
|
|||||||
* 第一次查询目标UserDO
|
* 第一次查询目标UserDO
|
||||||
* 第二次根据pid查询上级用户
|
* 第二次根据pid查询上级用户
|
||||||
* 第三次根据自身id查询下级用户
|
* 第三次根据自身id查询下级用户
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void test1() {
|
void test1() {
|
||||||
UserDO deep = userService.getByIdDeep(2);
|
UserDO deep = userMapper.selectByIdDeep(1);
|
||||||
System.out.println(deep);
|
System.out.println(deep);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,10 +103,11 @@ class MappingTest {
|
|||||||
* 第一次查询目标UserDO集合
|
* 第一次查询目标UserDO集合
|
||||||
* 第二次根据pid查询上级用户(不会一条记录一条记录的去查询,对pid进行汇总,用in语句一次性查出来,然后进行匹配)
|
* 第二次根据pid查询上级用户(不会一条记录一条记录的去查询,对pid进行汇总,用in语句一次性查出来,然后进行匹配)
|
||||||
* 第三次根据自身id查询下级用户(不会一条记录一条记录的去查询,对id进行汇总,用in语句一次性查出来,然后进行匹配)
|
* 第三次根据自身id查询下级用户(不会一条记录一条记录的去查询,对id进行汇总,用in语句一次性查出来,然后进行匹配)
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void test2() {
|
void test2() {
|
||||||
List<UserDO> list = userService.listDeep();
|
List<UserDO> list = userMapper.selectListDeep(Wrappers.emptyWrapper());
|
||||||
list.forEach(System.out::println);
|
list.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,10 +118,11 @@ class MappingTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void test3() {
|
void test3() {
|
||||||
Page<UserDO> page = userService.pageDeep(new Page<>(2, 2));
|
Page<UserDO> page = userMapper.selectPageDeep(new Page<>(2, 2), Wrappers.emptyWrapper());
|
||||||
page.getRecords().forEach(System.out::println);
|
page.getRecords().forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -1,121 +1,14 @@
|
|||||||
package com.github.yulichang.base;
|
package com.github.yulichang.base;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.github.yulichang.base.mapper.MPJDeepMapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.MPJMappingWrapper;
|
import com.github.yulichang.base.mapper.MPJJoinMapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.MPJTableFieldInfo;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
|
||||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
|
||||||
import com.github.yulichang.toolkit.Constant;
|
|
||||||
import org.apache.ibatis.annotations.Param;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
* @see BaseMapper
|
* @see BaseMapper
|
||||||
*/
|
*/
|
||||||
public interface MPJBaseMapper<T> extends BaseMapper<T> {
|
public interface MPJBaseMapper<T> extends MPJJoinMapper<T>, MPJDeepMapper<T> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据 Wrapper 条件,查询总记录数
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
*/
|
|
||||||
Integer selectJoinCount(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表查询返回一条记录
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
* @param clazz resultType
|
|
||||||
*/
|
|
||||||
<DTO> DTO selectJoinOne(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
|
||||||
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表查询返回Map
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
*/
|
|
||||||
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表查询返回记录集合
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
* @param clazz resultType
|
|
||||||
*/
|
|
||||||
<DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
|
||||||
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表查询返回Map集合
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
*/
|
|
||||||
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表查询返回记录集合并分页
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
* @param clazz resultType
|
|
||||||
* @param <DTO> 分页返回对象
|
|
||||||
*/
|
|
||||||
<DTO, P extends IPage<?>> IPage<DTO> selectJoinPage(P page,
|
|
||||||
@Param(Constant.CLAZZ) Class<DTO> clazz,
|
|
||||||
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 连表查询返回Map集合并分页
|
|
||||||
*
|
|
||||||
* @param wrapper joinWrapper
|
|
||||||
*/
|
|
||||||
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page,
|
|
||||||
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 映射 wrapper 构造器
|
|
||||||
* 仅对使用 @MPJMapping 时使用
|
|
||||||
*/
|
|
||||||
default Object mappingWrapperConstructor(boolean selectMap, SqlKeyword keyword,
|
|
||||||
String column, Object val, MPJTableFieldInfo fieldInfo) {
|
|
||||||
MPJMappingWrapper infoWrapper = fieldInfo.getWrapper();
|
|
||||||
MappingQuery<T> wrapper = new MappingQuery<>();
|
|
||||||
if (infoWrapper.isHasCondition()) {
|
|
||||||
infoWrapper.getConditionList().forEach(c -> wrapper.addCondition(true, c.getColumn(),
|
|
||||||
c.getKeyword(), c.getVal()));
|
|
||||||
}
|
|
||||||
wrapper.eq(SqlKeyword.EQ == keyword, column, val)
|
|
||||||
.first(infoWrapper.isHasFirst(), infoWrapper.getFirst())
|
|
||||||
.last(infoWrapper.isHasLast(), infoWrapper.getLast());
|
|
||||||
if (SqlKeyword.IN == keyword) {
|
|
||||||
wrapper.in(column, (List<?>) val);
|
|
||||||
}
|
|
||||||
if (infoWrapper.isHasSelect()) {
|
|
||||||
wrapper.select(infoWrapper.getSelect());
|
|
||||||
}
|
|
||||||
if (infoWrapper.isHasApply()) {
|
|
||||||
infoWrapper.getApplyList().forEach(a -> wrapper.apply(a.getSql(), (Object[]) a.getVal()));
|
|
||||||
}
|
|
||||||
if (selectMap) {
|
|
||||||
return selectMaps(wrapper);
|
|
||||||
}
|
|
||||||
return selectList(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 公开 addCondition 方法
|
|
||||||
*/
|
|
||||||
class MappingQuery<T> extends QueryWrapper<T> {
|
|
||||||
@Override
|
|
||||||
public QueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
|
|
||||||
return super.addCondition(condition, column, sqlKeyword, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package com.github.yulichang.base;
|
package com.github.yulichang.base;
|
||||||
|
|
||||||
|
import com.github.yulichang.base.service.MPJDeepService;
|
||||||
|
import com.github.yulichang.base.service.MPJJoinService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基础service
|
* 基础service
|
||||||
* 目前包含两个模块 连表查询 和 关系映射
|
* 目前包含两个模块 连表查询 和 关系映射
|
||||||
*
|
*
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
* @see MPJBaseJoinService
|
* @see MPJJoinService
|
||||||
* @see MPJBaseDeepService
|
* @see MPJDeepService
|
||||||
*/
|
*/
|
||||||
public interface MPJBaseService<T> extends MPJBaseJoinService<T>, MPJBaseDeepService<T> {
|
public interface MPJBaseService<T> extends MPJJoinService<T>, MPJDeepService<T> {
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,179 @@
|
|||||||
|
package com.github.yulichang.base.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.*;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see BaseMapper
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public interface MPJDeepMapper<T> extends BaseMapper<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 ID 查询
|
||||||
|
*
|
||||||
|
* @param id 主键ID
|
||||||
|
*/
|
||||||
|
default T selectByIdDeep(Serializable id) {
|
||||||
|
return queryMapping(selectById(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询(根据ID 批量查询)
|
||||||
|
*
|
||||||
|
* @param idList 主键ID列表(不能为 null 以及 empty)
|
||||||
|
*/
|
||||||
|
default List<T> selectBatchIdsDeep(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList) {
|
||||||
|
return queryMapping(selectBatchIds(idList));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询(根据 columnMap 条件)
|
||||||
|
*
|
||||||
|
* @param columnMap 表字段 map 对象
|
||||||
|
*/
|
||||||
|
default List<T> selectByMapDeep(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap) {
|
||||||
|
return queryMapping(selectByMap(columnMap));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 entity 条件,查询一条记录
|
||||||
|
*
|
||||||
|
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||||
|
*/
|
||||||
|
default T selectOneDeep(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
|
||||||
|
return queryMapping(selectOne(queryWrapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 entity 条件,查询全部记录
|
||||||
|
*
|
||||||
|
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||||
|
*/
|
||||||
|
default List<T> selectListDeep(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
|
||||||
|
return queryMapping(selectList(queryWrapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 entity 条件,查询全部记录(并翻页)
|
||||||
|
*
|
||||||
|
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
|
||||||
|
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||||
|
*/
|
||||||
|
default <E extends IPage<T>> E selectPageDeep(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper) {
|
||||||
|
E e = selectPage(page, queryWrapper);
|
||||||
|
queryMapping(e.getRecords());
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询映射关系
|
||||||
|
* 对结果进行二次查询
|
||||||
|
* 可以自行查询然后在通过此方法进行二次查询
|
||||||
|
*
|
||||||
|
* @param t 第一次查询结果
|
||||||
|
*/
|
||||||
|
default T queryMapping(T t) {
|
||||||
|
if (t == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(t.getClass());
|
||||||
|
if (tableInfo.isHasMapping()) {
|
||||||
|
for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) {
|
||||||
|
Object get = fieldInfo.thisFieldGet(t);
|
||||||
|
if (get != null) {
|
||||||
|
List<?> o = (List<?>) fieldInfo.getJoinMapper().mappingWrapperConstructor(fieldInfo.isFieldIsMap(),
|
||||||
|
SqlKeyword.EQ, fieldInfo.getJoinColumn(), get, fieldInfo);
|
||||||
|
MPJTableFieldInfo.bind(fieldInfo, t, o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询映射关系
|
||||||
|
* 对结果进行二次查询
|
||||||
|
* 可以自行查询然后在通过此方法进行二次查询
|
||||||
|
*
|
||||||
|
* @param list 第一次查询结果
|
||||||
|
*/
|
||||||
|
default List<T> queryMapping(List<T> list) {
|
||||||
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(list.get(0).getClass());
|
||||||
|
if (tableInfo.isHasMapping()) {
|
||||||
|
for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) {
|
||||||
|
List<Object> itemList = list.stream().map(fieldInfo::thisFieldGet).collect(Collectors.toList());
|
||||||
|
if (CollectionUtils.isNotEmpty(itemList)) {
|
||||||
|
List<?> joinList = (List<?>) fieldInfo.getJoinMapper().mappingWrapperConstructor(
|
||||||
|
fieldInfo.isFieldIsMap(), SqlKeyword.IN, fieldInfo.getJoinColumn(), itemList, fieldInfo);
|
||||||
|
list.forEach(i -> {
|
||||||
|
List<?> data = joinList.stream().filter(j -> fieldInfo.joinFieldGet(j)
|
||||||
|
.equals(fieldInfo.thisFieldGet(i))).collect(Collectors.toList());
|
||||||
|
MPJTableFieldInfo.bind(fieldInfo, i, data);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
list.forEach(i -> fieldInfo.fieldSet(i, new ArrayList<>()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 映射 wrapper 构造器
|
||||||
|
* 仅对使用 @MPJMapping 时使用
|
||||||
|
*/
|
||||||
|
default Object mappingWrapperConstructor(boolean selectMap, SqlKeyword keyword,
|
||||||
|
String column, Object val, MPJTableFieldInfo fieldInfo) {
|
||||||
|
MPJMappingWrapper infoWrapper = fieldInfo.getWrapper();
|
||||||
|
MappingQuery<T> wrapper = new MappingQuery<>();
|
||||||
|
if (infoWrapper.isHasCondition()) {
|
||||||
|
infoWrapper.getConditionList().forEach(c -> wrapper.addCondition(true, c.getColumn(),
|
||||||
|
c.getKeyword(), c.getVal()));
|
||||||
|
}
|
||||||
|
wrapper.eq(SqlKeyword.EQ == keyword, column, val)
|
||||||
|
.first(infoWrapper.isHasFirst(), infoWrapper.getFirst())
|
||||||
|
.last(infoWrapper.isHasLast(), infoWrapper.getLast());
|
||||||
|
if (SqlKeyword.IN == keyword) {
|
||||||
|
wrapper.in(column, (List<?>) val);
|
||||||
|
}
|
||||||
|
if (infoWrapper.isHasSelect()) {
|
||||||
|
wrapper.select(infoWrapper.getSelect());
|
||||||
|
}
|
||||||
|
if (infoWrapper.isHasApply()) {
|
||||||
|
infoWrapper.getApplyList().forEach(a -> wrapper.apply(a.getSql(), (Object[]) a.getVal()));
|
||||||
|
}
|
||||||
|
if (selectMap) {
|
||||||
|
return selectMaps(wrapper);
|
||||||
|
}
|
||||||
|
return selectList(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公开 addCondition 方法
|
||||||
|
*/
|
||||||
|
class MappingQuery<T> extends QueryWrapper<T> {
|
||||||
|
@Override
|
||||||
|
public QueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
|
||||||
|
return super.addCondition(condition, column, sqlKeyword, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.github.yulichang.base.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
|
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||||
|
import com.github.yulichang.toolkit.Constant;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yulichang
|
||||||
|
* @see BaseMapper
|
||||||
|
*/
|
||||||
|
public interface MPJJoinMapper<T> extends BaseMapper<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 Wrapper 条件,查询总记录数
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
*/
|
||||||
|
Integer selectJoinCount(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回一条记录
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
* @param clazz resultType
|
||||||
|
*/
|
||||||
|
<DTO> DTO selectJoinOne(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||||
|
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回Map
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
*/
|
||||||
|
Map<String, Object> selectJoinMap(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回记录集合
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
* @param clazz resultType
|
||||||
|
*/
|
||||||
|
<DTO> List<DTO> selectJoinList(@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||||
|
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回Map集合
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> selectJoinMaps(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回记录集合并分页
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
* @param clazz resultType
|
||||||
|
* @param <DTO> 分页返回对象
|
||||||
|
*/
|
||||||
|
<DTO, P extends IPage<?>> IPage<DTO> selectJoinPage(P page,
|
||||||
|
@Param(Constant.CLAZZ) Class<DTO> clazz,
|
||||||
|
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连表查询返回Map集合并分页
|
||||||
|
*
|
||||||
|
* @param wrapper joinWrapper
|
||||||
|
*/
|
||||||
|
<P extends IPage<?>> IPage<Map<String, Object>> selectJoinMapsPage(P page,
|
||||||
|
@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.github.yulichang.base;
|
package com.github.yulichang.base.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||||
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
||||||
@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.github.yulichang.annotation.MPJMapping;
|
import com.github.yulichang.annotation.MPJMapping;
|
||||||
|
import com.github.yulichang.base.MPJBaseMapper;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -30,7 +31,7 @@ import java.util.stream.Collectors;
|
|||||||
* @since 1.2.0
|
* @since 1.2.0
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public interface MPJBaseDeepService<T> extends IService<T> {
|
public interface MPJDeepService<T> extends IService<T> {
|
||||||
|
|
||||||
Class<T> currentModelClass();
|
Class<T> currentModelClass();
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @param id 主键ID列表
|
* @param id 主键ID列表
|
||||||
*/
|
*/
|
||||||
default <R> T getByIdDeep(Serializable id) {
|
default <R> T getByIdDeep(Serializable id) {
|
||||||
return queryMapping(getById(id));
|
return ((MPJBaseMapper<T>) getBaseMapper()).selectByIdDeep(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @param idList 主键ID列表
|
* @param idList 主键ID列表
|
||||||
*/
|
*/
|
||||||
default List<T> listByIdsDeep(Collection<? extends Serializable> idList) {
|
default List<T> listByIdsDeep(Collection<? extends Serializable> idList) {
|
||||||
return queryMapping(listByIds(idList));
|
return ((MPJBaseMapper<T>) getBaseMapper()).selectBatchIdsDeep(idList);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,7 +60,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @param columnMap 表字段 map 对象
|
* @param columnMap 表字段 map 对象
|
||||||
*/
|
*/
|
||||||
default List<T> listByMapDeep(Map<String, Object> columnMap) {
|
default List<T> listByMapDeep(Map<String, Object> columnMap) {
|
||||||
return queryMapping(listByMap(columnMap));
|
return ((MPJBaseMapper<T>) getBaseMapper()).selectByMapDeep(columnMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
||||||
*/
|
*/
|
||||||
default T getOneDeep(Wrapper<T> queryWrapper) {
|
default T getOneDeep(Wrapper<T> queryWrapper) {
|
||||||
return queryMapping(getOne(queryWrapper));
|
return ((MPJBaseMapper<T>) getBaseMapper()).selectOneDeep(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -81,7 +82,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @param throwEx 有多个 result 是否抛出异常
|
* @param throwEx 有多个 result 是否抛出异常
|
||||||
*/
|
*/
|
||||||
default T getOneDeep(Wrapper<T> queryWrapper, boolean throwEx) {
|
default T getOneDeep(Wrapper<T> queryWrapper, boolean throwEx) {
|
||||||
return queryMapping(getOne(queryWrapper, throwEx));
|
return ((MPJBaseMapper<T>) getBaseMapper()).queryMapping(getOne(queryWrapper, throwEx));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,7 +115,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
* @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
|
||||||
*/
|
*/
|
||||||
default List<T> listDeep(Wrapper<T> queryWrapper) {
|
default List<T> listDeep(Wrapper<T> queryWrapper) {
|
||||||
return queryMapping(list(queryWrapper));
|
return ((MPJBaseMapper<T>) getBaseMapper()).selectListDeep(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,7 +124,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
* @see Wrappers#emptyWrapper()
|
* @see Wrappers#emptyWrapper()
|
||||||
*/
|
*/
|
||||||
default List<T> listDeep() {
|
default List<T> listDeep() {
|
||||||
return queryMapping(list());
|
return ((MPJBaseMapper<T>) getBaseMapper()).selectListDeep(Wrappers.emptyWrapper());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,9 +135,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
*/
|
*/
|
||||||
default <E extends IPage<T>> E pageDeep(E page, Wrapper<T> queryWrapper) {
|
default <E extends IPage<T>> E pageDeep(E page, Wrapper<T> queryWrapper) {
|
||||||
E e = page(page, queryWrapper);
|
E e = page(page, queryWrapper);
|
||||||
if (CollectionUtils.isNotEmpty(e.getRecords())) {
|
((MPJBaseMapper<T>) getBaseMapper()).queryMapping(e.getRecords());
|
||||||
queryMapping(e.getRecords());
|
|
||||||
}
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,9 +147,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
*/
|
*/
|
||||||
default <E extends IPage<T>> E pageDeep(E page) {
|
default <E extends IPage<T>> E pageDeep(E page) {
|
||||||
E e = page(page);
|
E e = page(page);
|
||||||
if (CollectionUtils.isNotEmpty(e.getRecords())) {
|
((MPJBaseMapper<T>) getBaseMapper()).queryMapping(e.getRecords());
|
||||||
queryMapping(e.getRecords());
|
|
||||||
}
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,9 +178,7 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
*/
|
*/
|
||||||
default <E extends IPage<Map<String, Object>>> E pageMapsDeep(E page, Wrapper<T> queryWrapper) {
|
default <E extends IPage<Map<String, Object>>> E pageMapsDeep(E page, Wrapper<T> queryWrapper) {
|
||||||
E e = pageMaps(page, queryWrapper);
|
E e = pageMaps(page, queryWrapper);
|
||||||
if (CollectionUtils.isNotEmpty(e.getRecords())) {
|
|
||||||
queryMapMapping(e.getRecords());
|
queryMapMapping(e.getRecords());
|
||||||
}
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,68 +190,10 @@ public interface MPJBaseDeepService<T> extends IService<T> {
|
|||||||
*/
|
*/
|
||||||
default <E extends IPage<Map<String, Object>>> E pageMapsDeep(E page) {
|
default <E extends IPage<Map<String, Object>>> E pageMapsDeep(E page) {
|
||||||
E e = pageMaps(page);
|
E e = pageMaps(page);
|
||||||
if (CollectionUtils.isNotEmpty(e.getRecords())) {
|
|
||||||
queryMapMapping(e.getRecords());
|
queryMapMapping(e.getRecords());
|
||||||
}
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询映射关系
|
|
||||||
* 对结果进行二次查询
|
|
||||||
* 可以自行查询然后在通过此方法进行二次查询
|
|
||||||
*
|
|
||||||
* @param t 第一次查询结果
|
|
||||||
*/
|
|
||||||
default T queryMapping(T t) {
|
|
||||||
if (t == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(currentModelClass());
|
|
||||||
if (tableInfo.isHasMapping()) {
|
|
||||||
for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) {
|
|
||||||
Object get = fieldInfo.thisFieldGet(t);
|
|
||||||
if (get != null) {
|
|
||||||
List<?> o = (List<?>) fieldInfo.getJoinMapper().mappingWrapperConstructor(fieldInfo.isFieldIsMap(),
|
|
||||||
SqlKeyword.EQ, fieldInfo.getJoinColumn(), get, fieldInfo);
|
|
||||||
MPJTableFieldInfo.bind(fieldInfo, t, o);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询映射关系
|
|
||||||
* 对结果进行二次查询
|
|
||||||
* 可以自行查询然后在通过此方法进行二次查询
|
|
||||||
*
|
|
||||||
* @param list 第一次查询结果
|
|
||||||
*/
|
|
||||||
default List<T> queryMapping(List<T> list) {
|
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(currentModelClass());
|
|
||||||
if (tableInfo.isHasMapping()) {
|
|
||||||
for (MPJTableFieldInfo fieldInfo : tableInfo.getFieldList()) {
|
|
||||||
List<Object> itemList = list.stream().map(fieldInfo::thisFieldGet).collect(Collectors.toList());
|
|
||||||
if (CollectionUtils.isNotEmpty(itemList)) {
|
|
||||||
List<?> joinList = (List<?>) fieldInfo.getJoinMapper().mappingWrapperConstructor(
|
|
||||||
fieldInfo.isFieldIsMap(), SqlKeyword.IN, fieldInfo.getJoinColumn(), itemList, fieldInfo);
|
|
||||||
list.forEach(i -> {
|
|
||||||
List<?> data = joinList.stream().filter(j -> fieldInfo.joinFieldGet(j)
|
|
||||||
.equals(fieldInfo.thisFieldGet(i))).collect(Collectors.toList());
|
|
||||||
MPJTableFieldInfo.bind(fieldInfo, i, data);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
list.forEach(i -> fieldInfo.fieldSet(i, new ArrayList<>()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询映射关系
|
* 查询映射关系
|
@ -1,7 +1,8 @@
|
|||||||
package com.github.yulichang.base;
|
package com.github.yulichang.base.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.github.yulichang.base.MPJBaseMapper;
|
||||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -12,7 +13,7 @@ import java.util.Map;
|
|||||||
* @see IService
|
* @see IService
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public interface MPJBaseJoinService<T> extends IService<T> {
|
public interface MPJJoinService<T> extends IService<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 Wrapper 条件,查询总记录数
|
* 根据 Wrapper 条件,查询总记录数
|
Loading…
x
Reference in New Issue
Block a user