Commit f5331ce6ace5d909c21ef8e0edb9d7987a4ca3f2
Committed by
Gitee
!16 解决由于mybatis-plus无法过滤软删除导致角色去掉菜单缓存未刷新的问题。
Merge pull request !16 from dxyx/master
Showing
2 changed files
with
7 additions
and
7 deletions
src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/permission/SysRoleMenuMapper.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | 4 | import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleMenuDO; |
| 5 | 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 6 | 6 | import org.apache.ibatis.annotations.Mapper; |
| 7 | +import org.apache.ibatis.annotations.Select; | |
| 7 | 8 | |
| 8 | 9 | import java.util.Collection; |
| 9 | 10 | import java.util.Date; |
| ... | ... | @@ -32,7 +33,7 @@ public interface SysRoleMenuMapper extends BaseMapperX<SysRoleMenuDO> { |
| 32 | 33 | delete(new QueryWrapper<SysRoleMenuDO>().eq("role_id", roleId) |
| 33 | 34 | .in("menu_id", menuIds)); |
| 34 | 35 | } |
| 35 | - | |
| 36 | + | |
| 36 | 37 | default void deleteListByMenuId(Long menuId) { |
| 37 | 38 | delete(new QueryWrapper<SysRoleMenuDO>().eq("menu_id", menuId)); |
| 38 | 39 | } |
| ... | ... | @@ -41,9 +42,7 @@ public interface SysRoleMenuMapper extends BaseMapperX<SysRoleMenuDO> { |
| 41 | 42 | delete(new QueryWrapper<SysRoleMenuDO>().eq("role_id", roleId)); |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | - default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) { | |
| 45 | - return selectOne(new QueryWrapper<SysRoleMenuDO>().select("id") | |
| 46 | - .gt("update_time", maxUpdateTime).last("LIMIT 1")) != null; | |
| 47 | - } | |
| 45 | + @Select("SELECT id FROM sys_role_menu WHERE update_time > #{maxUpdateTime} LIMIT 1") | |
| 46 | + Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); | |
| 48 | 47 | |
| 49 | 48 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java
| ... | ... | @@ -86,6 +86,7 @@ public class SysPermissionServiceImpl implements SysPermissionService { |
| 86 | 86 | @Override |
| 87 | 87 | @PostConstruct |
| 88 | 88 | public void initLocalCache() { |
| 89 | + Date now = new Date(); | |
| 89 | 90 | // 获取角色与菜单的关联列表,如果有更新 |
| 90 | 91 | List<SysRoleMenuDO> roleMenuList = this.loadRoleMenuIfUpdate(maxUpdateTime); |
| 91 | 92 | if (CollUtil.isEmpty(roleMenuList)) { |
| ... | ... | @@ -102,7 +103,7 @@ public class SysPermissionServiceImpl implements SysPermissionService { |
| 102 | 103 | roleMenuCache = roleMenuCacheBuilder.build(); |
| 103 | 104 | menuRoleCache = menuRoleCacheBuilder.build(); |
| 104 | 105 | assert roleMenuList.size() > 0; // 断言,避免告警 |
| 105 | - maxUpdateTime = roleMenuList.stream().max(Comparator.comparing(BaseDO::getUpdateTime)).get().getUpdateTime(); | |
| 106 | + maxUpdateTime = now; | |
| 106 | 107 | log.info("[initLocalCache][初始化角色与菜单的关联数量为 {}]", roleMenuList.size()); |
| 107 | 108 | } |
| 108 | 109 | |
| ... | ... | @@ -123,7 +124,7 @@ public class SysPermissionServiceImpl implements SysPermissionService { |
| 123 | 124 | if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据 |
| 124 | 125 | log.info("[loadRoleMenuIfUpdate][首次加载全量角色与菜单的关联]"); |
| 125 | 126 | } else { // 判断数据库中是否有更新的角色与菜单的关联 |
| 126 | - if (!roleMenuMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) { | |
| 127 | + if (Objects.isNull(roleMenuMapper.selectExistsByUpdateTimeAfter(maxUpdateTime))) { | |
| 127 | 128 | return null; |
| 128 | 129 | } |
| 129 | 130 | log.info("[loadRoleMenuIfUpdate][增量加载全量角色与菜单的关联]"); | ... | ... |