淺談mybatis mapper.xml文件中$和#的區(qū)別
#{}表示一個占位符即?,可以有效防止sql注入。在使用時不需要關心參數值的類型,mybatis會自動進行java類型和jdbc類型的轉換。
#{}可以接收簡單類型值或pojo屬性值,如果傳入簡單類型值,#{}括號中可以是任意名稱。
<!-- 根據名稱模糊查詢用戶信息 -->
<select id="findUserById" parameterType="String" resultType="user">
select * from user where username like CONCAT(CONCAT('%', #{name}), '%')
</select>
${}可以將parameterType 傳入的內容拼接在sql中且不進行jdbc類型轉換。
${}可以接收簡單類型值或pojo屬性值,如果傳入簡單類型值,${}括號中名稱只能是value。
<!-- 根據名稱模糊查詢用戶信息 -->
<select id="selectUserByName" parameterType="string" resultType="user">
select * from user where username like '%${value}%'
</select>
對于order by排序,使用#{}將無法實現功能,應該寫成如下形式:
ORDER BY ${columnName}
另外,對于mybatis逆向工程生成的代碼中,進行模糊查詢調用andXxxLike()方法時,需要手動加%,如下:
CustomerExample customerExample=new CustomerExample();
customerExample.or().andNameLike("%"+name+"%");
補充知識: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ū)別就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java ScheduledExecutorService的具體使用
ScheduledExecutorService有線程池的特性,也可以實現任務循環(huán)執(zhí)行,本文主要介紹了Java ScheduledExecutorService的具體使用,具有一定的參考價值,感興趣的可以了解一下2023-05-05
Java commons-httpclient如果實現get及post請求
這篇文章主要介紹了Java commons-httpclient如果實現get及post請求,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09

