分享一下项目中遇到的排序失效问题

今天把原来的一个查询接口的业务代码进行了优化,减少了十几行冗余的代码。

原来的代码

ChongwuServiceImpl.java

/*** @author heyunlin* @version 1.0*/
@Slf4j
@Service
public class ChongwuServiceImpl implements ChongwuService {@Overridepublic JsonResult<JsonPage<Chongwu>> selectByPage(ChongwuPager pager) {List<Integer> skillIds = pager.getSkillIds();if (CollectionUtils.isNotEmpty(skillIds)) {int size = skillIds.size();// 得到order by语句String statement = Pager.getOrderByStatement(pager);List<Chongwu> rows = chongwuMapper.selectBySkills(pager, skillIds, size, statement);long total = chongwuMapper.selectCountBySkills(pager, skillIds, size);return JsonResult.restPage(total, rows);} else {Page<Chongwu> page = new Page<>(pager.getPage(), pager.getRows());QueryWrapper<Chongwu> wrapper = new QueryWrapper<>();wrapper.eq(pager.getCategoryId() != null,"category_id", pager.getCategoryId());wrapper.eq(StringUtils.isNotEmpty(pager.getRoleId()),"role_id", pager.getRoleId());wrapper.eq(StringUtils.isNotEmpty(pager.getZuoqiId()),"zuoqi_id", pager.getZuoqiId());// 得到order by语句String statement = Pager.getOrderByStatement(pager);wrapper.last(statement);Page<Chongwu> result = chongwuMapper.selectPage(page, wrapper);return JsonResult.restPage(result);}}}

ChongwuMapper.java

@Repository
public interface ChongwuMapper extends BaseMapper<Chongwu> {/*** 查询已学习指定技能的宠物数量* @param pager 分页参数* @param skillIds 宠物技能类型id列表* @param total 总技能数* @return int*/long selectCountBySkills(@Param("pager") ChongwuPager pager,@Param("skillIds") List<Integer> skillIds,@Param("total") int total);/*** 查询已学习指定技能的宠物* @param pager 分页参数* @param skillIds 宠物技能类型id列表* @param total 总技能数* @param statement order by后面的语句* @return List<Chongwu>*/List<Chongwu> selectBySkills(@Param("pager") ChongwuPager pager,@Param("skillIds") List<Integer> skillIds,@Param("total") int total,@Param("statement") String statement);
}

ChongwuMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuMapper"><resultMap id="resultMap" type="cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu"><result column = "id" property = "id" /><result column = "name" property = "name" /><result column = "type" property = "type" /><result column = "grade" property = "grade" /><result column = "score" property = "score" /><result column = "role_id" property = "roleId" /><result column = "zuoqi_id" property = "zuoqiId" /><result column = "zizhi_id" property = "zizhiId" /><result column = "lifespan" property = "lifespan" /><result column = "ty_status" property = "tyStatus" /><result column = "category_id" property = "categoryId" /><result column = "attribute_id" property = "attributeId" /></resultMap><select id="selectCountBySkills" resultType="long">select count(*) from chongwu where id in (select cs.chongwu_id from (select chongwu_id from chongwu_skill where skill_id in (<foreach item='skillId' collection='skillIds' separator=','>#{skillId}</foreach>)) as csgroup by cs.chongwu_idhaving count(cs.chongwu_id) >= #{total})<if test='pager.zuoqiId != null and pager.zuoqiId != ""'>and zuoqi_id = #{pager.zuoqiId}</if><if test='pager.roleId != null and pager.roleId != ""'>and role_id = #{pager.roleId}</if><if test='pager.categoryId != null'>and category_id = #{pager.categoryId}</if></select><select id="selectBySkills" resultMap="resultMap">select * from chongwu where id in (select cs.chongwu_id from (select chongwu_id from chongwu_skill where skill_id in (<foreach item='skillId' collection='skillIds' separator=','>#{skillId}</foreach>)) as csgroup by cs.chongwu_idhaving count(cs.chongwu_id) >= #{total})<if test='pager.zuoqiId != null and pager.zuoqiId != ""'>and zuoqi_id = #{pager.zuoqiId}</if><if test='pager.roleId != null and pager.roleId != ""'>and role_id = #{pager.roleId}</if><if test='pager.categoryId != null'>and category_id = #{pager.categoryId}</if>order by role_id, #{statement}</select>
</mapper>

重构后的代码

ChongwuServiceImpl.java

/*** @author heyunlin* @version 1.0*/
@Slf4j
@Service
public class ChongwuServiceImpl implements ChongwuService {@Overridepublic Page<Chongwu> selectByPage(ChongwuPager pager) {List<Integer> skillIds = pager.getSkillIds();pager.setTotal(skillIds != null ? skillIds.size() : 0);pager.setStatement(Pager.getStatement(pager));Page<Chongwu> page = Pager.ofPage(pager);return chongwuMapper.selectPage(page, pager);}}

ChongwuMapper.java

@Repository
public interface ChongwuMapper extends BaseMapper<Chongwu> {/*** 分页查询宠物列表* @param page 分页参数* @param pager 查询条件* @return List<Chongwu>*/Page<Chongwu> selectPage(Page<Chongwu> page, ChongwuPager pager);
}

ChongwuMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuMapper"><resultMap id="resultMap" type="cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu"><result column = "role_id" property = "roleId" /><result column = "zuoqi_id" property = "zuoqiId" /><result column = "zizhi_id" property = "zizhiId" /><result column = "ty_status" property = "tyStatus" /><result column = "category_id" property = "categoryId" /><result column = "attribute_id" property = "attributeId" /></resultMap><select id="selectPage" resultMap="resultMap">select * from chongwu<where>1 = 1<if test='pager.total > 0'>and id in (select cwjnb.chongwu_id from (select chongwu_id from chongwu_skill where skill_id in (<foreach item="skillId" collection="pager.skillIds" separator=",">#{skillId}</foreach>)) as cwjnbgroup by cwjnb.chongwu_idhaving count(cwjnb.chongwu_id) >= #{pager.total})</if><if test="pager.zuoqiId != null and pager.zuoqiId != ''">and zuoqi_id = #{pager.zuoqiId}</if><if test="pager.roleId != null and pager.roleId != ''">and role_id = #{pager.roleId}</if><if test='pager.categoryId != null'>and category_id = #{pager.categoryId}</if></where><if test="pager.statement != null and pager.statement != ''">order by #{pager.statement}</if></select>
</mapper>

排序失效问题

修改后引发了前端排序失效问题,点击排序图标触发了重新渲染表格数据的ajax请求,而且控制台打印的SQL语句也没有问题,直接复制到数据库中执行,能查出排序后的数据。

前端排序失效截图

控制台打印的SQL查询语句

直接复制到Navicat中执行,查询到了正确的结果(score列按升序排序)

问题原因分析

遇到这个问题其实仔细一想非常简单,就是${}和#{}的区别,把#{}改为${}即可。

这是把#{}改成${}之后,在控制台的SQL语句,两者有一定的区别:

