动态表名

This commit is contained in:
yulichang 2023-03-13 14:24:14 +08:00
parent 3211eba2d5
commit 7d616e0d6c
6 changed files with 82 additions and 32 deletions

View File

@ -1,7 +1,6 @@
package com.github.yulichang.base;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.yulichang.base.mapper.MPJDeepMapper;
import com.github.yulichang.base.mapper.MPJJoinMapper;
import com.github.yulichang.base.mapper.MPJRelationMapper;
@ -9,7 +8,7 @@ import com.github.yulichang.base.mapper.MPJRelationMapper;
* @author yulichang
* @see BaseMapper
*/
public interface MPJBaseMapper<T> extends MPJJoinMapper<T>, MPJRelationMapper<T>, MPJDeepMapper<T> {
public interface MPJBaseMapper<T> extends MPJJoinMapper<T>, MPJRelationMapper<T> {
}

View File

@ -1,6 +1,5 @@
package com.github.yulichang.base;
import com.github.yulichang.base.service.MPJDeepService;
import com.github.yulichang.base.service.MPJJoinService;
/**
@ -10,5 +9,5 @@ import com.github.yulichang.base.service.MPJJoinService;
* @author yulichang
* @see MPJJoinService
*/
public interface MPJBaseService<T> extends MPJJoinService<T>, MPJDeepService<T> {
public interface MPJBaseService<T> extends MPJJoinService<T> {
}

View File

@ -21,6 +21,13 @@ import java.util.function.Function;
*/
public interface MPJRelationMapper<T> extends BaseMapper<T> {
/**
* 通过注解实现单表多次查询
*
* @param function BaseMapper调用方法
* @see com.github.yulichang.annotation.EntityMapping
* @see com.github.yulichang.annotation.FieldMapping
*/
default <R, M extends BaseMapper<T>> R selectRelation(Function<M, R> function) {
return selectRelation(function, new ArrayList<>());
}
@ -38,8 +45,7 @@ public interface MPJRelationMapper<T> extends BaseMapper<T> {
R r = function.apply((M) this);
if (Objects.isNull(r)) {
return null;
}
if (r instanceof List) {
} else if (r instanceof List) {
List<T> data = (List<T>) r;
if (CollectionUtils.isEmpty(data)) {
return r;
@ -53,8 +59,7 @@ public interface MPJRelationMapper<T> extends BaseMapper<T> {
}
return (R) Relation.list(data, list);
}
}
if (r instanceof IPage) {
} else if (r instanceof IPage) {
IPage<T> data = (IPage<T>) r;
if (!CollectionUtils.isEmpty(data.getRecords())) {
T t = data.getRecords().get(0);
@ -67,20 +72,16 @@ public interface MPJRelationMapper<T> extends BaseMapper<T> {
Relation.list(data.getRecords(), list);
}
return r;
}
if (r instanceof Integer) {
} else if (r instanceof Integer) {
return r;
}
if (r instanceof Long) {
} else if (r instanceof Long) {
return r;
}
if (r instanceof Boolean) {
} else if (r instanceof Boolean) {
return r;
}
if (Object.class == r.getClass()) {
} else if (Object.class == r.getClass()) {
return r;
} else {
return (R) Relation.one((T) r, list);
}
T data = (T) r;
return (R) Relation.one(data, list);
}
}

View File

@ -0,0 +1,46 @@
package com.github.yulichang.base.service;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.yulichang.annotation.EntityMapping;
import com.github.yulichang.annotation.FieldMapping;
import com.github.yulichang.base.mapper.MPJRelationMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
* 深度查询
* <p>
* 对配置了映射注解的字段进行查询
* 目前查询深度只支持2级(只解析当前实体类的映射注解,不会对查询结果再次解析注解)
* 多级查询可能存在循环引用的问题也可能会导致全量查询
* 用于替换deep
*
* @author yulichang
* @see EntityMapping
* @see FieldMapping
* @since 1.4.4
*/
@SuppressWarnings({"unused"})
public interface MPJRelationService<T> extends IService<T> {
default <R, M extends BaseMapper<T>> R selectRelation(Function<M, R> function) {
return selectRelation(function, new ArrayList<>());
}
/**
* 通过注解实现单表多次查询
*
* @param function BaseMapper调用方法
* @param list 属性过滤, 可以只查询需要映射的属性
* @see com.github.yulichang.annotation.EntityMapping
* @see com.github.yulichang.annotation.FieldMapping
*/
default <R, M extends BaseMapper<T>> R selectRelation(Function<M, R> function, List<SFunction<T, ?>> list) {
return ((MPJRelationMapper<T>) getBaseMapper()).selectRelation(function, list);
}
}

View File

@ -1,12 +1,11 @@
package com.github.yulichang.test.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.yulichang.test.util.ThreadLocalUtils;
import lombok.SneakyThrows;
import org.apache.ibatis.builder.SqlSourceBuilder;
@ -51,11 +50,8 @@ public class MybatisPlusConfig {
@SneakyThrows
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
String sql = boundSql.getSql();
String s = ThreadLocalUtils.get();
if (StringUtils.isNotBlank(s)) {
ObjectMapper mapper = new ObjectMapper();
List<String> sqlList = mapper.readValue(s, new TypeReference<List<String>>() {
});
List<String> sqlList = ThreadLocalUtils.get();
if (CollectionUtils.isNotEmpty(sqlList)) {
if (sqlList.stream().anyMatch(e -> Objects.equals(formatSql(sql), formatSql(e)))) {
System.out.println("===============================================");
System.out.println();
@ -79,11 +75,8 @@ public class MybatisPlusConfig {
if (sql.toUpperCase().startsWith("SELECT")) {
return;
}
String s = ThreadLocalUtils.get();
if (StringUtils.isNotBlank(s)) {
ObjectMapper mapper = new ObjectMapper();
List<String> sqlList = mapper.readValue(s, new TypeReference<List<String>>() {
});
List<String> sqlList = ThreadLocalUtils.get();
if (CollectionUtils.isNotEmpty(sqlList)) {
if (sqlList.stream().anyMatch(e -> Objects.equals(formatSql(sql), formatSql(e)))) {
System.out.println("===============================================");
System.out.println();

View File

@ -1,9 +1,13 @@
package com.github.yulichang.test.util;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import java.util.Arrays;
import java.util.List;
public class ThreadLocalUtils {
@ -24,9 +28,17 @@ public class ThreadLocalUtils {
/**
* 获取线程中的数据
*/
public static String get() {
@SneakyThrows
public static List<String> get() {
String s = userThreadLocal.get();
if (StringUtils.isBlank(s)) {
return null;
}
ObjectMapper mapper = new ObjectMapper();
List<String> sqlList = mapper.readValue(s, new TypeReference<List<String>>() {
});
sqlList.removeIf(StringUtils::isBlank);
set("");
return s;
return sqlList;
}
}