Mybatis xml中排序(order by)条件用#{}查询失败

问题描述:

处理简单分页时,发现从外部传入的排序条件无法生效,但程序无报错,正常返回列表,只是排序条件不对;

原因:

#{}表示一个占位符,当#{}传入的数据是一个字符串时,会自动将传入的数据加一个双引号。

解决方法:

使用${}将传入的数据直接显示生成在sql中;

1、当查询语句使用#{},例如传入"update_date desc"排序条件,生成语句如下

复制代码
select id, title, name, status, create_by,create_date,update_by,update_date from table 
WHERE status = 1 order by "update_date desc"

2、使用${}生成语句是:

复制代码
select id, title, name, status, create_by,create_date,update_by,update_date from table 
WHERE status = 1 order by update_date desc

3、推荐文章

Mybatis中${}和#{}的区别:https://blog.csdn.net/BBQ__ZXB/article/details/127089187

相关推荐
用户8307196840822 小时前
秒杀面试!MyBatis-Spring-Boot 初始化流程深度拆解
spring boot·mybatis
chilavert3184 小时前
技术演进中的开发沉思-304计算机原理:XML
xml·计算机原理
8***f3954 小时前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
w***76554 小时前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
张较瘦_17 小时前
SpringBoot3 | MyBatis-Plus 搞定宠物管理:从0到1实现增删改查
mybatis·宠物
沙白猿1 天前
Redis报错:A bean with that name has already been defined in class path resource
spring boot·redis·mybatis
小马爱打代码1 天前
MyBatis设计模式:构建者、工厂、代理模式
设计模式·mybatis·代理模式
程序猿零零漆2 天前
Spring之旅 - 记录学习 Spring 框架的过程和经验(十一)基于XML方式、注解的声明式事务控制、Spring整合Web环境
xml·学习·spring
柒.梧.2 天前
SSM常见核心面试问题深度解析
java·spring·面试·职场和发展·mybatis