映射查询mapper降为BaseMapper

This commit is contained in:
yulichang 2021-12-29 13:02:09 +08:00
parent f86b3f0bc6
commit dbdc357e6e
8 changed files with 92 additions and 72 deletions

View File

@ -1,9 +1,9 @@
package com.baomidou.mybatisplus.core.metadata;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.github.yulichang.annotation.EntityMapping;
import com.github.yulichang.annotation.FieldMapping;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.exception.MPJException;
import com.github.yulichang.toolkit.SpringContentUtils;
import lombok.EqualsAndHashCode;
@ -107,7 +107,7 @@ public class MPJTableFieldInfo {
/**
* 关联的mapper引用
*/
private MPJBaseMapper<?> joinMapper;
private BaseMapper<?> joinMapper;
/**
* 关联查询条件配置
*/
@ -269,14 +269,14 @@ public class MPJTableFieldInfo {
}
}
public MPJBaseMapper<?> getJoinMapper() {
public BaseMapper<?> getJoinMapper() {
if (this.joinMapper == null) {
MPJTableInfo joinTableInfo = MPJTableInfoHelper.getTableInfos().stream().filter(table ->
table.getTableInfo().getEntityType() == this.joinClass).findFirst().orElse(null);
if (joinTableInfo == null) {
throw new MPJException("未注册 mapper " + this.joinClass.getName());
}
this.joinMapper = (MPJBaseMapper<?>) SpringContentUtils.getApplicationContext().getBean(joinTableInfo.getMapperClass());
this.joinMapper = (BaseMapper<?>) SpringContentUtils.getApplicationContext().getBean(joinTableInfo.getMapperClass());
}
return this.joinMapper;
}

View File

@ -1,12 +1,12 @@
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.support.SFunction;
import com.github.yulichang.base.mapper.wrapper.MappingQuery;
import com.github.yulichang.toolkit.LambdaUtils;
import java.io.Serializable;
@ -14,6 +14,8 @@ import java.util.*;
import java.util.stream.Collectors;
/**
* 关联查询
*
* @author yulichang
* @see BaseMapper
*/
@ -337,7 +339,7 @@ public interface MPJDeepMapper<T> extends BaseMapper<T> {
if (!hasProperty || list.contains(fieldInfo.getProperty())) {
Object obj = fieldInfo.thisFieldGet(t);
if (obj != null) {
List<?> joinList = (List<?>) fieldInfo.getJoinMapper().mpjMappingWrapperConstructor(
List<?> joinList = MappingQuery.mpjQueryList(fieldInfo.getJoinMapper(),
fieldInfo.isFieldIsMap(), SqlKeyword.EQ, fieldInfo.getJoinColumn(), obj, fieldInfo);
mpjBindData(t, fieldInfo, joinList);
fieldInfo.removeJoinField(joinList);
@ -369,7 +371,7 @@ public interface MPJDeepMapper<T> extends BaseMapper<T> {
if (!hasProperty || list.contains(fieldInfo.getProperty())) {
Object obj = map.get(fieldInfo.getThisMapKey());
if (obj != null) {
List<?> joinList = (List<?>) fieldInfo.getJoinMapper().mpjMappingWrapperConstructor(
List<?> joinList = MappingQuery.mpjQueryList(fieldInfo.getJoinMapper(),
fieldInfo.isFieldIsMap(), SqlKeyword.EQ, fieldInfo.getJoinColumn(), obj, fieldInfo);
mpjBindMap(map, fieldInfo, joinList);
fieldInfo.removeJoinField(joinList);
@ -401,7 +403,7 @@ public interface MPJDeepMapper<T> extends BaseMapper<T> {
if (!hasProperty || listProperty.contains(fieldInfo.getProperty())) {
List<Object> itemList = list.stream().map(fieldInfo::thisFieldGet).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(itemList)) {
List<?> joinList = (List<?>) fieldInfo.getJoinMapper().mpjMappingWrapperConstructor(
List<?> joinList = MappingQuery.mpjQueryList(fieldInfo.getJoinMapper(),
fieldInfo.isMappingEntity() && fieldInfo.isFieldIsMap(), SqlKeyword.IN,
fieldInfo.getJoinColumn(), itemList, fieldInfo);
list.forEach(i -> mpjBindData(i, fieldInfo, joinList));
@ -437,7 +439,7 @@ public interface MPJDeepMapper<T> extends BaseMapper<T> {
List<Object> itemList = list.stream().map(m -> m.get(fieldInfo.getThisMapKey()))
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(itemList)) {
List<?> joinList = (List<?>) fieldInfo.getJoinMapper().mpjMappingWrapperConstructor(
List<?> joinList = MappingQuery.mpjQueryList(fieldInfo.getJoinMapper(),
fieldInfo.isMappingEntity() && fieldInfo.isFieldIsMap(), SqlKeyword.IN,
fieldInfo.getJoinColumn(), itemList, fieldInfo);
list.forEach(i -> mpjBindMap(i, fieldInfo, joinList));
@ -488,65 +490,4 @@ public interface MPJDeepMapper<T> extends BaseMapper<T> {
}
MPJTableFieldInfo.bindMap(fieldInfo, t, list);
}
/**
* 映射 wrapper 构造器
* 仅对使用映射注解时使用
*/
default Object mpjMappingWrapperConstructor(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 -> {
if (c.getKeyword() == SqlKeyword.BETWEEN) {
wrapper.between(c.getColumn(), c.getVal()[0], c.getVal()[1]);
} else if (c.getKeyword() == SqlKeyword.IN) {
wrapper.in(c.getColumn(), (Object[]) c.getVal());
} else {
wrapper.addCondition(true, c.getColumn(),
c.getKeyword(), c.getVal()[0]);
}
});
}
wrapper.eq(SqlKeyword.EQ == keyword, column, val);
//此处不用链式调用提高效率
if (infoWrapper.isHasFirst()) {
wrapper.first(infoWrapper.getFirst());
}
if (infoWrapper.isHasOrderByAsc()) {
//mybatis plus 3.4.3 之后支持数组但之前版本仅支持可变参数为了兼容多个循环处理
infoWrapper.getOrderByAsc().forEach(wrapper::orderByAsc);
}
if (infoWrapper.isHasOrderByDesc()) {
//mybatis plus 3.4.3 之后支持数组但之前版本仅支持可变参数为了兼容多个循环处理
infoWrapper.getOrderByAsc().forEach(wrapper::orderByDesc);
}
if (infoWrapper.isHasLast()) {
wrapper.last(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);
}
}
}

View File

@ -0,0 +1,71 @@
package com.github.yulichang.base.mapper.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.MPJMappingWrapper;
import com.baomidou.mybatisplus.core.metadata.MPJTableFieldInfo;
import java.util.List;
public class MappingQuery<T> extends QueryWrapper<T> {
/**
* 公开 addCondition 方法
*/
@Override
public QueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
return super.addCondition(condition, column, sqlKeyword, val);
}
/**
* 映射 wrapper 构造器
* 仅对使用映射注解时使用
*/
public static <T> List<?> mpjQueryList(BaseMapper<T> baseMapper, 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 -> {
if (c.getKeyword() == SqlKeyword.BETWEEN) {
wrapper.between(c.getColumn(), c.getVal()[0], c.getVal()[1]);
} else if (c.getKeyword() == SqlKeyword.IN) {
wrapper.in(c.getColumn(), (Object[]) c.getVal());
} else {
wrapper.addCondition(true, c.getColumn(),
c.getKeyword(), c.getVal()[0]);
}
});
}
wrapper.eq(SqlKeyword.EQ == keyword, column, val);
//此处不用链式调用提高效率
if (infoWrapper.isHasFirst()) {
wrapper.first(infoWrapper.getFirst());
}
if (infoWrapper.isHasOrderByAsc()) {
//mybatis plus 3.4.3 之后支持数组但之前版本仅支持可变参数为了兼容多个循环处理
infoWrapper.getOrderByAsc().forEach(wrapper::orderByAsc);
}
if (infoWrapper.isHasOrderByDesc()) {
//mybatis plus 3.4.3 之后支持数组但之前版本仅支持可变参数为了兼容多个循环处理
infoWrapper.getOrderByAsc().forEach(wrapper::orderByDesc);
}
if (infoWrapper.isHasLast()) {
wrapper.last(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 baseMapper.selectMaps(wrapper);
}
return baseMapper.selectList(wrapper);
}
}

View File

@ -14,6 +14,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import java.lang.reflect.Field;
import java.util.List;
@ -24,6 +25,7 @@ import java.util.List;
*
* @author yulichang
*/
@Order(Integer.MIN_VALUE)
@SuppressWarnings("SpringJavaAutowiredMembersInspection")
public class InterceptorConfig implements ApplicationListener<ApplicationReadyEvent> {

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.MPJTableMapperHelper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
/**
* 关系映射配置
@ -12,6 +13,7 @@ import org.springframework.context.ApplicationListener;
* @author yulichang
* @since 1.2.0
*/
@Order(Integer.MIN_VALUE)
public class MappingConfig implements ApplicationListener<ApplicationReadyEvent> {
@Override

View File

@ -29,6 +29,7 @@ import com.github.yulichang.method.mp.SelectPage;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.Order;
import java.util.ArrayList;
import java.util.Arrays;
@ -43,6 +44,7 @@ import static java.util.stream.Collectors.toList;
* @author yulichang
* @see DefaultSqlInjector
*/
@Order(Integer.MIN_VALUE)
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
public class MPJSqlInjector extends DefaultSqlInjector {

View File

@ -23,6 +23,7 @@ import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.core.annotation.Order;
import java.util.ArrayList;
import java.util.List;
@ -38,6 +39,7 @@ import java.util.concurrent.ConcurrentHashMap;
*
* @author yulichang
*/
@Order(Integer.MIN_VALUE)
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
public class MPJInterceptor implements Interceptor {
private static final Log logger = LogFactory.getLog(MPJInterceptor.class);

View File

@ -261,7 +261,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
* select字段
*/
@Data
public static class SelectColumn {
private static class SelectColumn {
/**
* 字段实体类