淺談mybatis mapper.xml文件中$和#的區(qū)別
#{}表示一個占位符即?,可以有效防止sql注入。在使用時不需要關(guān)心參數(shù)值的類型,mybatis會自動進(jìn)行java類型和jdbc類型的轉(zhuǎn)換。
#{}可以接收簡單類型值或pojo屬性值,如果傳入簡單類型值,#{}括號中可以是任意名稱。
<!-- 根據(jù)名稱模糊查詢用戶信息 --> <select parameterType='String' resultType='user'> select * from user where username like CONCAT(CONCAT(’%’, #{name}), ’%’) </select>
${}可以將parameterType 傳入的內(nèi)容拼接在sql中且不進(jìn)行jdbc類型轉(zhuǎn)換。
${}可以接收簡單類型值或pojo屬性值,如果傳入簡單類型值,${}括號中名稱只能是value。
<!-- 根據(jù)名稱模糊查詢用戶信息 --> <select parameterType='string' resultType='user'> select * from user where username like ’%${value}%’ </select>
對于order by排序,使用#{}將無法實(shí)現(xiàn)功能,應(yīng)該寫成如下形式:
ORDER BY ${columnName}
另外,對于mybatis逆向工程生成的代碼中,進(jìn)行模糊查詢調(diào)用andXxxLike()方法時,需要手動加%,如下:
CustomerExample customerExample=new CustomerExample();
customerExample.or().andNameLike('%'+name+'%');
補(bǔ)充知識:Mybatis條件if test使用枚舉值
1 正確
package com.weather.weatherexpert.common.utils; /** * <p>Title: </p> * <p>Description: </p> * * @Author * @CreateTime */public enum City { XINZHOU(100002,'忻州'), DATONG(100003,'大同'), TAIYUAN(100001,'太原'); private final Integer code; private final String name; City(Integer value, String desc) { this.code = value; this.name = desc; } public Integer getCode() { return code; } public String getName() { return name; }}
xml:
<!--<if test='cityName == @com.weather.weatherexpert.common.utils.City.XINZHOU@getName'><!–wrong,java.lang.ClassNotFoundException: Unable to resolve class: com.weather.weatherexpert.common.utils.City.XINZHOU–>--><!--<if test='cityName == @com.weather.weatherexpert.common.utils.City@XINZHOU@getName'><!–wrong,[org.apache.ibatis.ognl.ParseException: Encountered ' '@' '@ '' at line 1, column 65.–>--><if test='cityName == @[email protected]'><!--right--> area_table</if> where 1=1<if test='cityName == @[email protected]'><!--right--> and city_name=#{cityName}</if>
2 錯誤
package com.weather.weatherexpert.common.utils; /** * <p>Title: </p> * <p>Description: </p> * * @Author * @CreateTime */public class CityClass { public static enum CityEnum { XINZHOU(100002, '忻州'), DATONG(100003, '大同'), TAIYUAN(100001, '太原'); private final Integer code; private final String name; CityEnum(Integer value, String desc) { this.code = value; this.name = desc; } public Integer getCode() { return code; } public String getName() { return name; } }}
xml:
/* Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression ’cityName == @com.weather.weatherexpert.common.utils.CityClass@CityEnum.XINZHOU.getName’. Cause: org.apache.ibatis.ognl.OgnlException: Could not get static field CityEnum from class com.weather.weatherexpert.common.utils.CityClass [java.lang.NoSuchFieldException: CityEnum]*/ <if test='cityName == @com.weather.weatherexpert.common.utils.CityClass@CityEnum.XINZHOU.getName'><!--wrong--> area_table </if>
可見,直接定義的枚舉類可以正常使用,在類中定義的枚舉類這樣使用會報錯,可能方法還沒有找到。
以上這篇淺談mybatis mapper.xml文件中$和#的區(qū)別就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. mysql 數(shù)據(jù)庫優(yōu)化技巧2. mysql的like模式3. mysql啟動時報錯 ERROR! Manager of pid-file quit without4. Mysql入門系列:對MYSQL查詢中有疑問的數(shù)據(jù)進(jìn)行編碼5. 解決Mybatis中mapper.xml文件update,delete及insert返回值問題6. 流行的oracle恢復(fù)工具7. 什么是Access數(shù)據(jù)庫8. SQL Server事務(wù)日志意外增大的處理方法9. MySQL創(chuàng)始人發(fā)郵件尋求中國幫助10. 詳解MySQL批量入庫的幾種方式
