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

相关推荐
程序员三明治26 分钟前
【Mybatis从入门到入土】ResultMap映射、多表查询与缓存机制全解析
java·sql·缓存·mybatis·resultmap·缓存机制·多表查询
此剑之势丶愈斩愈烈1 小时前
mybatis-plus分页插件使用
mybatis
六元七角八分12 小时前
pom.xml
xml·数据库
!if14 小时前
springboot mybatisplus 配置SQL日志,但是没有日志输出
spring boot·sql·mybatis
讓丄帝愛伱14 小时前
Mybatis Log Free插件使用
java·开发语言·mybatis
gaoshan1234567891015 小时前
‌MyBatis-Plus 的 LambdaQueryWrapper 可以实现 OR 条件查询‌
java·tomcat·mybatis
The best are water1 天前
jeesite mybatis添加拦截器,推送指定表的变更数据到其他数据库
数据库·mybatis
lunz_fly19921 天前
【源码解读之 Mybatis】【核心篇】-- 第6篇:StatementHandler语句处理器
mybatis
郑重其事,鹏程万里1 天前
commons-digester3(XML解析框架)
xml·java
lunzi_fly1 天前
【源码解读之 Mybatis】【核心篇】-- 第6篇:StatementHandler语句处理器
mybatis