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

相关推荐
一只大袋鼠18 小时前
MyBatis 入门详细实战教程(一):从环境搭建到查询运行
java·开发语言·数据库·mysql·mybatis
Full Stack Developme1 天前
MyBatis-Plus 流式查询教程
前端·python·mybatis
ccice011 天前
全面掌握Spring Boot + MyBatis + Maven + MySQL:从开发到部署的后端技术详解
spring boot·maven·mybatis
消失的旧时光-19431 天前
Spring Boot + MyBatis 从 0 到 1 跑通查询接口(含全部踩坑)
spring boot·后端·spring·mybatis
1.14(java)1 天前
MyBatis 操作数据库
数据库·mybatis
eSsO KERF1 天前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback
麻辣璐璐2 天前
EditText属性运用之适配RTL语言和LTR语言的输入习惯
android·xml·java·开发语言·安卓
杰克尼2 天前
天机学堂项目总结(day1~day2)
大数据·jvm·spring·elasticsearch·搜索引擎·spring cloud·mybatis
Java成神之路-2 天前
SpringBoot 整合 SSM 全流程详解(含 JUnit+MyBatis 实战)(Spring系列18)
spring boot·junit·mybatis
时间静止不是简史2 天前
当MyBatis-Plus的like遇上SQL通配符:下划线翻车记
java·sql·mybatis