﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Roy's Blog-随笔分类-ibatis</title><link>http://www.blogjava.net/RoyPayne/category/50131.html</link><description>－敲击思想的键盘，滑动灵感的鼠标。</description><language>zh-cn</language><lastBuildDate>Sat, 26 Nov 2011 00:00:34 GMT</lastBuildDate><pubDate>Sat, 26 Nov 2011 00:00:34 GMT</pubDate><ttl>60</ttl><item><title>Ibatis (井号)# (美元符号)$ 区别</title><link>http://www.blogjava.net/RoyPayne/archive/2011/11/17/364134.html</link><dc:creator>RoyPayne</dc:creator><author>RoyPayne</author><pubDate>Thu, 17 Nov 2011 14:15:00 GMT</pubDate><guid>http://www.blogjava.net/RoyPayne/archive/2011/11/17/364134.html</guid><wfw:comment>http://www.blogjava.net/RoyPayne/comments/364134.html</wfw:comment><comments>http://www.blogjava.net/RoyPayne/archive/2011/11/17/364134.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RoyPayne/comments/commentRss/364134.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RoyPayne/services/trackbacks/364134.html</trackback:ping><description><![CDATA[<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">1、#可以进行预编译，进行类型匹配，#变量名#? 会转化为 jdbc的?类型</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">?? $不进行数据类型匹配，$变量名$就直接把$name$替换为 name的内容</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">?? 例如：<br />????select * from tablename where id = #id#，假设id的值为12,其中如果数据库字段id为字符型，那么#id#表示的就是'12'，如果id为整型，那么#id#就是 12</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">??? 会转化为jdbc的select * from tablename where id=？，把？参数设置为id的值</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">????select * from tablename where id = $id$，如果字段id为整型，Sql语句就不会出错，但是如果字段id为字符型，<br />????那么Sql语句应该写成 select * from table where id = '$id$'<br />????</p>
<p style="padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" align="center"></p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">3、#方式能够很大程度防止sql注入．<br />4、$方式无法方式sql注入．<br />5、$方式一般用于传入数据库对象．例如传入表名.<br />6、所以ibatis用#比$好,一般能用#的就别用$.</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">另外,使用##可以指定参数对应数据库的类型</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">如:</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">select * from tablename where id =#id:number#?</p>
<p style="text-align: left; padding-bottom: 0px; border-right-width: 0px; widows: 2; text-transform: none; list-style-type: none; text-indent: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; border-top-width: 0px; border-bottom-width: 0px; letter-spacing: normal; color: rgb(0,0,0); border-left-width: 0px; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">在做in，like 操作时候要特别注意</p><span style="widows: 2; text-transform: none; text-indent: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">mysql: select * from user where user_name like concat('%',#name#,'%')oracle: select * from user where user_name like '%'||#name#||'%'sql server: select * from user where user_name like '%'+#name#+'%'</span><span style="widows: 2; text-transform: none; text-indent: 0px; font: 14px/20px Simsun; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br /></span><img src ="http://www.blogjava.net/RoyPayne/aggbug/364134.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RoyPayne/" target="_blank">RoyPayne</a> 2011-11-17 22:15 <a href="http://www.blogjava.net/RoyPayne/archive/2011/11/17/364134.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>