yulichang 2024-01-16 13:08:23 +08:00
parent 05baec13a9
commit fbce257571
7 changed files with 456 additions and 12 deletions

View File

@ -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 {
}
```

View File

@ -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>

View File

@ -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("");
}
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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> {
}