Mybatis涓?{}鍜?{}

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis涓?{}鍜?{}相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%bf%87%e6%bb%a4' title='杩囨护'>杩囨护   prepare   鐩存帴   col   tis   name   鑷姩   

1銆?{}灏嗕紶鍏ョ殑鏁版嵁閮藉綋鎴愪竴涓瓧绗︿覆锛屼細瀵硅嚜鍔ㄤ紶鍏ョ殑鏁版嵁鍔犱竴涓紩鍙凤紙鍗曞紩鍙凤紵鍙屽紩鍙凤紵鍔犱簡寮曞彿灏卞浜嗭級濡傦細

//浼犲叆鐨勫€兼槸sex
order by #{column}

瑙f瀽鍚庝负

order by “sex” //灏嗕細鍑洪敊

2銆?{}灏嗕紶鍏ョ殑鏁版嵁鐩存帴鏄剧ず鐢熸垚鍦╯ql涓紝濡傦細

//浼犲叆鐨勫€兼槸sex
order by ${column}

瑙f瀽鍚庝负

order by sex //姝g‘鐨?/pre>

3銆?{}鍙互鍦ㄥ緢澶х▼搴︿笂闃叉sql娉ㄥ叆锛?{}鏃犳硶闃叉sql娉ㄥ叆

4銆?{}涓€鑸敤浜庝紶鍏ユ暟鎹簱瀵硅薄锛屼緥濡備紶鍏ヨ〃鍚嶏紙mybatis鍔ㄦ€佹煡璇級

6銆佽兘鐢?灏卞埆鐢?

#浣跨敤浜哖reparedStatement鏉ヨ繘琛岄澶勭悊锛岀劧鍚庣敤set鐨勬柟寮忓鍗犱綅绗﹁繘琛岃缃紱

$鍒欐槸閫氳繃Statement鐩存帴杩涜鏌ヨ锛屽綋鏈夊弬鏁版椂鐩存帴杩涜鎷兼帴杩涜鏌ヨ銆傦紙鍏蜂綋澶у鍙互鍘荤湅jdbc閲岄潰鐨凱reparedStatement鍜孲tatement鐨勫鐞嗘柟寮忎笉鍚岋級

mybatis鍦ㄥSQL璇彞杩涜棰勭紪璇戜箣鍓嶏紝浼氬SQL杩涜鍔ㄦ€佽В鏋愶紝#{}鍜?{}浼氭湁涓嶅悓鐨勫彉鐜般€?/p>

鍦ㄤ笅闈㈢殑璇彞涓紝name鍊间负zhangsan锛屼笅闈袱绉嶆柟寮忓緱鍒扮殑缁撴灉娌℃湁鍖哄埆

1銆乻elect * from user where name  = #{name};
2銆乻elect * from user where name  = ${name};

寰楀埌鐨勮В鏋愬悗鐨勭粨鏋滃潎涓?/p>

select * from user where name = 鈥榸hangsan鈥?/pre>

浣嗘槸#{}鍦ㄥ姩鎬佽В鏋愮殑鏃跺€欙紝浼氭妸鍙傛暟閮ㄥ垎瑙f瀽鎴愪竴涓崰浣嶇锛熶唬鏇?/p>

select * from user where name = #{name};
鍔ㄦ€佽В鏋愪负
select * from user where name = ?; 

鑰?{}鍒欐槸绠€鍗曠殑瀛楃鏇挎崲锛屽湪鍔ㄦ€佽В鏋愰樁娈碉紝璇ql浼氳瑙f瀽鎴?/p>

select * from user where name = 鈥榸hangsan鈥?

浠ヤ笂锛?{} 鐨勫弬鏁版浛鎹㈡槸鍙戠敓鍦?DBMS(鏁版嵁搴撶鐞嗙郴缁? 涓紝鑰?${} 鍒欏彂鐢熷湪鍔ㄦ€佽В鏋愯繃绋嬩腑銆?/p>

閽堝涓婇潰鐨剆ql锛屽鏋滀紶鍏ョ殑鍊兼槸;drop table user;锛?/p>

閭d箞绗竴鏉$敤#{}鐨剆ql瑙f瀽涓猴細

select * from user where name = “;drop table user;” //鏌ヤ笉鍒颁笢瑗?/pre>

閭d箞绗簩鏉$敤${}鐨剆ql瑙f瀽涓猴細

select * from user where name = ;drop table user;//浼氬嚭閿?br />浼犲叆鐨勫簲璇ame = lhh;drop table user;鎵嶅鍟婏紝瑕佹槸name=;鏈潵灏变細鏈夐敊锛?/pre>

杩欐椂鍊欏凡缁弒ql娉ㄥ叆浜嗐€?/p>

浼樺厛浣跨敤 #{}銆傚洜涓?${} 浼氬鑷?sql 娉ㄥ叆鐨勯棶棰樸€?/strong>

鑻ヤ笉寰椾笉浣跨敤“${xxx}”杩欐牱鐨勫弬鏁帮紝瑕佹墜宸ュ湴鍋氬ソ杩囨护宸ヤ綔锛屾潵闃叉sql娉ㄥ叆鏀诲嚮銆?/p>

以上是关于Mybatis涓?{}鍜?{}的主要内容,如果未能解决你的问题,请参考以下文章

Vue涓殑 el 鍜?mount 鎸傝浇

Java7/8 涓?HashMap 鍜?ConcurrentHashMap婧愮爜瀵规瘮鍒嗘瀽

娣卞叆鐞嗚В Http 鍜?Https

Redis 鍜?Zookeeper 鍒板簳璋佹洿鐗涳紵