!114 1. 优化分配菜单的逻辑,使用批量插入 2. 修复 CodegenEngine 生成代码时,Mapper.XML 地址不对

Merge pull request !114 from 芋道源码/feature/1.6.2
This commit is contained in:
芋道源码 2022-04-01 14:43:57 +00:00 committed by Gitee
commit 3263b0ab5b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 71 additions and 29 deletions

View File

@ -75,12 +75,20 @@ public interface BaseMapperX<T> extends BaseMapper<T> {
return selectList(new LambdaQueryWrapper<T>().in(field, values));
}
/**
* 逐条插入适合少量数据插入或者对性能要求不高的场景
*
* 如果大量请使用 {@link com.baomidou.mybatisplus.extension.service.impl.ServiceImpl#saveBatch(Collection)} 方法
* 使用示例可见 RoleMenuBatchInsertMapperUserRoleBatchInsertMapper
*
* @param entities 实体们
*/
default void insertBatch(Collection<T> entities) {
// TODO 芋艿修改成支持批量的
entities.forEach(this::insert);
}
default void updateBatch(T update) {
update(update, new QueryWrapper<>());
}
}

View File

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

View File

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

View File

@ -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<RoleMenuMapper, RoleMenuDO> {
}

View File

@ -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<RoleMenuDO> {
default List<RoleMenuDO> selectListByRoleId(Long roleId) {
return selectList(new QueryWrapper<RoleMenuDO>().eq("role_id", roleId));
@Repository
class BatchInsertMapper extends ServiceImpl<RoleMenuMapper, RoleMenuDO> {
}
default void insertList(Long roleId, Collection<Long> menuIds) {
List<RoleMenuDO> list = menuIds.stream().map(menuId -> {
RoleMenuDO entity = new RoleMenuDO();
entity.setRoleId(roleId);
entity.setMenuId(menuId);
return entity;
}).collect(Collectors.toList());
insertBatch(list);
default List<RoleMenuDO> selectListByRoleId(Long roleId) {
return selectList(new QueryWrapper<RoleMenuDO>().eq("role_id", roleId));
}
default void deleteListByRoleIdAndMenuIds(Long roleId, Collection<Long> menuIds) {

View File

@ -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<UserRoleMapper, UserRoleDO> {
}

View File

@ -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<UserRoleDO> {
@ -20,17 +19,6 @@ public interface UserRoleMapper extends BaseMapperX<UserRoleDO> {
return selectList(new QueryWrapper<UserRoleDO>().eq("role_id", roleId));
}
default void insertList(Long userId, Collection<Long> roleIds) {
List<UserRoleDO> 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<Long> roleIds) {
delete(new QueryWrapper<UserRoleDO>().eq("user_id", userId)
.in("role_id", roleIds));

View File

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

View File

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