這篇文章給大家分享的是有關(guān)MyBatis中#與$取值有什么區(qū)別的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)公司是一家從事企業(yè)網(wǎng)站建設(shè)、成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)制作的專業(yè)網(wǎng)絡(luò)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計(jì)人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)上1000家。
一、前言
動(dòng)態(tài)SQL是MyBatis的主要特性之一,在mapper中定義的參數(shù)傳到XML中之后,在查詢之前 mybatis 會(huì)對(duì)其進(jìn)行動(dòng)態(tài)解析。MyBatis 為我們提供了兩種支持動(dòng)態(tài)SQL的語法:#{} 以及 ${}。
二、案例
下面我們通過相關(guān)案例來演示一下$與#的用法。
UserMapper.xml文件中查詢語句最初寫法
<select id="findUserByMapParam" parameterType="com.queen.mybatis.bean.User" resultType="com.queen.mybatis.bean.User"> select id, loginId, userName, role, note from t_user where id = #{id} and userName=#{userName} </select>
測試控制臺(tái)打印SQL如下:
2017-08-06 18:23:24,390 [main] [com.queen.mybatis.mapper.UserMapper.findUserByMapParam]-[DEBUG] ==> Preparing: select id, loginId, userName, role, note from t_user where id = ? and userName=?
現(xiàn)在我們修改一下語句,將where id = #{id} 修改成 ${id},如下:
<select id="findUserByMapParam" parameterType="com.queen.mybatis.bean.User" resultType="com.queen.mybatis.bean.User"> select id, loginId, userName, role, note from t_user where id = ${id} and userName=#{userName} </select>
再次測試,控制臺(tái)打印SQL如下:
2017-08-06 18:27:58,887 [main] [com.queen.mybatis.mapper.UserMapper.findUserByMapParam]-[DEBUG] ==> Preparing: select id, loginId, userName, role, note from t_user where id = 1 and userName=?
可以觀察到兩條SQL語句的不同,下面一條以${}語句獲取參數(shù)值時(shí),直接將數(shù)據(jù)取出來拼接到了SQL語句中;而我們用#{}取得值是以占位符的形式出現(xiàn)。
#{}:是以預(yù)編譯的方式,將參數(shù)設(shè)置到SQL語句中,跟我們?cè)瓉韺W(xué)JDBC時(shí)一樣,能夠很大程度防止sql注入
${}:取出來的值直接拼接到SQL語句中,會(huì)有安全問題,無法防止Sql注入。
大多數(shù)情況下我們?nèi)?shù)的值都是使用的#{},但是有些情況像表名,排序時(shí)使用order by 動(dòng)態(tài)參數(shù)時(shí)需要注意,用$而不是#。
修改UserMapper.xml:
<select id="findUserByMapParam" parameterType="com.queen.mybatis.bean.User" resultType="com.queen.mybatis.bean.User"> select id, loginId, userName, role, note from t_user where id = #{id} and userName=#{userName} order by userName #{orderDesc} </select>
測試控制臺(tái)打印如下:
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.MySQL.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc'' at line 1 ### The error may exist in UserMapper.xml ### The error may involve com.queen.mybatis.mapper.UserMapper.findUserByMapParam-Inline ### The error occurred while setting parameters ### SQL: select id, loginId, userName, role, note from t_user where id = ? and userName=? order by userName ? ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc'' at line 1 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
以#{}號(hào)方式取值,控制臺(tái)報(bào)錯(cuò)。
那我們修改一下UserMapper.xml,修改成${}取值方式
<select id="findUserByMapParam" parameterType="com.queen.mybatis.bean.User" resultType="com.queen.mybatis.bean.User"> select id, loginId, userName, role, note from t_user where id = #{id} and userName=#{userName} order by userName ${orderDesc} </select>
再次測試,控制臺(tái)打印SQL正常:
2017-08-06 19:17:02,331 [main] [com.queen.mybatis.mapper.UserMapper.findUserByMapParam]-[DEBUG] ==> Preparing: select id, loginId, userName, role, note from t_user where id = ? and userName=? order by userName desc
感謝各位的閱讀!關(guān)于“MyBatis中#與$取值有什么區(qū)別”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章標(biāo)題:MyBatis中#與$取值有什么區(qū)別
URL網(wǎng)址:http://chinadenli.net/article48/jiehhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站營銷、定制開發(fā)、網(wǎng)站改版、做網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)