网站开发做网站,网络广告类型,猎头公司找的工作怎么样,公司网页如何免费制作MyBatis中#{}和${}的区别
#{}和${}都是MyBatis提供的sql参数替换。区别是#xff1a;#{}是预编译处理#xff0c;${}是字符串直接替换。#{}可以防止SQL注入#xff0c;${}存在SQL注入的风险#xff0c;例如 “ or 11”虽然存在SQL注入风险#xff0c;但也有自己的适用场…MyBatis中#{}和${}的区别
#{}和${}都是MyBatis提供的sql参数替换。区别是#{}是预编译处理${}是字符串直接替换。#{}可以防止SQL注入${}存在SQL注入的风险例如 “ or 11”虽然存在SQL注入风险但也有自己的适用场景比如排序功能表名字段名等作为参数传入时。#{}模糊查询要搭配使用mysql内置的拼接函数concat安全性高。模糊查询虽然${}可以完成但是存在SQL注入不安全。
直接替换是指是MyBatis 在处理 ${} 时就是把 ${} 替换成变量的值。
预编译处理是指MyBatis 在处理#{}时会将 SQL 中的 #{} 替换为?号使⽤ PreparedStatement 的 set ⽅法来赋值。 $是直接替换传入的SQL参数若遇到String类型时不会自动加单/双引号就会报错必须加单/双引号才不出错。并且还有sql注入问题。不过$也有自己的使用场景比如排序传入descasc字符串时是不需要加单/双引号的。此时就可以用$并且这两个字符串不让用户自己传直接给升序降序按钮也就避免了sql注入风险。能发生SQL注入主要还是为用户提供了输入框用户能传参。 而#无论是Integer类型还是String类型都会提前预编译,预编译SQL而且预编译SQL的性能更高。遇到String类型会自动加单/双引号。 以模糊查询为例${}和#{}的使用区别
使用${},但存在SQL注入风险。
select idgetBookByNname resultTypecom.example.demo.entity.BookInfoselect * from book_info where book_name like ${bookName};
/select
使用#{}更安全更高效。
select idgetBookByName resultTypecom.example.demo.entity.BookInfoselect * from book_info where book_name like concat(%,#{bookName},%);
/select SQL注入问题${}
代码演示所传参数后跟 or 11 。就会查出全结果集。
bookMapper.getBookByN(平凡的世界 or 11); //传入参数
select * from book_info where book_name ${bookName}; //SQL语句 SQL日志打印 动态 SQL 语法
1.if标签
使用场景当我们在输入个人信息的时候不一定都得填写必填项非必填项这时候有的参数就为空。所以在插入时就得判空。 insert idinsert useGeneratedKeystrue keyPropertyidinsert into userinfo {username,password,nickname,if testsex ! null //test中的sex是属性不是字段sex,/if}birthday)values (#{username},#{password},#{nickname},if testsex !null //test中的这里的sex是属性#{sex},/if#{birthday})/insert
注意 test 中的 sex是传⼊对象中的属性不是数据库字段。 2.trim标签 prefix表示整个语句块以prefix的值作为前缀 suffix表示整个语句块以suffix的值作为后缀 prefixOverrides表示整个语句块要去除掉的前缀 suffixOverrides表示整个语句块要去除掉的后缀 如果输入参数全是非必填项。就需要if标签和trim标签相结合。 insert idss useGeneratedKeystrue keyPropertyidinsert into userinfotrim prefix( suffix) suffixOverrides,if testusername ! nullusername,/if //别忘记逗号if testpassword ! nullpassword,/if //test中的是属性不是字段if testnickname ! nullnickname,/ifif testsex ! nullsex,/ifif testbirthday ! nullbirthday,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testusername ! null#{username},/ifif testnickname ! null#{nickname},/ifif testsex ! null#{sex},/ifif testbirthday ! null#{birthday},/if/trim/insert 3.where标签
对于where后跟的参数是否为空不清楚时。 select idselect parameterTypecom.example.demo.entity.UserInfo //parameterType为 传入的参数类型select * from usrinfowhereif testusername ! nulland username #{username}/ifif testuserId ! nulland userId #{userId}/ifif testsex ! nulland sex #{sex}/if/where/select
以上标签也可以使⽤ trim prefixwhere suffixOverridesand 替换。 4.set标签
动态update操作 //parameterType 为传入参数的类型update idupdate parameterTypecom.example.demo.entity.UserInfoupdate userinfosetif testusername ! nullusername #{username},/ifif testpassword ! nullpassword #{password},/ifif testnickname ! nullnickname #{nickname},/ifif testsex ! nullsex #{sex},/ifif testbirthday ! nullbirthday #{birthday},/if/setwhere #{id}/update
以上标签也可以使⽤ trim prefixset suffixOverrides, 替换。 5.foreach标签
遍历集合如List时可以使用例如批量删除等操作。 // collection 集合类型 item集合名delete iddeleteByIdsdelete from userinfo where id inforeach collectionlist itemitem open( close) separator,#{list}/foreach/delete