詳解mybatis #{}和${}的區(qū)別、傳參、基本語法
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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot Actuator的指標監(jiān)控可視化功能詳解
SpringBoot Actuator是springboot為簡化我們對微服務(wù)項目的監(jiān)控功能抽取出來的模塊,使得我們每個微服務(wù)快速引用即可獲得生產(chǎn)界別的應(yīng)用監(jiān)控、審計等功能。這篇文章主要介紹了springboot Actuator的指標監(jiān)控可視化,需要的朋友可以參考下2021-08-08Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07spring boot使用@Async異步注解的實現(xiàn)原理+源碼
通常我們都是采用多線程的方式來實現(xiàn)上述業(yè)務(wù)功能,但spring 提供更優(yōu)雅的方式來實現(xiàn)上述功能,就是@Async 異步注解,在方法上添加@Async,spring就會借助AOP,異步執(zhí)行方法,接下來通過本文給大家介紹spring boot異步注解的相關(guān)知識,一起看看吧2021-06-06Spring JdbcTemplate實現(xiàn)添加與查詢方法詳解
JdbcTemplate是Spring框架自帶的對JDBC操作的封裝,目的是提供統(tǒng)一的模板方法使對數(shù)據(jù)庫的操作更加方便、友好,效率也不錯,這篇文章主要介紹了Spring?JdbcTemplate執(zhí)行數(shù)據(jù)庫操作,需要的朋友可以參考下2022-11-11SpringBoot中整合Ehcache實現(xiàn)熱點數(shù)據(jù)緩存的詳細過程
這篇文章主要介紹了SpringBoot中整合Ehcache實現(xiàn)熱點數(shù)據(jù)緩存,SpringBoot 中使用 Ehcache 比較簡單,只需要簡單配置,說白了還是 Spring Cache 的用法,合理使用緩存機制,可以很好地提高項目的響應(yīng)速度,需要的朋友可以參考下2023-04-04Java?Chassis3的多種序列化方式支持技術(shù)解密
這篇文章主要為大家介紹了Java?Chassis?3多種序列化方式支持技術(shù)解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01基于Servlet實現(xiàn)技術(shù)問答網(wǎng)站系統(tǒng)
這篇文章主要為大家詳細介紹了基于Servlet實現(xiàn)技術(shù)問答網(wǎng)站系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04