MyBatis 動態SQL與SQL片斷

前言:在使用Mybatis中想將生成的sql片斷插入到mappper.xml中的sqlsjava

SQL片斷引用方法一:將SQL片斷當作參數,使用${}帶入參數sql

DaoMapper:app

int updateUserInfo(@Param("uuid2") String uuid2, @Param("updateStr") String updateStr);

mapper.xmlui

<update id="updateUserInfo" >

    update yxs_user_info set ${updateStr},update_date=now()

    where user_uuid = (select uuid from yxs_user_login_info where uuid2=#{uuid2})

</update>

參數uuid2值爲ABC, updateStr值爲  email = 」lhm1830**@163.com「,,最終MyBatis解析後的SQL爲:code

update yxs_user_info set  email = 」lhm18***@163.com「 ,update_date=now()   where user_uuid = (select uuid from yxs_user_login_info where uuid2="ABC")xml

注:字符串

1. #{data}將傳入的數據都當成一個字符串,會對自動傳入的數據加一個雙引號。如:order by #{user_id},若是傳入的值是ABC,那麼解析成SQL時的值爲order by "ABC"。class

2. ${}將傳入的數據直接顯示生成在sql中。email

3. #方式可以很大程度防止sql注入, $方式沒法防止Sql注入。date

4. 通常能用#的就別用$。

相關文章
相關標籤/搜索