詳解mybatis #{}和${}的區(qū)別、傳參、基本語法
1 #{}和${}的區(qū)別、及注入問題
(1) 區(qū)別:
首先清楚一點(diǎn),動(dòng)態(tài) SQL 是 mybatis 的強(qiáng)大特性之一,在 mapper 中定義的參數(shù)傳到 xml 中之后,在查詢之前 mybatis 會(huì)對(duì)其進(jìn)行動(dòng)態(tài)解析,#{} 和 ${} 在預(yù)編譯中的處理是不一樣的:
例如:select * from t_user where userName = #{name};
#{}預(yù)編譯:用一個(gè)占位符 ? 代替參數(shù):select * from t_user where userName = ?
#{}預(yù)編譯:會(huì)將參數(shù)值一起進(jìn)行編譯:select * from t_user where userName = 'zhangsan'
(2) 使用場(chǎng)景:
一般情況首選#{},因?yàn)檫@樣能避免sql注入;如果需要傳參 動(dòng)態(tài)表名、動(dòng)態(tài)字段名時(shí),需要使用${}
比如:select * from ${tableName} where id > #{id};
(3) SQL注入問題:
舉個(gè)例子,如果使用${}出現(xiàn)的注入問題:
select * from ${tableName};
如果傳參 t_user;delete from t_user,則預(yù)編譯后的sql如下,將會(huì)導(dǎo)致系統(tǒng)不可用:
select * from t_user;delete from t_user;
(4) like 語句防注入:
使用concat函數(shù):
select * from t_user where name like concat('%', #{name}, '%')
2 mybatis幾種傳參方式
非注解:
(1)單參數(shù):
public User getUserByUuid(String uuid);
<select id="getUserByUuid" resultMap="BaseResultMap" parameterType="Object">
SELECT * FROM t_user WHERE uuid = #{uuid}
</select>
(2)多參數(shù)
public User getUserByNameAndPass(String name,String pass);
<select id="getUserByNameAndPass" resultMap="BaseResultMap" parameterType="Object">
SELECT * FROM t_user WHERE t_name = #{0} and t_pass = #{1}
</select>
(3)Map參數(shù)
public User getUserByMap(Map<String,Object> map);
<select id="getUserByMap" resultMap="BaseResultMap" parameterType="java.util.Map">
SELECT * FROM t_user WHERE t_name = #{name} and t_pass = #{pass}
</select>
(4)實(shí)體對(duì)象參數(shù)
public int updateUser(User user);
<select id="updateUser" resultMap="BaseResultMap" parameterType="Object">
update t_user set t_name = #{name}, t_pass = #{pass} where uuid=#{uuid}
</select>
(4)List集合參數(shù)
public int batchDelUser(List<String> uuidList);
<delete id="batchDelUser" parameterType="java.util.List">
DELETE FROM t_user WHERE uuid IN
<foreach collection="list" index="index" item="uuid" open="(" separator="," close=")">
#{uuid}
</foreach>
</delete>
注解:
public List<User> getUserByTime(@Param("startTime")String startTime, @Param("endTime")String endTime);
<select id="getUserByTime" resultMap="BaseResultMap" parameterType="Object">
SELECT * from t_user where createTime >= #{startTime} and createTime <= #{endTime}
</select>
2 choose when otherwise
//JAVA 代碼
public List<Group> getUserRoleRelByUserUuid(@Param("groupUuid") String userUuid,@Param("roleList")List<String> roleUuidList);
//SQL
SELECT * from user_role where groupUuid=#{groupUuid}
<choose>
<when test="roleList!=null&&roleList.size()>0">
AND roleUuid IN
<foreach collection="roleList" index="index" item="roleUuid" open="(" separator="," close=")">
#{roleUuid}
</foreach>
</when>
<otherwise>
AND roleUuid IN ('')
</otherwise>
</choose>
3 判斷字符串相等
//JAVA 代碼
public int getOrderCountByParams(Map<String, Object> params);
//SQL
<select id="getOrderCountByParams" resultType="java.lang.Integer" parameterType="Object">
SELECT count(*) FROM itil_publish_order where 1=1
<if test="timeType == '1'.toString()" >
AND create_time >= #{timeStr}
</if>
<if test="timeType == '2'.toString()" >
AND end_time <= #{timeStr}
</if>
</select>
或者
<if test = 'timeType== "1"'> </if>
4 CONCAT函數(shù)實(shí)現(xiàn) 模糊匹配
<select id="getMaxSerialCode" resultType="java.lang.String" parameterType="Object">
SELECT count(*) FROM
itil_publish_order
WHERE serial_code LIKE CONCAT('%',#{codeStr},'%')
ORDER BY serial_code DESC LIMIT 1
</select>
5 大于等于、小于等于
//JAVA代碼
public List<PublishOrder> getOrderCount(@Param("startTime") String startTime,@Param("startTime")List<String> startTime);
//SQL
<select id="getOrderCount" resultType="java.lang.String" parameterType="Object">
SELECT * FROM itil_publish_order
WHERE createTime >= #{startTime} and <= #{startTime}
</select>
到此這篇關(guān)于mybatis #{}和${}的區(qū)別、傳參、基本語法的文章就介紹到這了,更多相關(guān)MyBatis中${}和#{}傳參的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot如何整合Springsecurity實(shí)現(xiàn)數(shù)據(jù)庫登錄及權(quán)限控制
這篇文章主要給大家介紹了關(guān)于SpringBoot如何整合Springsecurity實(shí)現(xiàn)數(shù)據(jù)庫登錄及權(quán)限控制的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
生產(chǎn)環(huán)境NoHttpResponseException異常排查解決記錄分析
這篇文章主要為大家介紹了生產(chǎn)環(huán)境NoHttpResponseException異常排查解決記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
java實(shí)現(xiàn)微信支付結(jié)果通知
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信支付結(jié)果通知,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Java編程實(shí)現(xiàn)軌跡壓縮算法開放窗口實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)軌跡壓縮算法開放窗口實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
解決Java?API不能遠(yuǎn)程訪問HBase的問題
這篇文章主要介紹了解決Java?API不能遠(yuǎn)程訪問HBase的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
JAVA項(xiàng)目如何打包部署到Linux服務(wù)器上
本文詳細(xì)介紹了在服務(wù)器上部署環(huán)境包括JDK、MySQL、Tomcat的設(shè)置,以及使用Idea-Maven-SpringBoot進(jìn)行jar包打包部署的流程,內(nèi)容涵蓋了MySQL配置注意事項(xiàng)、pom.xml配置、打包命令等關(guān)鍵步驟,同時(shí),也提供了如何將jar包上傳到Linux服務(wù)器并運(yùn)行的具體方法2024-10-10

