---- only add up to pay homage, like me, slowly advancing human children
Question : fuzzy query in sqlserver, problems
Initially using "concat", will nest string, as shown below:
< SELECT ID = " queryCount " the resultType = " int " > SELECT COUNT (* ) from T_USER < WHERE > < IF Test = " ! = Null the queryText " > loginacct the concat like ( " % " , the queryText # {}, " %) </ IF> </ WHERE > </ SELECT >
Operating results : " concat " is not a recognized function, not a built-in function
Results : "concat" fight string method applies only to mysql and Oracle, SQLServer does not apply, this function is used to connect the string, not SQLServer, symbols may be used to get connected to +
Solution : use the traditional spelling string operations, " like " operation to try:
尝试一: 使用占位符(#{})
<select id = "queryCount" resultType="int" > select count(*) from t_user <where> <if test = "queryText!= null"> loginacct like '%#{queryText}%' </if> </where> </select>
运行结果:
Preparing: select count(*) from t_user WHERE loginacct like '%?%'
com.microsoft.sqlserver.jdbc.SQLServerException: 1 Index out of range.
Query results : When using placeholders fuzzy query can not put a question mark on the inside quotation marks, the location of the question mark must be at the same level with the field name; ( do not understand )
Solution: like '%' # {+} + quertText '%' (run successfully)
<select id = "queryCount" resultType="int" >
select count(*)
from t_user
<where>
<if test = "queryText!= null"> loginacct like '%' + #{queryText}+ '%' </if>
</where>
</select>
Try two: using EL expressions (ran successfully) <the SELECTthe above mentioned id ="queryCount"resultType ="int"> the SELECTCOUNT (*) fromt_user <the WHERE> <IFthe Test ="! = Null queryText"> loginacct like'% $ the queryText}% {'</IF> </WHERE> </SELECT>
problem: using {} $ sql injection can not be prevented
Conclusion: sqlserver fuzzy query, preferentially used like '%' + # {quertText} + '%' operation
sqlserver page fuzzy query example code for themselves after :( forget to check)
<select id = "queryList" resultType="T_user"> select top ${pagesize}* from t_user <where> <if test = "queryText!=null">loginacct like '%' + #{queryText}+ '%' and id not in (select top ${startIndex} id from t_user order by id) order by id </if> </ WHERE > < WHERE > < IF Test = " the queryText == null " > ID Not in ( SELECT Top $} {startIndex ID from T_USER by Order ID) Order by ID </ IF > </ WHERE > </ SELECT >
Note: a single or multiple criteria query, is only one use "and" connection between "where" condition