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

相关推荐
弹简特2 小时前
【JavaEE】Mybatis实现分页查询功能
java·java-ee·mybatis
The Sheep 20235 小时前
C# 操作XML
xml·前端·c#
不吃香菜学java5 小时前
苍穹外卖-新增套餐
java·spring boot·spring·tomcat·maven·mybatis
有梦想的小何6 小时前
告别 N+1:MyBatis-Plus 批量同步优化套路(预加载、去重、批量落库)
mybatis
sxhcwgcy6 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
yuweiade6 小时前
Spring Boot 整合 MyBatis 与 PostgreSQL 实战指南
spring boot·postgresql·mybatis
弹简特7 小时前
【JavaEE】MybatisPlus速成
java·数据库·java-ee·mybatis
polaris06308 小时前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
MegaDataFlowers8 小时前
整合MyBatis
mybatis
tumeng07118 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis