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 小时前
动态拼接SQL实践备忘📝
java·sql·mybatis
galaxyffang8 小时前
skywalking整合logback.xml日志,日志文件出现乱码问题解决
xml·logback·skywalking
朝新_11 小时前
【实战】博客系统:项目公共模块 + 博客列表的实现
数据库·笔记·sql·mybatis·交互·javaee
小小哭包13 小时前
Spring Boot整合多个MyBatis数据源实战教程
spring boot·后端·mybatis
b***9101 天前
【SpringBoot3】Spring Boot 3.0 集成 Mybatis Plus
android·前端·后端·mybatis
小杍随笔1 天前
【基于 Spring Boot 3 + Spring Security + MyBatis-Plus 构建的用户登录功能】
spring boot·spring·mybatis
百***26631 天前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback
864记忆1 天前
Qt 对 JSON和XML文件的操作详解
xml·qt·json
♡喜欢做梦2 天前
MyBatis操作数据库(入门)
java·数据库·mybatis
ArabySide2 天前
【Spring Boot】基于MyBatis的条件分页
java·spring boot·后端·mybatis