淺談mybatis mapper.xml文件中$和#的區(qū)別
#{}表示一個(gè)占位符即?,可以有效防止sql注入。在使用時(shí)不需要關(guān)心參數(shù)值的類型,mybatis會(huì)自動(dòng)進(jìn)行java類型和jdbc類型的轉(zhuǎn)換。
#{}可以接收簡(jiǎn)單類型值或pojo屬性值,如果傳入簡(jiǎn)單類型值,#{}括號(hào)中可以是任意名稱。
<!-- 根據(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)換。
${}可以接收簡(jiǎn)單類型值或pojo屬性值,如果傳入簡(jiǎn)單類型值,${}括號(hào)中名稱只能是value。
<!-- 根據(jù)名稱模糊查詢用戶信息 -->
<select id="selectUserByName" parameterType="string" resultType="user">
select * from user where username like '%${value}%'
</select>
對(duì)于order by排序,使用#{}將無(wú)法實(shí)現(xiàn)功能,應(yīng)該寫成如下形式:
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>
可見,直接定義的枚舉類可以正常使用,在類中定義的枚舉類這樣使用會(huì)報(bào)錯(cuò),可能方法還沒有找到。
以上這篇淺談mybatis mapper.xml文件中$和#的區(qū)別就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java ScheduledExecutorService的具體使用
ScheduledExecutorService有線程池的特性,也可以實(shí)現(xiàn)任務(wù)循環(huán)執(zhí)行,本文主要介紹了Java ScheduledExecutorService的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05
DoytoQuery中關(guān)于N+1查詢問(wèn)題解決方案詳解
這篇文章主要為大家介紹了DoytoQuery中關(guān)于N+1查詢問(wèn)題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
springboot3.x中Jakarta包無(wú)法引入的問(wèn)題
最近想將一些項(xiàng)目升級(jí)到springboot3.x和java17的時(shí)候,發(fā)現(xiàn)項(xiàng)目依賴中有Jakarta的包,但是代碼標(biāo)紅提示沒有相關(guān)的類,本文就來(lái)介紹一下解決方法,感興趣的可以了解一下2024-02-02
快速掌握SpringBoot應(yīng)用的啟動(dòng)入口
本篇并不是深究?jī)?nèi)置服務(wù)器的啟動(dòng)過(guò)程,而是追溯Springboot啟動(dòng)之前到底做了什么?它是如何與我們經(jīng)常寫的@SpringBootApplication注解注釋的main方法類綁定起來(lái)的?對(duì)SpringBoot啟動(dòng)入口相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05
Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求
這篇文章主要介紹了Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java基礎(chǔ)之MapReduce框架總結(jié)與擴(kuò)展知識(shí)點(diǎn)
本章,是MapReduce的最終章,我在寫本章的時(shí)候,發(fā)現(xiàn)前面忘記介紹MpaTask與ReduceTask了,所以本章補(bǔ)上哈,另外還有兩個(gè)擴(kuò)展的知識(shí)點(diǎn),講完這些,我會(huì)對(duì)整個(gè)MapReduce進(jìn)行總結(jié)一下,讓大家再次了解MapReduce的工作流程,更加清晰地認(rèn)識(shí)MapReduce ,需要的朋友可以參考下2021-05-05
Java手寫簡(jiǎn)易版HashMap的使用(存儲(chǔ)+查找)
這篇文章主要介紹了Java手寫簡(jiǎn)易版HashMap的使用(存儲(chǔ)+查找),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

