淺談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)中可以是任意名稱(chēng)。
<!-- 根據(jù)名稱(chēng)模糊查詢(xún)用戶(hù)信息 -->
<select id="findUserById" 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)中名稱(chēng)只能是value。
<!-- 根據(jù)名稱(chēng)模糊查詢(xún)用戶(hù)信息 -->
<select id="selectUserByName" 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)行模糊查詢(xú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è)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解代理模式
代理模式是Java常見(jiàn)的設(shè)計(jì)模式之一。所謂代理模式是指客戶(hù)端并不直接調(diào)用實(shí)際的對(duì)象,而是通過(guò)調(diào)用代理,來(lái)間接的調(diào)用實(shí)際的對(duì)象2022-04-04
IDEA連接MySQL提示serverTimezone的問(wèn)題及解決方法
很多朋友私聊小編,使用IDEA軟件連接MySQL數(shù)據(jù)庫(kù)時(shí)總是提示Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.的錯(cuò)誤,小編就不一一回復(fù)大家了,下面小編把我的解決方法分享到腳本之家平臺(tái),需要的朋友參考下吧2021-05-05
Springmvc處理ajax請(qǐng)求并返回json數(shù)據(jù)
這篇文章主要介紹了Springmvc處理ajax請(qǐng)求并返回json數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

