淺談mybatis mapper.xml文件中$和#的區(qū)別
#{}表示一個占位符即?,可以有效防止sql注入。在使用時不需要關(guān)心參數(shù)值的類型,mybatis會自動進(jìn)行java類型和jdbc類型的轉(zhuǎn)換。
#{}可以接收簡單類型值或pojo屬性值,如果傳入簡單類型值,#{}括號中可以是任意名稱。
<!-- 根據(jù)名稱模糊查詢用戶信息 -->
<select id="findUserById" 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 id="selectUserByName" parameterType="string" resultType="user">
select * from user where username like '%${value}%'
</select>
對于order by排序,使用#{}將無法實現(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 == @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 錯誤
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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java設(shè)計模式以虹貓藍(lán)兔的故事講解代理模式
代理模式是Java常見的設(shè)計模式之一。所謂代理模式是指客戶端并不直接調(diào)用實際的對象,而是通過調(diào)用代理,來間接的調(diào)用實際的對象2022-04-04
IDEA連接MySQL提示serverTimezone的問題及解決方法
很多朋友私聊小編,使用IDEA軟件連接MySQL數(shù)據(jù)庫時總是提示Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.的錯誤,小編就不一一回復(fù)大家了,下面小編把我的解決方法分享到腳本之家平臺,需要的朋友參考下吧2021-05-05
Springmvc處理ajax請求并返回json數(shù)據(jù)
這篇文章主要介紹了Springmvc處理ajax請求并返回json數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07

