Ibatis fuzzy query SQL

Source: Internet
Author: User

For a simple SQL statement, for example:

Select * from user where id = # ID #

Ibatis will compile the SQL statement

Select * from user where id =?

The passed value is then filled, similar to the JDBC preparestatment format.
The preceding SQL statement is a simple method. Adding like statements that require fuzzy queries is troublesome and may cause SQL injection.
If you want to query users with "sa" in their usernames, you may write SQL statements in this way.
 

Select * from user where name like '% SA %'

But it cannot be written in the XML of ibatis.

Select * from user where name like # % SA % # Or like % # SA # % is definitely not acceptable, and an error is reported.

There is a simple way to write

Select * from user where name like '% $ name $ %'

$ Will embed the original parameters into the SQL statement without pre-compilation, which may cause SQL injection attacks. In fact, ibatis simply put, every # is used as a parameter and precompiled in setobject mode. The $ method directly replaces the string. Therefore, $ is not safe. It puts user input directly into SQL.

Conclusion: We recommend that you do not use like for queries. 1 is because of low efficiency, 2 is because it is quite troublesome to use in ibatis, and the readability is poor. If you really want to use it, do not use $, instead, use the # Like patchwork form:

Select * from user where name like '%' | # name # | '% ';

 

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.