淺談mybatis mapper.xml文件中$和#的區(qū)別
#{}表示一個(gè)占位符即?,可以有效防止sql注入。在使用時(shí)不需要關(guān)心參數(shù)值的類(lèi)型,mybatis會(huì)自動(dòng)進(jìn)行java類(lèi)型和jdbc類(lèi)型的轉(zhuǎn)換。
#{}可以接收簡(jiǎn)單類(lèi)型值或pojo屬性值,如果傳入簡(jiǎn)單類(lèi)型值,#{}括號(hào)中可以是任意名稱。
<!-- 根據(jù)名稱模糊查詢用戶信息 --> <select parameterType='String' resultType='user'> select * from user where username like CONCAT(CONCAT(’%’, #{name}), ’%’) </select>
${}可以將parameterType 傳入的內(nèi)容拼接在sql中且不進(jìn)行jdbc類(lèi)型轉(zhuǎn)換。
${}可以接收簡(jiǎn)單類(lèi)型值或pojo屬性值,如果傳入簡(jiǎn)單類(lèi)型值,${}括號(hào)中名稱只能是value。
<!-- 根據(jù)名稱模糊查詢用戶信息 --> <select parameterType='string' resultType='user'> select * from user where username like ’%${value}%’ </select>
對(duì)于order by排序,使用#{}將無(wú)法實(shí)現(xiàn)功能,應(yīng)該寫(xiě)成如下形式:
ORDER BY ${columnName}
另外,對(duì)于mybatis逆向工程生成的代碼中,進(jìn)行模糊查詢調(diào)用andXxxLike()方法時(shí),需要手動(dòng)加%,如下:
CustomerExample customerExample=new CustomerExample();
customerExample.or().andNameLike('%'+name+'%');
補(bǔ)充知識(shí):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 == @com.weather.weatherexpert.common.utils.City@XINZHOU.getName'><!--right--> area_table</if> where 1=1<if test='cityName == @com.weather.weatherexpert.common.utils.City@XINZHOU.getName'><!--right--> and city_name=#{cityName}</if>
2 錯(cuò)誤
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>
可見(jiàn),直接定義的枚舉類(lèi)可以正常使用,在類(lèi)中定義的枚舉類(lèi)這樣使用會(huì)報(bào)錯(cuò),可能方法還沒(méi)有找到。
以上這篇淺談mybatis mapper.xml文件中$和#的區(qū)別就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Mysql命令行連接遠(yuǎn)程/本地?cái)?shù)據(jù)庫(kù)詳解2. Navicat連接Oracle數(shù)據(jù)庫(kù)的詳細(xì)步驟與注意事項(xiàng)3. mybatis 如何判斷l(xiāng)ist集合是否包含指定數(shù)據(jù)4. MySQL找出未提交事務(wù)的SQL實(shí)例淺析5. 如何利用MySQL查詢varbinary中存儲(chǔ)的數(shù)據(jù)6. SQL Server開(kāi)發(fā)智能提示插件SQL Prompt介紹7. SQL Server跨服務(wù)器操作數(shù)據(jù)庫(kù)的圖文方法(LinkedServer)8. 一文教會(huì)你配置使用Navicat或PLSQL可視化工具遠(yuǎn)程連接Oracle9. 淺談MySQL之select優(yōu)化方案10. SQL Server中T-SQL標(biāo)識(shí)符介紹與無(wú)排序生成序號(hào)的方法
