Skip to content

Commit

Permalink
解决 Spring 定时任务下,无租户编号的问题,导致查询报错
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Feb 6, 2022
1 parent 251bd08 commit 0bac283
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cn.iocoder.yudao.module.system.dal.mysql.dept;

import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
Expand All @@ -29,10 +29,13 @@ default Integer selectCountByParentId(Long parentId) {
return selectCount(DeptDO::getParentId, parentId);
}

@Select("SELECT id FROM system_dept WHERE update_time > #{maxUpdateTime} LIMIT 1")
@InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号
default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
return selectOne(new LambdaQueryWrapper<DeptDO>().select(DeptDO::getId)
.gt(DeptDO::getUpdateTime, maxUpdateTime).last(SqlConstants.LIMIT1)) != null;
}
Long selectExistsByUpdateTimeAfter(Date maxUpdateTime);

// TODO 芋艿:后续想想,有没可能优化下。大体思路,是支持某个方法,忽略租户
@Select("SELECT * FROM system_dept")
@InterceptorIgnore(tenantLine = "true")
List<DeptDO> selectListIgnoreTenant();

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package cn.iocoder.yudao.module.system.dal.mysql.dict;

import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -47,10 +46,7 @@ default List<DictDataDO> selectList(DictDataExportReqVO reqVO) {
.eqIfPresent(DictDataDO::getStatus, reqVO.getStatus()));
}

@InterceptorIgnore(tenantLine = "true") // 该方法忽略多租户。原因:该方法被异步 task 调用,此时获取不到租户编号
default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
return selectOne(new LambdaQueryWrapper<DictDataDO>().select(DictDataDO::getId)
.gt(DictDataDO::getUpdateTime, maxUpdateTime).last(SqlConstants.LIMIT1)) != null;
}
@Select("SELECT id FROM system_dict_data WHERE update_time > #{maxUpdateTime} LIMIT 1")
Long selectExistsByUpdateTimeAfter(Date maxUpdateTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ private List<DeptDO> loadDeptIfUpdate(Date maxUpdateTime) {
if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据
log.info("[loadMenuIfUpdate][首次加载全量部门]");
} else { // 判断数据库中是否有更新的部门
if (!deptMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) {
if (deptMapper.selectExistsByUpdateTimeAfter(maxUpdateTime) != null) {
return null;
}
log.info("[loadMenuIfUpdate][增量加载全量部门]");
}
// 第二步,如果有更新,则从数据库加载所有部门
return deptMapper.selectList();
return deptMapper.selectListIgnoreTenant();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private List<DictDataDO> loadDictDataIfUpdate(Date maxUpdateTime) {
if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据
log.info("[loadDictDataIfUpdate][首次加载全量字典数据]");
} else { // 判断数据库中是否有更新的字典数据
if (!dictDataMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) {
if (dictDataMapper.selectExistsByUpdateTimeAfter(maxUpdateTime) == null) {
return null;
}
log.info("[loadDictDataIfUpdate][增量加载全量字典数据]");
Expand Down

0 comments on commit 0bac283

Please sign in to comment.