MyBatis How to prevent SQL injection

Source: Internet
Author: User
Tags how to prevent sql injection sql injection

SQL injection

What is SQL injection? First of all, SQL injection is an attack method, a malicious SQL statement is used to deceive the server to execute SQL commands, the database in the background to parse, so as to reach the target network, access to sensitive information attack means.

MyBatis How to prevent SQL injection in SQL # and $ differences
# $
Equivalent to double quotation marks on the data Equivalent to displaying data directly
Prevent SQL injection to a large extent Cannot prevent SQL injection
#{xxx}, using the PreparedStatement, there will be type conversion, more secure ${XXX}, using string concatenation, easy SQL injection

simply put, #{} is pre-compiled, is secure,${} is not precompiled, just takes the value of the variable, is non-secure, there is SQL injection.

Example
1<select id= "Selectbackgoodsdetail" resulttype= "Java.util.Map" >2 SELECT sum (a.item_num) Backgoodsnum,a.item_price Backgoodsprice,3SUM (A.item_num *a.item_price) Backgoodssumprice,4 b.barcode,b.name itemname,b.weight,c.name itemcategoryname5 From Back_goods_detail a6Left JOIN item B on a.item_id=b.id7Left JOIN item_category C on b.item_category_id =c.id8<where>9<iftest= "Backgoodsid!=null" >Tena.back_goods_id =#{backgoodsid} One</if> A<iftest= "Itemcategoryid!=null" > -and b.item_category_id =#{itemcategoryid} -</if> the<ifTest= "searchkey!= null" > -and (B.sequence like CONCAT ('% ', #{searchkey}, '% ') -OR b.name like CONCAT ('% ', #{searchkey}, '% ') -OR b.barcode like CONCAT ('% ', #{searchkey}, '% ')) +</if> -</where> + GROUP by a.item_id A LIMIT #{pagestart},#{pagenum} at</select>

MyBatis How to prevent SQL injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.