Mybatis wildcard, Mybatis and SQL fuzzy query
A, mybatis of $ and #
In mybatis in $
and #
are dynamic parameters passed in the sql.
select id,name,age from student where name=#{name}
This name is dynamic and variable. When you pass what value will be executed sql statement based on your passed-in value.
- MyBatis SQL statements used to pass parameters parameterType, the type can be substantially parameterType type int, String, HashMap and custom types java
- In the xml to write SQL are used
#
, such as#{param}
.
But sometimes you see with$
time, such as${param}
- Well
$
and#
, what difference does it make?
(1) wildcard character "#
" is passed as a string value, such as:
select id,name,age from user where id=#{id}
当前端把id值1,传入到后台的时候,就相当于 (#{} 传入值时,sql解析时,参数是带引号的)
select id,name,age from student where id ='1'
(2) wildcard character "$
" is displayed directly incoming data generation sql statement, such as
select id, name, age from user where id=${id}
当前端把id值1,传入到后台的时候,就相当于 (${}穿入值,sql解析时,参数是不带引号的)
select id,name,age from student where id = 1
(3) "#
" can be largely prevent sql injection. (Splicing statement)
#{}
: resolved to a JDBC prepared statement (prepared statement) parameter markers, a # {} is interpreted as a parameter placeholder.
${}
: Just a pure replacement for the broken string, in a dynamic SQL parsing stage will be variable substitution. For example:
select id,name,age from student where name=#{name} -- name='cy'
select id,name,age from student where name=${name} -- name=cy
(4) However, if used in the order by the need to use "$
."
(5) is often used in most cases "#
", but must be used under different circumstances "$
."
Two, Mybatis various fuzzy query
1. sql string splicing
SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');
2. Instead of using $ {...} # {...}
SELECT * FROM tableName WHERE name LIKE '%${text}%';
3. stitching program
Java
// or String searchText = "%" + text + "%";
String searchText = new StringBuilder("%").append(text).append("%").toString();
parameterMap.put("text", searchText);
SqlMap.xml
SELECT * FROM tableName WHERE name LIKE #{text};
4. Match case inquiry
SELECT * FROM TABLENAME WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%'
--或者是
SELECT * FROM TABLENAME WHERE LOWER(SUBSYSTEM) LIKE '%' || LOWER('jz') || '%'
Three, MySql fuzzy query
During a database query, a complete query and fuzzy query points.
General fuzzy query as follows:
SELECT 字段 FROM 表 WHERE 某字段 Like 条件
Wherein on condition, SQL is provided four kinds of pattern matching :
(1) %: 0 indicates an arbitrary or more characters . Can match any type and length of characters, in some cases if the Chinese use two percent signs (%%) denote.
For example, SELECT * FROM [user] WHERE u_name LIKE '%三%'
will the u_name as "Joe Smith", "Zhang cat three", "three legs", "Zang" and so on "three" to find out the whole record.
In addition, if you need to find out u_name in both "three" and a "cat" record, conditions of use and
SELECT * FROM [user] WHERE u_name LIKE '%三%' AND u_name LIKE '%猫%'
if used SELECT * FROM [user] WHERE u_name LIKE '%三%猫%'
although able to search out the "three legs", but you can not search out qualified "Zhang cat three."
(2) _: represents any single character . Matches any single character, it is used to limit the expression of the character length statement:
example SELECT * FROM [user] WHERE u_name LIKE '_三_'
only identify the "Zang" u_name such as words of a word and the "three";
Another example SELECT * FROM [user] WHERE u_name LIKE '三__';
only find the "three legs" so name for the first word and the words "three" in;
(3) []: The character listed in the brackets in a (similar to regular expressions) . Specify a character string or the scope of the claims to be matched to any one of them.
For example, SELECT * FROM [user] WHERE u_name LIKE '[张李王]三'
you will find "Joe Smith", "Lee III", "King III" (rather than "Zhang Li Wang Three");
If there is a series of characters [] within (01234, abcde and the like) can be abbreviated as "0-4", "ae"
the SELECT * the FROM [the User] the WHERE u_name the LIKE 'old [1-9]'
will identify " old one "," the old 2 ", ......" old 9 ";
(4) [^]: represents a single character is not listed in the brackets . Its value, and [] the same, but it requires a character to any object other than the specified character matches.
For example, SELECT * FROM [user] WHERE u_name LIKE '[^张李王]三'
will find no name, "Zhang", "Li", "King" and "Zhao Three", "Sun Three" and so on;
SELECT * FROM [user] WHERE u_name LIKE '老[^1-4]';
the exclusion of "the old one" to "the old 4", looking for "old 5" and "old 6 "......
Note:
Due to the wildcard's sake, lead us to query the special character "%", "_", "[" the statement can not be achieved properly, and the special characters "[]" from the enclosed can be a normal query. Accordingly, we write the following function:
function sqlencode(str)
str=replace(str,"[","[[]") '此句一定要在最前
str=replace(str,"_","[_]")
str=replace(str,"%","[%]")
sqlencode=str
end function
Will be examined before the query string before the function can be treated, use of such a database query and is connected on the page is to be noted Hou:
As Select * FROM user Where name LIKE '老[^1-4]'
;
above '
old [^ 1-4] '
is to have a single quotes ,do not forget!
SQL模糊查询,使用like比较关键字,加上SQL里的通配符,请参考以下:
1、LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。
2、LIKE'%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。
3、LIKE'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。
7、LIKE'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。
for example:
sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address
Examples explain : zipcode_key queries based on zip code table in the variable zipcode corresponding data sentence is determined whether the variable is non-digital zipcode_key query, with a string of arbitrary length to match%, from the table address, city, Shozo column query all the data items contained keywords and provincial, city, address sorting.
Examples of fuzzy query using stored procedures:
SELECT * FROM Questions where QTitle like ' % [ '+ @KeyWord +' ] % ' and IsFinish = @IsFinsih`
Statement pairs of square brackets is the key to writing format.