zoukankan      html  css  js  c++  java
  • sqlserver 分页模糊查询

                                      积少成多 ----  仅以此致敬和我一样在慢慢前进的人儿

    问题: 在sqlserver 进行模糊查询,出现问题

    最初使用“concat”,进行拼串操作,如下所示:
    <select id = "queryCount" resultType="int" > select count(*) from t_user <where> <if test = "queryText!= null"> loginacct like concat("%", #{queryText}, "%) </if> </where> </select>

    运行结果concat” 是不是可以识别的函数,不是内置函数

    查询结果: “concat”方法进行拼串,只适用于mysql 和 oracle, 不适用于sqlserver,这个函数是用来连接字符串,                            sqlserver中没有,可以使用 + 连接符号搞定

    解决方案:使用传统的拼串操作,“like”操作尝试:

    尝试一: 使用占位符(#{})
    <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 超出范围。

      查询结果:在使用占位符进行模糊查询时不能把问号放在引号里面,问号的位置必须跟字段名同级;(不理解

      解决方案:like '%' + #{quertText} + '%’(成功运行)

    <select id = "queryCount" resultType="int" >
    select count(*)
    from t_user
    <where>
    <if test = "queryText!= null"> loginacct like '%' + #{queryText}+ '%' </if>
    </where>
    </select>
    尝试二: 使用EL表达式(成功运行)
     <select id = "queryCount" resultType="int" >
            select count(*)
            from t_user
            <where>
                <if test = "queryText!= null"> loginacct like '%${queryText}%' </if>
            </where>
       </select>
    存在问题:使用 ${}无法防止sql注入   

      结论:sqlserver 模糊查询中, 优先使用 like '%' + #{quertText} + '%’进行操作

    sqlserver 分页模糊查询例码:(供自己忘记后查看)

    <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 = "queryText==null">
                id not in
                (select top ${startIndex} id from  t_user order by id)
                order by id
            </if>
        </where>
    </select>

    注意事项: 单个或者是多个条件查询,都是只有一个“where” 条件之间使用 “and” 连接

    2020 / 03 / 12 

    今天做模糊查询的时候, 回来看发现使用上面的不行,折腾了半天, 双引号换成单引号就可以,好奇怪

    <select id = "productFuzzyQuery" parameterType="String" resultType="com.wchat.secondhand.entity.Items">
            select
                id, address, goods, description, price, location
            from
                t_item
            where
                goods
            like
               '%' + #{product} + '%'
        </select>

    很奇怪,上面原来是可以的,今天测却又是不行,哎

    2020 -3-19

    今天使用分页,依据上面的写法, 但是报错显示 

    ibatis Cause: com.microsoft.sqlserver.jdbc.SQLServerException: '@P0' 附近有语法错误

    查询资料:

      https://blog.csdn.net/niqikun/article/details/49047153

          https://blog.csdn.net/SUN_song520/article/details/50482178?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

      http://yayihouse.com/yayishuwu/chapter/1765

     

    又是一件很奇怪的事情

  • 相关阅读:
    位置控制
    Scaleform结合C++编程
    使用定点缓存进行绘制
    纹理
    动态规划:背包问题
    希尔排序
    折半插入排序
    快速排序
    上楼梯算法
    归并排序
  • 原文地址:https://www.cnblogs.com/helloqiufei/p/11183302.html
Copyright © 2011-2022 走看看