Commit 0bac28386426a3a349c4cd5fcd7e6b10ba06d418
1 parent
251bd085
解决 Spring 定时任务下,无租户编号的问题,导致查询报错
Showing
4 changed files
with
17 additions
and
18 deletions
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dept/DeptMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.system.dal.mysql.dept; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; | |
| 4 | 3 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | 4 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 6 | 5 | import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
| ... | ... | @@ -8,6 +7,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; |
| 8 | 7 | import com.baomidou.mybatisplus.annotation.InterceptorIgnore; |
| 9 | 8 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 10 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 10 | +import org.apache.ibatis.annotations.Select; | |
| 11 | 11 | |
| 12 | 12 | import java.util.Date; |
| 13 | 13 | import java.util.List; |
| ... | ... | @@ -29,10 +29,13 @@ public interface DeptMapper extends BaseMapperX<DeptDO> { |
| 29 | 29 | return selectCount(DeptDO::getParentId, parentId); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | + @Select("SELECT id FROM system_dept WHERE update_time > #{maxUpdateTime} LIMIT 1") | |
| 32 | 33 | @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 |
| 33 | - default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) { | |
| 34 | - return selectOne(new LambdaQueryWrapper<DeptDO>().select(DeptDO::getId) | |
| 35 | - .gt(DeptDO::getUpdateTime, maxUpdateTime).last(SqlConstants.LIMIT1)) != null; | |
| 36 | - } | |
| 34 | + Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); | |
| 35 | + | |
| 36 | + // TODO 芋艿:后续想想,有没可能优化下。大体思路,是支持某个方法,忽略租户 | |
| 37 | + @Select("SELECT * FROM system_dept") | |
| 38 | + @InterceptorIgnore(tenantLine = "true") | |
| 39 | + List<DeptDO> selectListIgnoreTenant(); | |
| 37 | 40 | |
| 38 | 41 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/dict/DictDataMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.system.dal.mysql.dict; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; | |
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO; | |
| 5 | -import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataPageReqVO; | |
| 6 | -import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO; | |
| 7 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 9 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 10 | -import com.baomidou.mybatisplus.annotation.InterceptorIgnore; | |
| 6 | +import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO; | |
| 7 | +import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataPageReqVO; | |
| 8 | +import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO; | |
| 11 | 9 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 12 | 10 | import org.apache.ibatis.annotations.Mapper; |
| 11 | +import org.apache.ibatis.annotations.Select; | |
| 13 | 12 | |
| 14 | 13 | import java.util.Arrays; |
| 15 | 14 | import java.util.Collection; |
| ... | ... | @@ -47,10 +46,7 @@ public interface DictDataMapper extends BaseMapperX<DictDataDO> { |
| 47 | 46 | .eqIfPresent(DictDataDO::getStatus, reqVO.getStatus())); |
| 48 | 47 | } |
| 49 | 48 | |
| 50 | - @InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号 | |
| 51 | - default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) { | |
| 52 | - return selectOne(new LambdaQueryWrapper<DictDataDO>().select(DictDataDO::getId) | |
| 53 | - .gt(DictDataDO::getUpdateTime, maxUpdateTime).last(SqlConstants.LIMIT1)) != null; | |
| 54 | - } | |
| 49 | + @Select("SELECT id FROM system_dict_data WHERE update_time > #{maxUpdateTime} LIMIT 1") | |
| 50 | + Long selectExistsByUpdateTimeAfter(Date maxUpdateTime); | |
| 55 | 51 | |
| 56 | 52 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
| ... | ... | @@ -112,13 +112,13 @@ public class DeptServiceImpl implements DeptService { |
| 112 | 112 | if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据 |
| 113 | 113 | log.info("[loadMenuIfUpdate][首次加载全量部门]"); |
| 114 | 114 | } else { // 判断数据库中是否有更新的部门 |
| 115 | - if (!deptMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) { | |
| 115 | + if (deptMapper.selectExistsByUpdateTimeAfter(maxUpdateTime) != null) { | |
| 116 | 116 | return null; |
| 117 | 117 | } |
| 118 | 118 | log.info("[loadMenuIfUpdate][增量加载全量部门]"); |
| 119 | 119 | } |
| 120 | 120 | // 第二步,如果有更新,则从数据库加载所有部门 |
| 121 | - return deptMapper.selectList(); | |
| 121 | + return deptMapper.selectListIgnoreTenant(); | |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | @Override | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/dict/DictDataServiceImpl.java
| ... | ... | @@ -116,7 +116,7 @@ public class DictDataServiceImpl implements DictDataService { |
| 116 | 116 | if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据 |
| 117 | 117 | log.info("[loadDictDataIfUpdate][首次加载全量字典数据]"); |
| 118 | 118 | } else { // 判断数据库中是否有更新的字典数据 |
| 119 | - if (!dictDataMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) { | |
| 119 | + if (dictDataMapper.selectExistsByUpdateTimeAfter(maxUpdateTime) == null) { | |
| 120 | 120 | return null; |
| 121 | 121 | } |
| 122 | 122 | log.info("[loadDictDataIfUpdate][增量加载全量字典数据]"); | ... | ... |