diff --git a/mybatis-plus-join-solon-plugin/README.md b/mybatis-plus-join-solon-plugin/README.md index 6f8e809..2b12503 100644 --- a/mybatis-plus-join-solon-plugin/README.md +++ b/mybatis-plus-join-solon-plugin/README.md @@ -6,4 +6,37 @@ mybatis-plus-join-solon-plugin lastVersion +``` + +### mapper继承JoinBaseMapper + +```java +import com.github.yulichang.base.MPJBaseMapper; + +@Mapper +public interface UserMapper extends MPJBaseMapper { + +} +``` + +### (可选)service继承JoinBaseService + +```java +import com.github.yulichang.mybatisplusjoin.solon.plugin.base.JoinService; + +public interface UserService extends JoinService { + +} +``` + +### (可选)serviceImpl继承JoinBaseServiceImpl + +```java +import com.github.yulichang.mybatisplusjoin.solon.plugin.base.JoinServiceImpl; +import org.noear.solon.annotation.Component; + +@Component +public class UserServiceImpl extends JoinServiceImpl implements UserService { + +} ``` \ No newline at end of file diff --git a/mybatis-plus-join-solon-plugin/pom.xml b/mybatis-plus-join-solon-plugin/pom.xml index 5fd1732..b27d424 100644 --- a/mybatis-plus-join-solon-plugin/pom.xml +++ b/mybatis-plus-join-solon-plugin/pom.xml @@ -24,12 +24,6 @@ mybatis-plus-join-extension ${revision} - - com.baomidou - mybatis-plus-core - ${mpj.mybatis.plus.version} - provided - org.noear solon @@ -44,7 +38,7 @@ org.noear - mybatis-plus-solon-plugin + mybatis-plus-extension-solon-plugin ${solon.varrsion} provided diff --git a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java index f39dc0c..7db2be8 100644 --- a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java +++ b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java @@ -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(""); } } } \ No newline at end of file diff --git a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinDeepService.java b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinDeepService.java new file mode 100644 index 0000000..70629e8 --- /dev/null +++ b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinDeepService.java @@ -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; + +/** + * 深度查询 + *

+ * 对配置了映射注解的字段进行查询 + * 目前查询深度只支持2级(只解析当前实体类的映射注解,不会对查询结果再次解析注解) + * 多级查询可能存在循环引用的问题,也可能会导致全量查询 + * + * @author yulichang + * @see EntityMapping + * @see FieldMapping + * @since 1.2.0 + */ +@SuppressWarnings({"unused"}) +public interface JoinDeepService extends IService { + + /** + * 根据 ID 查询 并关联全部映射 + * + * @param id 主键ID + */ + default T getByIdDeep(Serializable id) { + return Relation.mpjGetRelation(getById(id), DeepConfig.defaultConfig()); + } + + /** + * 根据 ID 查询 并关联指定映射 + *

+ * JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution) + * + * @param id 主键ID + * @param config 映射配置 + */ + default T getByIdDeep(Serializable id, Function, DeepConfig.Builder> config) { + return Relation.mpjGetRelation(getById(id), config.apply(DeepConfig.builder()).build()); + } + + /** + * 针对可变参数堆污染提供的重载 + * list为null或空,会查询全部映射关系 + *

+ * 例: selectByIdDeep(1, Arrays.asList(User::getId, ... )) + * + * @param id 主键ID + * @param config 映射配置 + */ + default T getByIdDeep(Serializable id, DeepConfig config) { + return Relation.mpjGetRelation(getById(id), config); + } + + /** + * 查询(根据ID 批量查询) + * + * @param idList 主键ID列表(不能为 null 以及 empty) + */ + default List listByIdsDeep(Collection idList) { + return Relation.mpjGetRelation(listByIds(idList), DeepConfig.defaultConfig()); + } + + /** + * 查询(根据ID 批量查询) + *

+ * JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution) + * + * @param idList 主键ID列表(不能为 null 以及 empty) + * @param config 映射配置 + */ + default List listByIdsDeep(Collection idList, Function, DeepConfig.Builder> config) { + return Relation.mpjGetRelation(listByIds(idList), config.apply(DeepConfig.builder()).build()); + } + + /** + * 针对可变参数堆污染提供的重载 + * list为null或空,会查询全部映射关系 + *

+ * 例: selectBatchIdsDeep(idList, Arrays.asList(User::getId, ... )) + * + * @param idList 主键ID列表(不能为 null 以及 empty) + * @param config 映射配置 + */ + default List listByIdsDeep(Collection idList, DeepConfig config) { + return Relation.mpjGetRelation(listByIds(idList), config); + } + + /** + * 查询(根据 columnMap 条件) + * + * @param columnMap 表字段 map 对象 + */ + default List listByMapDeep(Map columnMap) { + return Relation.mpjGetRelation(listByMap(columnMap), DeepConfig.defaultConfig()); + } + + /** + * 查询(根据 columnMap 条件) + *

+ * JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution) + * + * @param columnMap 表字段 map 对象 + * @param config 映射配置 + */ + default List listByMapDeep(Map columnMap, Function, DeepConfig.Builder> config) { + return Relation.mpjGetRelation(listByMap(columnMap), config.apply(DeepConfig.builder()).build()); + } + + /** + * 针对可变参数堆污染提供的重载 + * list为null或空,会查询全部映射关系 + *

+ * 例: selectByMapDeep(columnMap, Arrays.asList(User::getId, ... )) + * + * @param columnMap 表字段 map 对象 + * @param config 映射配置 + */ + default List listByMapDeep(Map columnMap, DeepConfig config) { + return Relation.mpjGetRelation(listByMap(columnMap), config); + } + + /** + * 根据 entity 条件,查询一条记录 + * + * @param queryWrapper 实体对象封装操作类(可以为 null) + */ + default T getOneDeep(Wrapper queryWrapper) { + return Relation.mpjGetRelation(getOne(queryWrapper), DeepConfig.defaultConfig()); + } + + /** + * 根据 entity 条件,查询一条记录 + *

+ * JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution) + * + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param config 映射配置 + */ + default T getOneDeep(Wrapper queryWrapper, Function, DeepConfig.Builder> config) { + return Relation.mpjGetRelation(getOne(queryWrapper), config.apply(DeepConfig.builder()).build()); + } + + /** + * 针对可变参数堆污染提供的重载 + * list为null或空,会查询全部映射关系 + *

+ * 例: selectOneDeep(queryWrapper, Arrays.asList(User::getId, ... )) + * + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param config 映射配置 + */ + default T getOneDeep(Wrapper queryWrapper, DeepConfig config) { + return Relation.mpjGetRelation(getOne(queryWrapper), config); + } + + /** + * 根据 entity 条件,查询全部记录 + * + * @param queryWrapper 实体对象封装操作类(可以为 null) + */ + default List listDeep(Wrapper queryWrapper) { + return Relation.mpjGetRelation(list(queryWrapper), DeepConfig.defaultConfig()); + } + + /** + * 根据 entity 条件,查询全部记录 + *

+ * JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution) + * + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param config 映射配置 + */ + default List listDeep(Wrapper queryWrapper, Function, DeepConfig.Builder> config) { + return Relation.mpjGetRelation(list(queryWrapper), config.apply(DeepConfig.builder()).build()); + } + + /** + * 针对可变参数堆污染提供的重载 + * list为null或空,会查询全部映射关系 + *

+ * 例: selectListDeep(queryWrapper, Arrays.asList(User::getId, ... )) + * + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param config 映射配置 + */ + default List listDeep(Wrapper queryWrapper, DeepConfig config) { + return Relation.mpjGetRelation(list(queryWrapper), config); + } + + /** + * 根据 entity 条件,查询全部记录(并翻页) + * + * @param page 分页查询条件(可以为 RowBounds.DEFAULT) + * @param queryWrapper 实体对象封装操作类(可以为 null) + */ + default > E pageDeep(E page, Wrapper queryWrapper) { + E e = page(page, queryWrapper); + Relation.mpjGetRelation(e.getRecords(), DeepConfig.defaultConfig()); + return e; + } + + /** + * 根据 entity 条件,查询全部记录(并翻页) + *

+ * JDK 默认不推荐泛型数组,会引起 Java堆污染(Heap Pollution) + * + * @param page 分页查询条件(可以为 RowBounds.DEFAULT) + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param config 映射配置 + */ + default > E pageDeep(E page, Wrapper queryWrapper, Function, DeepConfig.Builder> config) { + E e = page(page, queryWrapper); + Relation.mpjGetRelation(e.getRecords(), config.apply(DeepConfig.builder()).build()); + return e; + } + + /** + * 针对可变参数堆污染提供的重载 + * list为null或空,会查询全部映射关系 + *

+ * 例: selectPageDeep(page, queryWrapper, Arrays.asList(User::getId, ... )) + * + * @param page 分页查询条件(可以为 RowBounds.DEFAULT) + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param config 映射配置 + */ + default > E pageDeep(E page, Wrapper queryWrapper, DeepConfig config) { + E e = page(page, queryWrapper); + Relation.mpjGetRelation(e.getRecords(), config); + return e; + } +} diff --git a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinRelationService.java b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinRelationService.java new file mode 100644 index 0000000..8d76afe --- /dev/null +++ b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinRelationService.java @@ -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; + +/** + * 深度查询 + *

+ * 对配置了映射注解的字段进行查询 + * 目前查询深度只支持2级(只解析当前实体类的映射注解,不会对查询结果再次解析注解) + * 多级查询可能存在循环引用的问题,也可能会导致全量查询 + * 用于替换deep + * + * @author yulichang + * @see EntityMapping + * @see FieldMapping + * @since 1.4.4 + */ +@SuppressWarnings({"unchecked", "unused"}) +public interface JoinRelationService extends IService { + + /** + * 通过注解实现单表多次查询 + * + * @param function BaseMapper调用方法 + * @see EntityMapping + * @see FieldMapping + */ + default > R getRelation(Function function) { + return Relation.mpjGetRelation(function.apply((M) getBaseMapper()), DeepConfig.defaultConfig()); + } + + /** + * 通过注解实现单表多次查询 + * + * @param function BaseMapper调用方法 + * @see EntityMapping + * @see FieldMapping + */ + default > R getRelation(Function function, DeepConfig config) { + return Relation.mpjGetRelation(function.apply((M) getBaseMapper()), config); + } + + /** + * 通过注解实现单表多次查询 + * + * @param function BaseMapper调用方法 + * @param config 映射配置 + * @see EntityMapping + * @see FieldMapping + */ + default > R getRelation(Function function, Function, DeepConfig.Builder> config) { + return Relation.mpjGetRelation(function.apply((M) getBaseMapper()), config.apply(DeepConfig.builder()).build()); + } +} diff --git a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinService.java b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinService.java new file mode 100644 index 0000000..13af811 --- /dev/null +++ b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinService.java @@ -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 extends IService { + + + /** + * 根据 Wrapper 条件,连表删除 + * + * @param wrapper joinWrapper + */ + default boolean deleteJoin(MPJBaseJoin wrapper) { + return SqlHelper.retBool(((MPJBaseMapper) getBaseMapper()).deleteJoin(wrapper)); + } + + /** + * 根据 whereEntity 条件,更新记录 + * + * @param entity 实体对象 (set 条件值,可以为 null) + * @param wrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) + */ + default boolean updateJoin(T entity, MPJBaseJoin wrapper) { + return SqlHelper.retBool(((MPJBaseMapper) getBaseMapper()).updateJoin(entity, wrapper)); + } + + /** + * 根据 whereEntity 条件,更新记录 (null字段也会更新 !!!) + * + * @param entity 实体对象 (set 条件值,可以为 null) + * @param wrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) + */ + default boolean updateJoinAndNull(T entity, MPJBaseJoin wrapper) { + return SqlHelper.retBool(((MPJBaseMapper) getBaseMapper()).updateJoinAndNull(entity, wrapper)); + } + + /** + * 根据 Wrapper 条件,查询总记录数 + */ + default Long selectJoinCount(MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinCount(wrapper); + } + + /** + * 连接查询返回一条记录 + */ + default DTO selectJoinOne(Class clazz, MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinOne(clazz, wrapper); + } + + /** + * 连接查询返回集合 + */ + default List selectJoinList(Class clazz, MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinList(clazz, wrapper); + } + + /** + * 连接查询返回集合并分页 + */ + default > P selectJoinListPage(P page, Class clazz, MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinPage(page, clazz, wrapper); + } + + /** + * 连接查询返回Map + */ + default Map selectJoinMap(MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinMap(wrapper); + } + + /** + * 连接查询返回Map集合 + */ + default List> selectJoinMaps(MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinMaps(wrapper); + } + + /** + * 连接查询返回Map集合并分页 + */ + default

>> P selectJoinMapsPage(P page, MPJBaseJoin wrapper) { + return ((MPJBaseMapper) getBaseMapper()).selectJoinMapsPage(page, wrapper); + } +} diff --git a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinServiceImpl.java b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinServiceImpl.java new file mode 100644 index 0000000..6d577b0 --- /dev/null +++ b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/base/JoinServiceImpl.java @@ -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, T> extends ServiceImpl implements JoinService { + +}