詳解mybatis #{}和${}的區(qū)別、傳參、基本語法
更新時間:2020年07月22日 10:09:12 作者:wutongyuWxc
這篇文章主要介紹了mybatis #{}和${}的區(qū)別、傳參、基本語法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
1 #{}和${}的區(qū)別、及注入問題
(1) 區(qū)別:
首先清楚一點,動態(tài) SQL 是 mybatis 的強大特性之一,在 mapper 中定義的參數(shù)傳到 xml 中之后,在查詢之前 mybatis 會對其進行動態(tài)解析,#{} 和 ${} 在預(yù)編譯中的處理是不一樣的:
例如:select * from t_user where userName = #{name};
#{}預(yù)編譯:用一個占位符 ? 代替參數(shù):select * from t_user where userName = ?
#{}預(yù)編譯:會將參數(shù)值一起進行編譯:select * from t_user where userName = 'zhangsan'
(2) 使用場景:
一般情況首選#{},因為這樣能避免sql注入;如果需要傳參 動態(tài)表名、動態(tài)字段名時,需要使用${}
比如:select * from ${tableName} where id > #{id};
(3) SQL注入問題:
舉個例子,如果使用${}出現(xiàn)的注入問題:
select * from ${tableName};
如果傳參 t_user;delete from t_user,則預(yù)編譯后的sql如下,將會導(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ù)
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ù)實現(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Mybatis中#{}和${}傳參的區(qū)別及#和$的區(qū)別小結(jié)
- MyBatis中#{}和${}的區(qū)別詳解
- MyBatis 中 ${}和 #{}的正確使用方法(千萬不要亂用)
- mybatis中${}和#{}的區(qū)別以及底層原理分析
- MyBatis中使用#{}和${}占位符傳遞參數(shù)的各種報錯信息處理方案
- MyBatis中#{}?和?${}?的區(qū)別和動態(tài)?SQL詳解
- MyBatis 探秘之#{} 與 ${} 參傳差異解碼(數(shù)據(jù)庫連接池筑牢數(shù)據(jù)交互根基)
- MyBatis中#{}和${}的區(qū)別及底層原理、適用場景和安全風(fēng)險
相關(guān)文章
Java的ConcurrentHashMap中不能存儲null的原因解析
眾所周知,在Java中Map可以存儲null,而ConcurrentHashMap不能存儲null值,那么為什么呢?今天通過源碼分析給大家詳細(xì)解讀,感興趣的朋友一起看看吧2022-07-07
SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出
這篇文章主要介紹了SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出的方法,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下2021-05-05

