一个解决三天的bug,到现在也没有搞明白,如题,到底redis和mysql、mybatisplus能有啥关系。三个不相关的嘛
记录下问题: (框架用的Jeecgboot 3.6.0) – 学习使用
看下面的一段代码,是一个分页查询:注意引用的redisTemplate
对象,它定义成立RedisTemplate
,没加任何泛型
controller:
@Autowired
private IDeviceRandomRecordService deviceRandomRecordService;
@Autowired
private RedisTemplate redisTemplate;
/**
* 分页列表查询
*
* @param deviceRandomRecord
* @param pageNo
* @param pageSize
* @param req
* @return
*/
@ApiOperation(value="设备随机数生成记录表-分页列表查询", notes="设备随机数生成记录表-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<DeviceRandomRecord>> queryPageList(DeviceRandomRecord deviceRandomRecord,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
Set<String> keys = redisTemplate.keys(DeviceCommonConstant.DEVICE_RANDOM_CODE + "*");// 查询所有在线设备id的key
List<String> codes = redisTemplate.opsForValue().multiGet(keys);// 查询所有在线设备的shakeCode
// .... 此处省略不必要的判断操作
Page<DeviceRandomRecord> page = new Page<DeviceRandomRecord>(pageNo, pageSize);
IPage<DeviceRandomRecord> pageList = deviceRandomRecordService.pageDeviceRandomRecord(page, deviceRandomRecord);
List<DeviceRandomRecord> list = pageList.getRecords();
if (ObjectUtils.isNotEmpty(list)) {
if (deviceRandomRecord.getIsOnline()) {
for (DeviceRandomRecord randomRecord : list) {
randomRecord.setIsOnline(true);// 在线
}
} else {
for (DeviceRandomRecord randomRecord : list) {
randomRecord.setIsOnline(false);// 离线
}
}
}
return Result.OK(pageList);
}
上述代码中,首先通过redis查询在线的设备的shakeCode列表,有了codes 信息后在通过mybatisplus插件去查询mysql数据库。我把mapper的xml文件也贴出来,如下:
<resultMap id="DeviceRandomRecordMap" type="org.jeecg.modules.device.entity.DeviceRandomRecord">
<result column="id" property="id" jdbcType="VARCHAR"/>
<result column="create_by" property="createBy" jdbcType="VARCHAR"/>
<!-- .... 省略字段 -->
</resultMap>
<select id="pageDeviceRandomRecord" resultMap="DeviceRandomRecordMap">
SELECT
DeviceRandomRecord.*, device.device_name as deviceName
from device_random_record as DeviceRandomRecord left join device on DeviceRandomRecord.device_id=device.device_code
where 1=1
<if test="query.deviceName!=null and query.deviceName!=''">
and device.device_name like CONCAT('%', #{query.deviceName}, '%')
</if>
<if test="query.isOnline!=null and query.isOnline==true">
and DeviceRandomRecord.shake_code in
<foreach item="code" collection="query.onlineCodes" open="(" separator="," close=")">
#{code}
</foreach>
</if>
<if test="query.isOnline!=null and query.isOnline==false">
and DeviceRandomRecord.shake_code not in
<foreach item="code" collection="query.offlineCodes" open="(" separator="," close=")">
#{code}
</foreach>
</if>
<!-- .... 省略其他查询条件的构建 -->
order by DeviceRandomRecord.create_time desc,DeviceRandomRecord.id
</select>
上面的过程看似标准的分页查询,正常可以得到结果:
好嘛,为了代码显得格式规范,我把代码格式化,将漏掉的泛型添加上去后,controller代码如下:
@Autowired
private IDeviceRandomRecordService deviceRandomRecordService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 分页列表查询
*
* @param deviceRandomRecord
* @param pageNo
* @param pageSize
* @param req
* @return
*/
@ApiOperation(value="设备随机数生成记录表-分页列表查询", notes="设备随机数生成记录表-分页列表查询")
@GetMapping(value = "/list")
public Result<IPage<DeviceRandomRecord>> queryPageList(DeviceRandomRecord deviceRandomRecord,
@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
HttpServletRequest req) {
Set<String> keys = redisTemplate.keys(DeviceCommonConstant.DEVICE_RANDOM_CODE + "*");// 查询所有在线设备id的key
List<String> codes = redisTemplate.opsForValue().multiGet(keys);// 查询所有在线设备的shakeCode
// .... 此处省略不必要的判断操作
Page<DeviceRandomRecord> page = new Page<DeviceRandomRecord>(pageNo, pageSize);
IPage<DeviceRandomRecord> pageList = deviceRandomRecordService.pageDeviceRandomRecord(page, deviceRandomRecord);
List<DeviceRandomRecord> list = pageList.getRecords();
if (ObjectUtils.isNotEmpty(list)) {
if (deviceRandomRecord.getIsOnline()) {
for (DeviceRandomRecord randomRecord : list) {
randomRecord.setIsOnline(true);// 在线
}
} else {
for (DeviceRandomRecord randomRecord : list) {
randomRecord.setIsOnline(false);// 离线
}
}
}
return Result.OK(pageList);
}
注意,只改动了这里
@Autowired
private RedisTemplate<String, String> redisTemplate;
将RedisTemplate添加了泛型,因为查询分页的方法里面所有的操作都是对字符串的,当然也尝试过
@Autowired
private StringRedisTemplate redisTemplate;
问题,就出现了,同样的条件,数据就查询不出来了:
简直离谱,毫无道理可言!
这个bug跟踪了三天没有理出个头绪来,redis、mysql、mybatisplus能有什么关系?框架中的东西就是简单的查询操作,redis中获取的数据只是一个查询的条件变量,简直离谱!!!
去掉泛型后,就有结果,加上后,mybatisplus只能打印出 count语句,并且count是有结果的,就是不执行select查询数据操作:
2024-03-27 22:13:17.362 [http-nio-8080-exec-2] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord_mpCount:137[0;39m - ==> Preparing: SELECT COUNT(*) AS total FROM device_random_record AS DeviceRandomRecord WHERE 1 = 1 AND DeviceRandomRecord.shake_code IN (?, ?)
2024-03-27 22:13:17.363 [http-nio-8080-exec-2] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord_mpCount:137[0;39m - ==> Parameters: "318812"(String), "320221"(String)
2024-03-27 22:13:17.366 [http-nio-8080-exec-2] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord_mpCount:137[0;39m - <== Total: 1
实际上mysql数据库中的数据都嘎嘎存在:
2024-03-27 22:22:59.451 [http-nio-8080-exec-1] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord_mpCount:137[0;39m - ==> Preparing: SELECT COUNT(*) AS total FROM device_random_record AS DeviceRandomRecord WHERE 1 = 1 AND DeviceRandomRecord.shake_code IN (?, ?)
2024-03-27 22:22:59.515 [http-nio-8080-exec-1] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord_mpCount:137[0;39m - ==> Parameters: 318812(String), 320221(String)
2024-03-27 22:22:59.530 [http-nio-8080-exec-1] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord_mpCount:137[0;39m - <== Total: 1
2024-03-27 22:22:59.536 [http-nio-8080-exec-1] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord:137[0;39m - ==> Preparing: SELECT DeviceRandomRecord.id, DeviceRandomRecord.create_by, DeviceRandomRecord.create_time, DeviceRandomRecord.update_by, DeviceRandomRecord.update_time, DeviceRandomRecord.sys_org_code, DeviceRandomRecord.shake_code, DeviceRandomRecord.device_id, DeviceRandomRecord.port, DeviceRandomRecord.dest_port, DeviceRandomRecord.cur_day, DeviceRandomRecord.ip_addr, DeviceRandomRecord.conn_duration, DeviceRandomRecord.offline_time, device.device_name AS deviceName FROM device_random_record AS DeviceRandomRecord LEFT JOIN device ON DeviceRandomRecord.device_id = device.device_code WHERE 1 = 1 AND DeviceRandomRecord.shake_code IN (?, ?) ORDER BY DeviceRandomRecord.create_time DESC, DeviceRandomRecord.id LIMIT ?
2024-03-27 22:22:59.544 [http-nio-8080-exec-1] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord:137[0;39m - ==> Parameters: 318812(String), 320221(String), 10(Long)
2024-03-27 22:22:59.551 [http-nio-8080-exec-1] [39mDEBUG[0;39m [36mo.j.m.d.m.D.pageDeviceRandomRecord:137[0;39m - <== Total: 2
到底是啥子原因,为啥RedisTemplate<String, String>
泛型能影响数据查询结果,无语~~搞不明白
来个大神解答下吧,一个问题研究3天了也没弄明白,愁人