一、ORDER BY 排序失效
如果傳遞給 mapper 的參數(shù)值是以 #{test_參數(shù)}
的形式,那么就會(huì)報(bào)錯(cuò)
具體如下:
傳遞參數(shù)是 name
排序規(guī)則是升序 asc
package com.ruoyi.web.mapper;
public interface TestMapper {
List<TestEntity> getTestData( @Param("testcolumn") String testColumn, @Param("rule") String rule);
}
<mapper namespace="com.test.mapper.TestMapper">
<select id="getTestData" resultType="com.test.entity.TestEntity">
SELECT * FROM test_table ORDER BY #{testcolumn} #{rule}
</select>
</mapper>
ORDER BY
后 使用 #{ }
獲取參數(shù)值,運(yùn)行后,會(huì)報(bào)錯(cuò)的,必須改成 ${ }
,井號(hào)改成 美元符號(hào)。
如下所示:
<mapper namespace="com.test.mapper.TestMapper">
<select id="getTestData" resultType="com.test.entity.TestEntity">
SELECT * FROM test_table ORDER BY ${testcolumn} ${rule}
</select>
</mapper>
二、ORDER BY 與 CASE WHEN THEN 排序問(wèn)題
數(shù)據(jù)庫(kù)表 test_table
的真實(shí)字段名: test_id
測(cè)試參數(shù)值:
testcolumn
參數(shù)賦予內(nèi)容是 testId
rule
排序規(guī)則是升序 asc
package com.ruoyi.web.mapper;
public interface TestMapper {
List<TestEntity> getTestData( @Param("testcolumn") String testColumn, @Param("rule") String rule);
}
<mapper namespace="com.test.mapper.TestMapper">
<select id="getTestData" resultType="com.test.entity.TestEntity">
SELECT * FROM test_table ORDER BY
CASE
WHEN "${testcolumn}" = 'testId'
THEN test_id
END ${rule}
</select>
</mapper>
注意:
${ } 一定要被雙引號(hào)包含,否則不會(huì)進(jìn)行一個(gè)字符串匹配,即和 'testId'
進(jìn)行對(duì)比,會(huì)報(bào)錯(cuò)的,然后返回值的就不需要加雙引號(hào)或單引號(hào)了, 直接就是數(shù)據(jù)庫(kù)表 test_table
的字段名
參考鏈接
1. Mybatis實(shí)現(xiàn) 動(dòng)態(tài)排序
2. MyBatis排序時(shí)使用order by 動(dòng)態(tài)參數(shù)時(shí)需要注意,用$而不是#
3. Mybatis Order By動(dòng)態(tài)參數(shù)詳解
4. mybatis中orderBy(排序字段)和sort(排序方式)引起的bug
5. Mybatis動(dòng)態(tài)字段排序防注入-簡(jiǎn)單粗暴上代碼的方式
6. Mybatis自定義排序詳解CASE WHEN
7. Mybatis CASE WHEN 的用法
8. Mybatis中case when 配合 trim的使用方法
9. MYBATIS中CASE WHEN的使用
10. Mybatis CASE WHEN 的用法
11. mysql 某列指定值靠前排序:order by 高級(jí)用法之case when文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-667466.html
12. MyBatis 排序時(shí)使用 order by 動(dòng)態(tài)參數(shù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-667466.html
到了這里,關(guān)于Mybatis ORDER BY 排序失效 & ORDER BY 與 CASE WHEN THEN 排序問(wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!