MyBatis fuzzy Query sql

Source: Internet
Author: User

This afternoon to do a function, to use the fuzzy query, the field is description, just at the beginning of my writing with the SQL in the Fuzzy query statement,

But this has a problem, only the entire value of the field is passed in to query, so it is not confused query.

Later through the search, found to add a concat field, the string must be spliced before the fuzzy query can be implemented.

After changing to this appearance, the Fuzzy query function realizes.

In the blog I searched for some other ways of writing, but tested two, one success, another failed to achieve fuzzy query, but currently do not know where the error. If anyone knows, please enlighten me.

His blog address is: http://blog.sina.com.cn/s/blog_667bef380101f2da.html

I used the first method he said:

1. String concatenation in SQL

SELECT * from TableName where name is like CONCAT (CONCAT ('% ', #{text}), '% ');

That's exactly what I did in this way.

In fact, there is a way: write like this is OK:

His second method:

2. Use ${...} instead of #{...}

SELECT * from TableName WHERE name is like '%${text}% ';

After I tried, I found that I did not realize the function of fuzzy query, and I do not know where the error is, if anyone knows please advise.

His third method:

3. Splicing in the program

Java

String searchtext = "%" + text + "%";

String SearchText = new StringBuilder ("%"). Append (Text). Append ("%"). ToString ();

Parametermap.put ("text", SearchText);

Sqlmap.xml

SELECT * from TableName WHERE name is like #{text};

I haven't tried this method, but I'll record it and try it later.

He's got a fourth way.

4. Case matching Query

SELECT * from TABLENAME WHERE UPPER (SUBSYSTEM) like '% ' | | UPPER (' JZ ') | | ‘%‘

Or

SELECT * from TABLENAME WHERE LOWER (SUBSYSTEM) like '% ' | | LOWER (' JZ ') | | ‘%‘

That's right.

Http://www.cnblogs.com/dushan/p/4766954.html

MyBatis fuzzy Query sql

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.