前言:在使用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. 通常能用#的就别用$。