mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-29 18:51:53 +08:00
1. 优化分配菜单的逻辑,使用批量插入
2. 修复 CodegenEngine 生成代码时,Mapper.XML 地址不对
This commit is contained in:
parent
e3ddb29c18
commit
1bb204adb5
@ -75,12 +75,20 @@ public interface BaseMapperX<T> extends BaseMapper<T> {
|
|||||||
return selectList(new LambdaQueryWrapper<T>().in(field, values));
|
return selectList(new LambdaQueryWrapper<T>().in(field, values));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 逐条插入,适合少量数据插入,或者对性能要求不高的场景
|
||||||
|
*
|
||||||
|
* 如果大量,请使用 {@link com.baomidou.mybatisplus.extension.service.impl.ServiceImpl#saveBatch(Collection)} 方法
|
||||||
|
* 使用示例,可见 RoleMenuBatchInsertMapper、UserRoleBatchInsertMapper 类
|
||||||
|
*
|
||||||
|
* @param entities 实体们
|
||||||
|
*/
|
||||||
default void insertBatch(Collection<T> entities) {
|
default void insertBatch(Collection<T> entities) {
|
||||||
// TODO 芋艿:修改成支持批量的
|
|
||||||
entities.forEach(this::insert);
|
entities.forEach(this::insert);
|
||||||
}
|
}
|
||||||
|
|
||||||
default void updateBatch(T update) {
|
default void updateBatch(T update) {
|
||||||
update(update, new QueryWrapper<>());
|
update(update, new QueryWrapper<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import cn.iocoder.yudao.module.system.api.permission.RoleApi;
|
|||||||
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.flowable.bpmn.model.BpmnModel;
|
import org.flowable.bpmn.model.BpmnModel;
|
||||||
import org.flowable.bpmn.model.FlowElement;
|
|
||||||
import org.flowable.bpmn.model.UserTask;
|
import org.flowable.bpmn.model.UserTask;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -217,7 +217,7 @@ public class CodegenEngine {
|
|||||||
private static String mapperXmlFilePath() {
|
private static String mapperXmlFilePath() {
|
||||||
return "yudao-module-${table.moduleName}/" + // 顶级模块
|
return "yudao-module-${table.moduleName}/" + // 顶级模块
|
||||||
"yudao-module-${table.moduleName}-impl/" + // 子模块
|
"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) {
|
private static String vueTemplatePath(String path) {
|
||||||
|
@ -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> {
|
||||||
|
}
|
@ -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.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
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.Mapper;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -14,18 +16,12 @@ import java.util.stream.Collectors;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface RoleMenuMapper extends BaseMapperX<RoleMenuDO> {
|
public interface RoleMenuMapper extends BaseMapperX<RoleMenuDO> {
|
||||||
|
|
||||||
default List<RoleMenuDO> selectListByRoleId(Long roleId) {
|
@Repository
|
||||||
return selectList(new QueryWrapper<RoleMenuDO>().eq("role_id", roleId));
|
class BatchInsertMapper extends ServiceImpl<RoleMenuMapper, RoleMenuDO> {
|
||||||
}
|
}
|
||||||
|
|
||||||
default void insertList(Long roleId, Collection<Long> menuIds) {
|
default List<RoleMenuDO> selectListByRoleId(Long roleId) {
|
||||||
List<RoleMenuDO> list = menuIds.stream().map(menuId -> {
|
return selectList(new QueryWrapper<RoleMenuDO>().eq("role_id", roleId));
|
||||||
RoleMenuDO entity = new RoleMenuDO();
|
|
||||||
entity.setRoleId(roleId);
|
|
||||||
entity.setMenuId(menuId);
|
|
||||||
return entity;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
insertBatch(list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default void deleteListByRoleIdAndMenuIds(Long roleId, Collection<Long> menuIds) {
|
default void deleteListByRoleIdAndMenuIds(Long roleId, Collection<Long> menuIds) {
|
||||||
|
@ -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> {
|
||||||
|
}
|
@ -7,7 +7,6 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserRoleMapper extends BaseMapperX<UserRoleDO> {
|
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));
|
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) {
|
default void deleteListByUserIdAndRoleIdIds(Long userId, Collection<Long> roleIds) {
|
||||||
delete(new QueryWrapper<UserRoleDO>().eq("user_id", userId)
|
delete(new QueryWrapper<UserRoleDO>().eq("user_id", userId)
|
||||||
.in("role_id", roleIds));
|
.in("role_id", roleIds));
|
||||||
|
@ -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.common.util.json.JsonUtils;
|
||||||
import cn.iocoder.yudao.framework.datapermission.core.dept.service.dto.DeptDataPermissionRespDTO;
|
import cn.iocoder.yudao.framework.datapermission.core.dept.service.dto.DeptDataPermissionRespDTO;
|
||||||
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
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.security.core.util.SecurityFrameworkUtils;
|
||||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
|
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.RoleDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO;
|
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.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.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.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.mq.producer.permission.PermissionProducer;
|
||||||
import cn.iocoder.yudao.module.system.service.dept.DeptService;
|
import cn.iocoder.yudao.module.system.service.dept.DeptService;
|
||||||
import com.google.common.collect.ImmutableMultimap;
|
import com.google.common.collect.ImmutableMultimap;
|
||||||
@ -79,7 +81,11 @@ public class PermissionServiceImpl implements PermissionService {
|
|||||||
@Resource
|
@Resource
|
||||||
private RoleMenuMapper roleMenuMapper;
|
private RoleMenuMapper roleMenuMapper;
|
||||||
@Resource
|
@Resource
|
||||||
|
private RoleMenuBatchInsertMapper roleMenuBatchInsertMapper;
|
||||||
|
@Resource
|
||||||
private UserRoleMapper userRoleMapper;
|
private UserRoleMapper userRoleMapper;
|
||||||
|
@Resource
|
||||||
|
private UserRoleBatchInsertMapper userRoleBatchInsertMapper;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RoleService roleService;
|
private RoleService roleService;
|
||||||
@ -202,7 +208,12 @@ public class PermissionServiceImpl implements PermissionService {
|
|||||||
Collection<Long> deleteMenuIds = CollUtil.subtract(dbMenuIds, menuIds);
|
Collection<Long> deleteMenuIds = CollUtil.subtract(dbMenuIds, menuIds);
|
||||||
// 执行新增和删除。对于已经授权的菜单,不用做任何处理
|
// 执行新增和删除。对于已经授权的菜单,不用做任何处理
|
||||||
if (!CollectionUtil.isEmpty(createMenuIds)) {
|
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)) {
|
if (!CollectionUtil.isEmpty(deleteMenuIds)) {
|
||||||
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
|
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
|
||||||
@ -240,7 +251,12 @@ public class PermissionServiceImpl implements PermissionService {
|
|||||||
Collection<Long> deleteMenuIds = CollUtil.subtract(dbRoleIds, roleIds);
|
Collection<Long> deleteMenuIds = CollUtil.subtract(dbRoleIds, roleIds);
|
||||||
// 执行新增和删除。对于已经授权的角色,不用做任何处理
|
// 执行新增和删除。对于已经授权的角色,不用做任何处理
|
||||||
if (!CollectionUtil.isEmpty(createRoleIds)) {
|
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)) {
|
if (!CollectionUtil.isEmpty(deleteMenuIds)) {
|
||||||
userRoleMapper.deleteListByUserIdAndRoleIdIds(userId, deleteMenuIds);
|
userRoleMapper.deleteListByUserIdAndRoleIdIds(userId, deleteMenuIds);
|
||||||
|
@ -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.RoleDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleMenuDO;
|
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.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.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.dal.mysql.permission.UserRoleMapper;
|
||||||
import cn.iocoder.yudao.module.system.mq.producer.permission.PermissionProducer;
|
import cn.iocoder.yudao.module.system.mq.producer.permission.PermissionProducer;
|
||||||
import cn.iocoder.yudao.module.system.service.dept.DeptService;
|
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.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@Import(PermissionServiceImpl.class)
|
@Import({PermissionServiceImpl.class,
|
||||||
|
RoleMenuBatchInsertMapper.class, UserRoleBatchInsertMapper.class})
|
||||||
public class PermissionServiceTest extends BaseDbUnitTest {
|
public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@ -39,7 +42,11 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
|||||||
@Resource
|
@Resource
|
||||||
private RoleMenuMapper roleMenuMapper;
|
private RoleMenuMapper roleMenuMapper;
|
||||||
@Resource
|
@Resource
|
||||||
|
private RoleMenuBatchInsertMapper roleMenuBatchInsertMapper;
|
||||||
|
@Resource
|
||||||
private UserRoleMapper userRoleMapper;
|
private UserRoleMapper userRoleMapper;
|
||||||
|
@Resource
|
||||||
|
private UserRoleBatchInsertMapper userRoleBatchInsertMapper;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private RoleService roleService;
|
private RoleService roleService;
|
||||||
|
Loading…
Reference in New Issue
Block a user