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

相关推荐
只因在人海中多看了你一眼43 分钟前
B.40.3.2-MyBatis核心技术详解与性能优化
性能优化·mybatis
.又是新的一天.7 小时前
健身房预约系统SSM+Mybatis(五、预约展示)
前端·mybatis
北城以北88888 小时前
SSM--MyBatis框架之动态SQL
java·开发语言·数据库·sql·mybatis
计算机学姐11 小时前
基于SpringBoot的公务员考试管理系统【题库组卷+考试练习】
java·vue.js·spring boot·后端·java-ee·intellij-idea·mybatis
ejinxian13 小时前
protobuf 、JSON、XML传输格式
xml·json·protobuf
yue00821 小时前
C# XML文件的读写V2.0
xml·开发语言·c#
刘一说1 天前
深入解析 Spring Boot 数据访问:Spring Data JPA 与 MyBatis 集成实战
spring boot·tomcat·mybatis
赋能大师兄1 天前
MyBatis缓存机制
mybatis·二级缓存·一级缓存
codingPower2 天前
升级mybatis-plus导致项目启动报错: net.sf.jsqlparser.statement.select.SelectBody
java·spring boot·maven·mybatis
emma羊羊2 天前
【weblogic】XML反序列化漏洞
xml·安全