From 1bb204adb53e79d49a89090711b4e47bf465ac58 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 1 Apr 2022 22:40:19 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96=E5=88=86=E9=85=8D?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5=202.=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20CodegenEngine=20=E7=94=9F=E6=88=90=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=97=B6=EF=BC=8CMapper.XML=20=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E4=B8=8D=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/core/mapper/BaseMapperX.java | 10 ++++++++- .../BpmTaskAssignRuleServiceImpl.java | 1 - .../service/codegen/inner/CodegenEngine.java | 2 +- .../permission/RoleMenuBatchInsertMapper.java | 14 ++++++++++++ .../dal/mysql/permission/RoleMenuMapper.java | 16 +++++--------- .../permission/UserRoleBatchInsertMapper.java | 14 ++++++++++++ .../dal/mysql/permission/UserRoleMapper.java | 12 ---------- .../permission/PermissionServiceImpl.java | 22 ++++++++++++++++--- .../permission/PermissionServiceTest.java | 9 +++++++- 9 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuBatchInsertMapper.java create mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleBatchInsertMapper.java 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 4ca5becb3..fb4af6d69 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 @@ -75,12 +75,20 @@ public interface BaseMapperX extends BaseMapper { return selectList(new LambdaQueryWrapper().in(field, values)); } + /** + * 逐条插入,适合少量数据插入,或者对性能要求不高的场景 + * + * 如果大量,请使用 {@link com.baomidou.mybatisplus.extension.service.impl.ServiceImpl#saveBatch(Collection)} 方法 + * 使用示例,可见 RoleMenuBatchInsertMapper、UserRoleBatchInsertMapper 类 + * + * @param entities 实体们 + */ default void insertBatch(Collection entities) { - // TODO 芋艿:修改成支持批量的 entities.forEach(this::insert); } default void updateBatch(T update) { update(update, new QueryWrapper<>()); } + } diff --git a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java index 6d6430da8..7ed096cc0 100644 --- a/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java +++ b/yudao-module-bpm/yudao-module-bpm-impl-flowable/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java @@ -21,7 +21,6 @@ import cn.iocoder.yudao.module.system.api.permission.RoleApi; import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import lombok.extern.slf4j.Slf4j; import org.flowable.bpmn.model.BpmnModel; -import org.flowable.bpmn.model.FlowElement; import org.flowable.bpmn.model.UserTask; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; diff --git a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java index 8fb104318..6fdddd6bd 100644 --- a/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java +++ b/yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java @@ -217,7 +217,7 @@ public class CodegenEngine { private static String mapperXmlFilePath() { return "yudao-module-${table.moduleName}/" + // 顶级模块 "yudao-module-${table.moduleName}-impl/" + // 子模块 - "src/resources/mapper/${table.businessName}/${table.className}Mapper.xml"; + "src/main/java/resources/mapper/${table.businessName}/${table.className}Mapper.xml"; } private static String vueTemplatePath(String path) { diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuBatchInsertMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuBatchInsertMapper.java new file mode 100644 index 000000000..69ddfaf90 --- /dev/null +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuBatchInsertMapper.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.module.system.dal.mysql.permission; + +import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.stereotype.Repository; + +/** + * 实体 {@link RoleMenuDO} 的批量插入 Mapper + * + * @author 芋道源码 + */ +@Repository +public class RoleMenuBatchInsertMapper extends ServiceImpl { +} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuMapper.java index f519b4ee9..8fb84d3bf 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/RoleMenuMapper.java @@ -3,8 +3,10 @@ package cn.iocoder.yudao.module.system.dal.mysql.permission; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; +import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.Date; @@ -14,18 +16,12 @@ import java.util.stream.Collectors; @Mapper public interface RoleMenuMapper extends BaseMapperX { - default List selectListByRoleId(Long roleId) { - return selectList(new QueryWrapper().eq("role_id", roleId)); + @Repository + class BatchInsertMapper extends ServiceImpl { } - default void insertList(Long roleId, Collection menuIds) { - List list = menuIds.stream().map(menuId -> { - RoleMenuDO entity = new RoleMenuDO(); - entity.setRoleId(roleId); - entity.setMenuId(menuId); - return entity; - }).collect(Collectors.toList()); - insertBatch(list); + default List selectListByRoleId(Long roleId) { + return selectList(new QueryWrapper().eq("role_id", roleId)); } default void deleteListByRoleIdAndMenuIds(Long roleId, Collection menuIds) { diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleBatchInsertMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleBatchInsertMapper.java new file mode 100644 index 000000000..474bdb2c1 --- /dev/null +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleBatchInsertMapper.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.module.system.dal.mysql.permission; + +import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.stereotype.Repository; + +/** + * 实体 {@link UserRoleDO} 的批量插入 Mapper + * + * @author 芋道源码 + */ +@Repository +public class UserRoleBatchInsertMapper extends ServiceImpl { +} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleMapper.java index bc1d36cf2..e2ab6ce20 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/permission/UserRoleMapper.java @@ -7,7 +7,6 @@ import org.apache.ibatis.annotations.Mapper; import java.util.Collection; import java.util.List; -import java.util.stream.Collectors; @Mapper public interface UserRoleMapper extends BaseMapperX { @@ -20,17 +19,6 @@ public interface UserRoleMapper extends BaseMapperX { return selectList(new QueryWrapper().eq("role_id", roleId)); } - - default void insertList(Long userId, Collection roleIds) { - List list = roleIds.stream().map(roleId -> { - UserRoleDO entity = new UserRoleDO(); - entity.setUserId(userId); - entity.setRoleId(roleId); - return entity; - }).collect(Collectors.toList()); - insertBatch(list); - } - default void deleteListByUserIdAndRoleIdIds(Long userId, Collection roleIds) { delete(new QueryWrapper().eq("user_id", userId) .in("role_id", roleIds)); diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java index 15c4396bd..4f93879a7 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java @@ -8,7 +8,6 @@ import cn.iocoder.yudao.framework.common.util.collection.MapUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.datapermission.core.dept.service.dto.DeptDataPermissionRespDTO; import cn.iocoder.yudao.framework.security.core.LoginUser; -import cn.iocoder.yudao.module.system.enums.permission.DataScopeEnum; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; @@ -16,8 +15,11 @@ import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO; +import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMenuBatchInsertMapper; import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMenuMapper; +import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleBatchInsertMapper; import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper; +import cn.iocoder.yudao.module.system.enums.permission.DataScopeEnum; import cn.iocoder.yudao.module.system.mq.producer.permission.PermissionProducer; import cn.iocoder.yudao.module.system.service.dept.DeptService; import com.google.common.collect.ImmutableMultimap; @@ -79,7 +81,11 @@ public class PermissionServiceImpl implements PermissionService { @Resource private RoleMenuMapper roleMenuMapper; @Resource + private RoleMenuBatchInsertMapper roleMenuBatchInsertMapper; + @Resource private UserRoleMapper userRoleMapper; + @Resource + private UserRoleBatchInsertMapper userRoleBatchInsertMapper; @Resource private RoleService roleService; @@ -202,7 +208,12 @@ public class PermissionServiceImpl implements PermissionService { Collection deleteMenuIds = CollUtil.subtract(dbMenuIds, menuIds); // 执行新增和删除。对于已经授权的菜单,不用做任何处理 if (!CollectionUtil.isEmpty(createMenuIds)) { - roleMenuMapper.insertList(roleId, createMenuIds); + roleMenuBatchInsertMapper.saveBatch(CollectionUtils.convertList(createMenuIds, menuId -> { + RoleMenuDO entity = new RoleMenuDO(); + entity.setRoleId(roleId); + entity.setMenuId(menuId); + return entity; + })); } if (!CollectionUtil.isEmpty(deleteMenuIds)) { roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds); @@ -240,7 +251,12 @@ public class PermissionServiceImpl implements PermissionService { Collection deleteMenuIds = CollUtil.subtract(dbRoleIds, roleIds); // 执行新增和删除。对于已经授权的角色,不用做任何处理 if (!CollectionUtil.isEmpty(createRoleIds)) { - userRoleMapper.insertList(userId, createRoleIds); + userRoleBatchInsertMapper.saveBatch(CollectionUtils.convertList(createRoleIds, roleId -> { + UserRoleDO entity = new UserRoleDO(); + entity.setUserId(userId); + entity.setRoleId(roleId); + return entity; + })); } if (!CollectionUtil.isEmpty(deleteMenuIds)) { userRoleMapper.deleteListByUserIdAndRoleIdIds(userId, deleteMenuIds); diff --git a/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java b/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java index 9e2e95e1e..ef828c940 100644 --- a/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java +++ b/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java @@ -5,7 +5,9 @@ import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO; import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO; +import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMenuBatchInsertMapper; import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMenuMapper; +import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleBatchInsertMapper; import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper; import cn.iocoder.yudao.module.system.mq.producer.permission.PermissionProducer; import cn.iocoder.yudao.module.system.service.dept.DeptService; @@ -30,7 +32,8 @@ import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@Import(PermissionServiceImpl.class) +@Import({PermissionServiceImpl.class, + RoleMenuBatchInsertMapper.class, UserRoleBatchInsertMapper.class}) public class PermissionServiceTest extends BaseDbUnitTest { @Resource @@ -39,7 +42,11 @@ public class PermissionServiceTest extends BaseDbUnitTest { @Resource private RoleMenuMapper roleMenuMapper; @Resource + private RoleMenuBatchInsertMapper roleMenuBatchInsertMapper; + @Resource private UserRoleMapper userRoleMapper; + @Resource + private UserRoleBatchInsertMapper userRoleBatchInsertMapper; @MockBean private RoleService roleService;