  • 上图的SQL语句:select * from chongwu WHERE 1 = 1 order by role_id , "score desc" LIMIT 10
  • 下图的SQL语句:select * from chongwu WHERE 1 = 1 order by role_id , score desc LIMIT 10

纠正后的代码

ChongwuMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuMapper"><resultMap id="resultMap" type="cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu"><result column = "id" property = "id" /><result column = "name" property = "name" /><result column = "type" property = "type" /><result column = "grade" property = "grade" /><result column = "score" property = "score" /><result column = "role_id" property = "roleId" /><result column = "zuoqi_id" property = "zuoqiId" /><result column = "zizhi_id" property = "zizhiId" /><result column = "lifespan" property = "lifespan" /><result column = "ty_status" property = "tyStatus" /><result column = "category_id" property = "categoryId" /><result column = "attribute_id" property = "attributeId" /></resultMap><select id="selectPage" resultMap="resultMap">select * from chongwu<where>1 = 1<if test='pager.total > 0'>and id in (select cwjnb.chongwu_id from (select chongwu_id from chongwu_skill where skill_id in (<foreach item="skillId" collection="pager.skillIds" separator=",">#{skillId}</foreach>)) as cwjnbgroup by cwjnb.chongwu_idhaving count(cwjnb.chongwu_id) >= #{pager.total})</if><if test="pager.zuoqiId != null and pager.zuoqiId != ''">and zuoqi_id = #{pager.zuoqiId}</if><if test="pager.roleId != null and pager.roleId != ''">and role_id = #{pager.roleId}</if><if test='pager.categoryId != null'>and category_id = #{pager.categoryId}</if></where><if test="pager.statement != null and pager.statement != ''">order by ${pager.statement}</if></select>
</mapper>

重新启动项目之后,问题得以解决,不知道有没有大佬遇到类似的问题,欢迎评论区留言分享~

好了,文章就分享到这里了,看完不要忘了点赞+收藏哦~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/615784.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

中介者模式:简化对象间通信的协调者

在面向对象的软件开发中&#xff0c;中介者模式是一种重要的行为型设计模式&#xff0c;用于降低多个对象间通信的复杂性。通过提供一个中心化的对象来处理不同组件之间的交互&#xff0c;中介者模式使得组件间不必显式引用彼此&#xff0c;从而使其松散耦合、更易于维护。本文…

数据库系统概论(超详解!!!)第四节 数据库安全性

问题的提出&#xff1a; 数据库的一大特点是数据可以共享 数据共享必然带来数据库的安全性问题 数据库系统中的数据共享不能是无条件的共享。 1.数据库的安全概述 数据库的安全性是指保护数据库以防止不合法使用所造成的数据泄露、更改或破坏 。 系统安全保护措施是否有效…

【电控笔记6】电流回路+延迟效应

问题提出 数字控制系统的delay: 5.4节有介绍T0=0.5TS 低通滤波器的时间常数? 可用示例程序 m2 2 1b 如下图画出开环系统的伯德图进行比较,如图 2-2-4 所示,由于延迟组件会侵蚀系统的相位,因此从图可以看出,加入延迟效应后,q轴电流回路的相位裕度(Phase Margin) 从…

【python】python饮料销售数据分析可视化(源码+数据集)【独一无二】

&#x1f449;博__主&#x1f448;&#xff1a;米码收割机 &#x1f449;技__能&#x1f448;&#xff1a;C/Python语言 &#x1f449;公众号&#x1f448;&#xff1a;测试开发自动化【获取源码商业合作】 &#x1f449;荣__誉&#x1f448;&#xff1a;阿里云博客专家博主、5…

7/8电源连接器航空插头端子

概述 7/8电源连接器是一种工业电源连接器的规格型号之一&#xff0c;常见于工业领域的电力传输和连接应用。它的名称中的“7/8”代表连接器插头的直径尺寸&#xff0c;通常为7/8英寸。这种类型的连接器通常用于较大电流传输和较高功率设备的连接&#xff0c;具有较大的电流承载…

JS控制元素平滑滚动,页面自动滚动锚点实现

使用 scrollIntoView 实现元素内子元素的平滑滚动&#xff0c; 下面是模拟接口list返回&#xff0c;然后通过按钮切换下一个&#xff0c;页面就会滚动到响应的位置 具体 scrollIntoView 有一些其他参数来配置滚动的具体交换&#xff0c;网上去查即可 备注&#xff1a;下面的代码…

实时智能应答3D数字人搭建2

先看效果&#xff1a; 3d数字人讲黑洞 根据艾媒咨询数据&#xff0c;2021年&#xff0c;中国虚拟人核心产业规模达到62.2亿元&#xff0c;带动市场规模达到1074.9亿元&#xff1b;2025年&#xff0c;这一数据预计将达到480.6亿元与6402.7亿元&#xff0c;同比增长迅猛。数字人可…

什么是线程的上下文切换?

我们知道使用多线程的目的是为了充分利用多核CPU&#xff0c;比如说我们是16核&#xff0c;但是当创建很多线程比如说160个&#xff0c;CPU不够用了&#xff0c;此时就是一个CPU来应付多个线程&#xff08;这里我们是一个CPU应对10个线程&#xff09;。这个时候&#xff0c;操作…

无人机巡检技术革命性变革光伏电站运维管理

在中国广袤的大地上&#xff0c;光伏电站如雨后春笋般崛起&#xff0c;晶体硅组件板在阳光下熠熠生辉&#xff0c;为人们带来了源源不断的绿色能源。然而&#xff0c;随着光伏产业的迅猛发展&#xff0c;电站运维管理面临着前所未有的挑战。而无人机巡检技术的引入&#xff0c;…

Oracle获取对象的DDL创建语句

1.命令行方式&#xff08;如&#xff1a;sqlplus&#xff09; ## 用户 select dbms_metadata.get_ddl(USER,TEST) from dual;## 表 select dbms_metadata.get_ddl(TABLE,TEST,T1) from dual;## 表空间 select dbms_metadata.get_ddl(TABLESPACE,TBS_NAME) from dual;## 索引 s…

cocos creator 实现spine局部换装

1 使用3.7.4版本 2 js代码 3 c Native层修改源码

vscode ssh远程服务器并通过代码程序以及terminal启动GUI

写在前面 之前在做带有GUI界面的程序一般都在MobaXterm类似得应用程序中实现&#xff0c;因为自带X Server,但是现在在代码中遇到Bug&#xff0c;需要在vscode中断点调试&#xff0c;但vscode不自带X server,导致没有到问题出就被卡在GUI这一步&#xff0c;这就带来了问题&…