After learning how to use ibatis over the past few days, I suddenly used fuzzy queries and dynamic multi-condition queries. I tried it for a long time based on my own ideas and did not solve this problem.
The first problem is fuzzy query. At the beginning, I used the following conditions: Select * from user where name like '% # value # % '. but why not? It seems that an error has been reported. later, I found a solution on the Internet, which is to use $ instead.
1> write as: Like '% $ value $ %'. <! -- Fuzzy query cannot be used #, # Is preparestatement used? Insert parameter, $ is text replacement -->,
2> at the same time, I found another method, but I tried that method for a long time, but it won't work. The method is like '%' | # value # | '% ', the query results are all. later, some people said on the Internet that this method is written in Oracle,
3> if it is MySQL, it should be written as: name like Concat ('%', # value: varchar #, '%'), but I have never tried it, there is a method to complete the operation.
I tried the first method successfully, but I did not try it later. If you are interested, you can try it.
The second major problem is multi-condition Combined Query. At the beginning, I was also thinking about this problem. I cannot write an SQL statement for every query. This is too much ........ later, I referred to some documents and found that the original ibatis provides dynamic ing. example:
<! -- Using secure concatenation statements in ibatis, one of the advantages of dynamic query ibatis over JDBC, safe and efficient description text in the comment --> <select id = "selectallproducts" parameterclass = "product" resultmap = "productresult"> select ID, note from product <dynamic prepend = "where"> <! -- Isnotnull: determines whether the parameter exists. The integer type --> <isnotnull property = "ID"> <! -- Isgreaterthan: determines whether the parameter is greater than comparevalue, isgreaterequals is greater than or equal to --> <isgreaterthan prepend = "and" property = "ID" comparevalue = "0"> id = # ID # </isgreaterthan> </isnotnull> <! -- Isnotempty indicates that the string is not empty. isempty indicates that the string is empty. --> <isnotempty prepend = "and" property = "NOTE"> <! -- Fuzzy query cannot be used #, # Is preparestatement used? Insert parameters, $ is text replacement --> note like '% $ note $ %' </isnotempty> </dynamic> </SELECT>
Transmit parameters with map
<Select id = "selectallproducts" parameterclass = "Java. util. hashmap" resultmap = "productresult"> select ID, note from product <dynamic prepend = "where"> <! -- Ispropertyavailable determines whether the attribute is valid --> <ispropertyavailable property = "ID"> <isnotnull property = "ID"> <! -- Islessthan: determines whether the parameter is smaller than comparevalue, islessequals is less than or equal to --> <islessthan prepend = "and" property = "ID" comparevalue = "10"> id = # ID # </islessthan> </isnotnull> </ispropertyavailable> </dynamic> </SELECT>
Several common attributes:
Attribute keyword |
Description |
<Isequal> |
If the parameter is equal to the value, the query condition is valid. |
<Isnotequal> |
If the parameter is not equal to the value, the query condition is valid. |
<Isgreaterthan> |
If the parameter value is greater than the value, the query condition is valid. |
<Isgreaterequal> |
If the parameter is equal to the value, the query condition is valid. |
<Islessequal> |
If the parameter is smaller than the value, the query condition is valid. As follows: <Islessequal prepend = "and" property = "Age" comparevalue = "18"> Adolescent = 'true' </Islessequal> |
<Ispropertyavailable> |
If the parameters are used, the query conditions are valid. |
<Isnotpropertyavailable> |
If the parameter is not used, the query condition is valid. |
<Isnull> |
If the parameter is null, the query condition is valid. |
<Isnotnull> |
If the parameter is not null, the query condition is valid. |
<Isempty> |
If the parameter is null, the query condition is valid. |
<Isnotempty> |
If the parameter is not blank, the query condition is valid. When the data type of the parameter is collection or string, the parameter is not null or "". As follows: <Isnotempty prepend = "and" property = "firstname"> First_name = # firstname # </Isnotempty> |
<Isparameterpresent> |
If the parameter class is not null, the query condition is valid. |
<Isnotparameterpresent> |
Checks to see if the parameter object is not present (null). Example usage: <Isnotparameterpresent prepend = "and"> Employee_type = 'default' </Isnotparameterpresent>
|