mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
This commit is contained in:
parent
05baec13a9
commit
fbce257571
@ -6,4 +6,37 @@
|
||||
<artifactId>mybatis-plus-join-solon-plugin</artifactId>
|
||||
<version>lastVersion</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### mapper继承JoinBaseMapper
|
||||
|
||||
```java
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserMapper extends MPJBaseMapper<UserDO> {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### (可选)service继承JoinBaseService
|
||||
|
||||
```java
|
||||
import com.github.yulichang.mybatisplusjoin.solon.plugin.base.JoinService;
|
||||
|
||||
public interface UserService extends JoinService<UserDO> {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### (可选)serviceImpl继承JoinBaseServiceImpl
|
||||
|
||||
```java
|
||||
import com.github.yulichang.mybatisplusjoin.solon.plugin.base.JoinServiceImpl;
|
||||
import org.noear.solon.annotation.Component;
|
||||
|
||||
@Component
|
||||
public class UserServiceImpl extends JoinServiceImpl<UserMapper, UserDO> implements UserService {
|
||||
|
||||
}
|
||||
```
|
@ -24,12 +24,6 @@
|
||||
<artifactId>mybatis-plus-join-extension</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-core</artifactId>
|
||||
<version>${mpj.mybatis.plus.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>solon</artifactId>
|
||||
@ -44,7 +38,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
<artifactId>mybatis-plus-solon-plugin</artifactId>
|
||||
<artifactId>mybatis-plus-extension-solon-plugin</artifactId>
|
||||
<version>${solon.varrsion}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
@ -76,10 +76,9 @@ public class XPluginImpl implements Plugin {
|
||||
|
||||
private final Properties props;
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public Prop(Props props) {
|
||||
this.props = props.entrySet().stream().filter(e -> format(e.getKey().toString())
|
||||
.startsWith("MYBATISPLUSJOIN.")).collect(Collectors.toMap(e -> e.getKey().toString()
|
||||
this.props = props.entrySet().stream().filter(e -> format(e.getKey())
|
||||
.startsWith(format("mybatis-plus-join."))).collect(Collectors.toMap(e -> e.getKey().toString()
|
||||
.substring(e.getKey().toString().lastIndexOf(".") + 1)
|
||||
.toUpperCase(Locale.ENGLISH), Map.Entry::getValue, (o, n) -> n, Properties::new));
|
||||
}
|
||||
@ -95,8 +94,9 @@ public class XPluginImpl implements Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
private String format(String key) {
|
||||
return key.replaceAll("[-_]", "").toUpperCase(Locale.ENGLISH);
|
||||
private String format(Object key) {
|
||||
return Optional.ofNullable(key).map(k -> k.toString().replaceAll("[-_]", "")
|
||||
.toUpperCase(Locale.ENGLISH)).orElse("");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,244 @@
|
||||
package com.github.yulichang.mybatisplusjoin.solon.plugin.base;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.solon.service.IService;
|
||||
import com.github.yulichang.annotation.EntityMapping;
|
||||
import com.github.yulichang.annotation.FieldMapping;
|
||||
import com.github.yulichang.extension.mapping.config.DeepConfig;
|
||||
import com.github.yulichang.extension.mapping.relation.Relation;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 深度查询
|
||||
* <p>
|
||||
* 对配置了映射注解的字段进行查询
|
||||
* 目前查询深度只支持2级(只解析当前实体类的映射注解,不会对查询结果再次解析注解)
|
||||
* 多级查询可能存在循环引用的问题,也可能会导致全量查询
|
||||
*
|
||||
* @author yulichang
|
||||
* @see EntityMapping
|
||||
* @see FieldMapping
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@SuppressWarnings({"unused"})
|
||||
public interface JoinDeepService<T> extends IService<T> {
|
||||
|
||||
/**
|
||||
* 根据 ID 查询 并关联全部映射
|
||||
*
|
||||
* @param id 主键ID
|
||||
*/
|
||||
default T getByIdDeep(Serializable id) {
|
||||
return Relation.mpjGetRelation(getById(id), DeepConfig.defaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询 并关联指定映射
|
||||
* <p>
|
||||
* JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution)
|
||||
*
|
||||
* @param id 主键ID
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> T getByIdDeep(Serializable id, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
return Relation.mpjGetRelation(getById(id), config.apply(DeepConfig.builder()).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对可变参数堆污染提供的重载
|
||||
* list为null或空,会查询全部映射关系
|
||||
* <p>
|
||||
* 例: selectByIdDeep(1, Arrays.asList(User::getId, ... ))
|
||||
*
|
||||
* @param id 主键ID
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> T getByIdDeep(Serializable id, DeepConfig<T> config) {
|
||||
return Relation.mpjGetRelation(getById(id), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询(根据ID 批量查询)
|
||||
*
|
||||
* @param idList 主键ID列表(不能为 null 以及 empty)
|
||||
*/
|
||||
default List<T> listByIdsDeep(Collection<? extends Serializable> idList) {
|
||||
return Relation.mpjGetRelation(listByIds(idList), DeepConfig.defaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询(根据ID 批量查询)
|
||||
* <p>
|
||||
* JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution)
|
||||
*
|
||||
* @param idList 主键ID列表(不能为 null 以及 empty)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> List<T> listByIdsDeep(Collection<? extends Serializable> idList, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
return Relation.mpjGetRelation(listByIds(idList), config.apply(DeepConfig.builder()).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对可变参数堆污染提供的重载
|
||||
* list为null或空,会查询全部映射关系
|
||||
* <p>
|
||||
* 例: selectBatchIdsDeep(idList, Arrays.asList(User::getId, ... ))
|
||||
*
|
||||
* @param idList 主键ID列表(不能为 null 以及 empty)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> List<T> listByIdsDeep(Collection<? extends Serializable> idList, DeepConfig<T> config) {
|
||||
return Relation.mpjGetRelation(listByIds(idList), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询(根据 columnMap 条件)
|
||||
*
|
||||
* @param columnMap 表字段 map 对象
|
||||
*/
|
||||
default List<T> listByMapDeep(Map<String, Object> columnMap) {
|
||||
return Relation.mpjGetRelation(listByMap(columnMap), DeepConfig.defaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询(根据 columnMap 条件)
|
||||
* <p>
|
||||
* JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution)
|
||||
*
|
||||
* @param columnMap 表字段 map 对象
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> List<T> listByMapDeep(Map<String, Object> columnMap, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
return Relation.mpjGetRelation(listByMap(columnMap), config.apply(DeepConfig.builder()).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对可变参数堆污染提供的重载
|
||||
* list为null或空,会查询全部映射关系
|
||||
* <p>
|
||||
* 例: selectByMapDeep(columnMap, Arrays.asList(User::getId, ... ))
|
||||
*
|
||||
* @param columnMap 表字段 map 对象
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> List<T> listByMapDeep(Map<String, Object> columnMap, DeepConfig<T> config) {
|
||||
return Relation.mpjGetRelation(listByMap(columnMap), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 entity 条件,查询一条记录
|
||||
*
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
*/
|
||||
default T getOneDeep(Wrapper<T> queryWrapper) {
|
||||
return Relation.mpjGetRelation(getOne(queryWrapper), DeepConfig.defaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 entity 条件,查询一条记录
|
||||
* <p>
|
||||
* JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution)
|
||||
*
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> T getOneDeep(Wrapper<T> queryWrapper, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
return Relation.mpjGetRelation(getOne(queryWrapper), config.apply(DeepConfig.builder()).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对可变参数堆污染提供的重载
|
||||
* list为null或空,会查询全部映射关系
|
||||
* <p>
|
||||
* 例: selectOneDeep(queryWrapper, Arrays.asList(User::getId, ... ))
|
||||
*
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> T getOneDeep(Wrapper<T> queryWrapper, DeepConfig<T> config) {
|
||||
return Relation.mpjGetRelation(getOne(queryWrapper), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 entity 条件,查询全部记录
|
||||
*
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
*/
|
||||
default List<T> listDeep(Wrapper<T> queryWrapper) {
|
||||
return Relation.mpjGetRelation(list(queryWrapper), DeepConfig.defaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 entity 条件,查询全部记录
|
||||
* <p>
|
||||
* JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution)
|
||||
*
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> List<T> listDeep(Wrapper<T> queryWrapper, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
return Relation.mpjGetRelation(list(queryWrapper), config.apply(DeepConfig.builder()).build());
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对可变参数堆污染提供的重载
|
||||
* list为null或空,会查询全部映射关系
|
||||
* <p>
|
||||
* 例: selectListDeep(queryWrapper, Arrays.asList(User::getId, ... ))
|
||||
*
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R> List<T> listDeep(Wrapper<T> queryWrapper, DeepConfig<T> config) {
|
||||
return Relation.mpjGetRelation(list(queryWrapper), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 entity 条件,查询全部记录(并翻页)
|
||||
*
|
||||
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
*/
|
||||
default <E extends IPage<T>> E pageDeep(E page, Wrapper<T> queryWrapper) {
|
||||
E e = page(page, queryWrapper);
|
||||
Relation.mpjGetRelation(e.getRecords(), DeepConfig.defaultConfig());
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 entity 条件,查询全部记录(并翻页)
|
||||
* <p>
|
||||
* JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution)
|
||||
*
|
||||
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R, E extends IPage<T>> E pageDeep(E page, Wrapper<T> queryWrapper, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
E e = page(page, queryWrapper);
|
||||
Relation.mpjGetRelation(e.getRecords(), config.apply(DeepConfig.builder()).build());
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* 针对可变参数堆污染提供的重载
|
||||
* list为null或空,会查询全部映射关系
|
||||
* <p>
|
||||
* 例: selectPageDeep(page, queryWrapper, Arrays.asList(User::getId, ... ))
|
||||
*
|
||||
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
|
||||
* @param queryWrapper 实体对象封装操作类(可以为 null)
|
||||
* @param config 映射配置
|
||||
*/
|
||||
default <R, E extends IPage<T>> E pageDeep(E page, Wrapper<T> queryWrapper, DeepConfig<T> config) {
|
||||
E e = page(page, queryWrapper);
|
||||
Relation.mpjGetRelation(e.getRecords(), config);
|
||||
return e;
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.github.yulichang.mybatisplusjoin.solon.plugin.base;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.solon.service.IService;
|
||||
import com.github.yulichang.annotation.EntityMapping;
|
||||
import com.github.yulichang.annotation.FieldMapping;
|
||||
import com.github.yulichang.extension.mapping.config.DeepConfig;
|
||||
import com.github.yulichang.extension.mapping.relation.Relation;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 深度查询
|
||||
* <p>
|
||||
* 对配置了映射注解的字段进行查询
|
||||
* 目前查询深度只支持2级(只解析当前实体类的映射注解,不会对查询结果再次解析注解)
|
||||
* 多级查询可能存在循环引用的问题,也可能会导致全量查询
|
||||
* 用于替换deep
|
||||
*
|
||||
* @author yulichang
|
||||
* @see EntityMapping
|
||||
* @see FieldMapping
|
||||
* @since 1.4.4
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "unused"})
|
||||
public interface JoinRelationService<T> extends IService<T> {
|
||||
|
||||
/**
|
||||
* 通过注解实现单表多次查询
|
||||
*
|
||||
* @param function BaseMapper调用方法
|
||||
* @see EntityMapping
|
||||
* @see FieldMapping
|
||||
*/
|
||||
default <R, M extends BaseMapper<T>> R getRelation(Function<M, R> function) {
|
||||
return Relation.mpjGetRelation(function.apply((M) getBaseMapper()), DeepConfig.defaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过注解实现单表多次查询
|
||||
*
|
||||
* @param function BaseMapper调用方法
|
||||
* @see EntityMapping
|
||||
* @see FieldMapping
|
||||
*/
|
||||
default <R, M extends BaseMapper<T>> R getRelation(Function<M, R> function, DeepConfig<T> config) {
|
||||
return Relation.mpjGetRelation(function.apply((M) getBaseMapper()), config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过注解实现单表多次查询
|
||||
*
|
||||
* @param function BaseMapper调用方法
|
||||
* @param config 映射配置
|
||||
* @see EntityMapping
|
||||
* @see FieldMapping
|
||||
*/
|
||||
default <R, M extends BaseMapper<T>> R getRelation(Function<M, R> function, Function<DeepConfig.Builder<T>, DeepConfig.Builder<T>> config) {
|
||||
return Relation.mpjGetRelation(function.apply((M) getBaseMapper()), config.apply(DeepConfig.builder()).build());
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package com.github.yulichang.mybatisplusjoin.solon.plugin.base;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.solon.service.IService;
|
||||
import com.baomidou.mybatisplus.solon.toolkit.SqlHelper;
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 基础service
|
||||
* 目前包含两个模块 连表查询 和 关系映射
|
||||
*
|
||||
* @author yulichang
|
||||
*/
|
||||
@SuppressWarnings({"unused"})
|
||||
public interface JoinService<T> extends IService<T> {
|
||||
|
||||
|
||||
/**
|
||||
* 根据 Wrapper 条件,连表删除
|
||||
*
|
||||
* @param wrapper joinWrapper
|
||||
*/
|
||||
default boolean deleteJoin(MPJBaseJoin<T> wrapper) {
|
||||
return SqlHelper.retBool(((MPJBaseMapper<T>) getBaseMapper()).deleteJoin(wrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 whereEntity 条件,更新记录
|
||||
*
|
||||
* @param entity 实体对象 (set 条件值,可以为 null)
|
||||
* @param wrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
|
||||
*/
|
||||
default boolean updateJoin(T entity, MPJBaseJoin<T> wrapper) {
|
||||
return SqlHelper.retBool(((MPJBaseMapper<T>) getBaseMapper()).updateJoin(entity, wrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 whereEntity 条件,更新记录 (null字段也会更新 !!!)
|
||||
*
|
||||
* @param entity 实体对象 (set 条件值,可以为 null)
|
||||
* @param wrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
|
||||
*/
|
||||
default boolean updateJoinAndNull(T entity, MPJBaseJoin<T> wrapper) {
|
||||
return SqlHelper.retBool(((MPJBaseMapper<T>) getBaseMapper()).updateJoinAndNull(entity, wrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Wrapper 条件,查询总记录数
|
||||
*/
|
||||
default Long selectJoinCount(MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinCount(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回一条记录
|
||||
*/
|
||||
default <DTO> DTO selectJoinOne(Class<DTO> clazz, MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinOne(clazz, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回集合
|
||||
*/
|
||||
default <DTO> List<DTO> selectJoinList(Class<DTO> clazz, MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinList(clazz, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回集合并分页
|
||||
*/
|
||||
default <DTO, P extends IPage<DTO>> P selectJoinListPage(P page, Class<DTO> clazz, MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinPage(page, clazz, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回Map
|
||||
*/
|
||||
default Map<String, Object> selectJoinMap(MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinMap(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回Map集合
|
||||
*/
|
||||
default List<Map<String, Object>> selectJoinMaps(MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinMaps(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接查询返回Map集合并分页
|
||||
*/
|
||||
default <P extends IPage<Map<String, Object>>> P selectJoinMapsPage(P page, MPJBaseJoin<T> wrapper) {
|
||||
return ((MPJBaseMapper<T>) getBaseMapper()).selectJoinMapsPage(page, wrapper);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.github.yulichang.mybatisplusjoin.solon.plugin.base;
|
||||
|
||||
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
|
||||
import com.github.yulichang.base.MPJBaseMapper;
|
||||
|
||||
/**
|
||||
* @author yulichang
|
||||
* @see ServiceImpl
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class JoinServiceImpl<M extends MPJBaseMapper<T>, T> extends ServiceImpl<M, T> implements JoinService<T> {
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user