diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 1d94323a2..caef2d31c 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -26,6 +26,7 @@ 3.5.3.1 3.5.3.1 3.6.1 + 1.3.4 3.18.0 2.2.3 @@ -209,6 +210,12 @@ ${dynamic-datasource.version} + + icu.mhb + mybatis-plus-join + ${mybatis-plus-join.version} + + cn.iocoder.boot yudao-spring-boot-starter-redis diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/pom.xml b/yudao-framework/yudao-spring-boot-starter-mybatis/pom.xml index 99c4c6a06..3bfa86871 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/pom.xml @@ -59,6 +59,11 @@ dynamic-datasource-spring-boot-starter + + icu.mhb + mybatis-plus-join + + diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/MybatisPlusJoinConfiguration.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/MybatisPlusJoinConfiguration.java new file mode 100644 index 000000000..3321a0212 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/MybatisPlusJoinConfiguration.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.framework.mybatis.config; + +import com.baomidou.mybatisplus.core.injector.AbstractMethod; +import com.baomidou.mybatisplus.core.metadata.TableInfo; +import icu.mhb.mybatisplus.plugln.injector.JoinDefaultSqlInjector; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +@Configuration +public class MybatisPlusJoinConfiguration extends JoinDefaultSqlInjector { + + @Override + public List getMethodList(Class mapperClass, TableInfo tableInfo) { + return super.getMethodList(mapperClass, tableInfo); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java index a831337e8..56e1deb05 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java @@ -6,10 +6,10 @@ import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.toolkit.Db; +import icu.mhb.mybatisplus.plugln.base.mapper.JoinBaseMapper; import org.apache.ibatis.annotations.Param; import java.util.Collection; @@ -18,7 +18,7 @@ import java.util.List; /** * 在 MyBatis Plus 的 BaseMapper 的基础上拓展,提供更多的能力 */ -public interface BaseMapperX extends BaseMapper { +public interface BaseMapperX extends JoinBaseMapper { default PageResult selectPage(PageParam pageParam, @Param("ew") Wrapper queryWrapper) { // MyBatis Plus 查询 diff --git a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java index 6a9a3ffcb..1b16ba0d1 100644 --- a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java +++ b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java @@ -1,10 +1,13 @@ package cn.iocoder.yudao.framework.test.core.ut; import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; +import cn.iocoder.yudao.framework.mybatis.config.MybatisPlusJoinConfiguration; import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; +import icu.mhb.mybatisplus.plugln.interceptor.JoinInterceptor; +import icu.mhb.mybatisplus.plugln.interceptor.JoinInterceptorConfig; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; @@ -34,6 +37,9 @@ public class BaseDbUnitTest { // MyBatis 配置类 YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 + MybatisPlusJoinConfiguration.class, // MyBatis 的自动配置类 + JoinInterceptor.class, // MyBatis 的Join配置类 + JoinInterceptorConfig.class, // MyBatis 的Join配置类 }) public static class Application { } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java index 287a388d9..2ca77ff37 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java @@ -3,11 +3,15 @@ package cn.iocoder.yudao.module.system.dal.dataobject.user; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.mybatis.core.type.JsonLongSetTypeHandler; import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; +import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; import cn.iocoder.yudao.module.system.enums.common.SexEnum; import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler; +import icu.mhb.mybatisplus.plugln.annotations.JoinField; +import icu.mhb.mybatisplus.plugln.constant.RelevancyType; import lombok.*; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -93,4 +97,13 @@ public class AdminUserDO extends TenantBaseDO { */ private LocalDateTime loginDate; + /** + * 用户所属部门 + */ + @JoinField(masterModelClass = AdminUserDO.class, masterModelField = "deptId", + sunModelClass = DeptDO.class, sunModelField = "id", + relevancyType = RelevancyType.ONT_TO_ONE, sunAlias = "d") + @TableField(exist = false, typeHandler = FastjsonTypeHandler.class) + private DeptDO dept; + } diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java index a5e0183a4..46d096496 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java @@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; +import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper; import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; import cn.iocoder.yudao.module.system.enums.common.SexEnum; @@ -23,6 +24,7 @@ import cn.iocoder.yudao.module.system.service.dept.DeptService; import cn.iocoder.yudao.module.system.service.dept.PostService; import cn.iocoder.yudao.module.system.service.permission.PermissionService; import cn.iocoder.yudao.module.system.service.tenant.TenantService; +import icu.mhb.mybatisplus.plugln.extend.Joins; import org.junit.jupiter.api.Test; import org.mockito.stubbing.Answer; import org.springframework.boot.test.mock.mockito.MockBean; @@ -63,6 +65,8 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { private AdminUserMapper userMapper; @Resource private UserPostMapper userPostMapper; + @Resource + private DeptMapper deptMapper; @MockBean private DeptService deptService; @@ -770,6 +774,37 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { userDO.getNickname()); } + @Test + public void testSelectUserIncludeDept_success() { + // 需设置 application-unit-test.yaml 文件中 spring.main.lazy-initialization: true + + // 准备部门数据 + DeptDO dept = new DeptDO(); + dept.setName("测试"); + dept.setStatus(CommonStatusEnum.ENABLE.getStatus()); + deptMapper.insert(dept); + + // 准备用户参数 + UserCreateReqVO reqVO = randomPojo(UserCreateReqVO.class, o -> { + o.setSex(RandomUtil.randomEle(SexEnum.values()).getSex()); + o.setMobile(randomString()); + o.setPostIds(asSet(1L, 2L)); + o.setDeptId(dept.getId()); + }); + + // 调用 + Long userId = userService.createUser(reqVO); + + // 断言 + AdminUserDO user = Joins.of(AdminUserDO.class) + .leftJoin(DeptDO.class, DeptDO::getId, AdminUserDO::getDeptId) + .end() + .eq(AdminUserDO::getId, userId) + .joinGetOne(AdminUserDO.class); + System.out.println("=========>" + user); + } + + // ========== 随机对象 ========== @SafeVarargs