springMVC+Mybatis 在oracle中进行模糊查询

在使用mybatis 进行拼接模糊查询的时候,需要使用字符串连接符,否则会报错。可以参照如下代码示例:

    public JsonResult searchIteminfo(UserInfo userinfo, Map<String, Object> map) {
        try {
            map.put("employeeid",userinfo.getEmployeeinfo().getEmployeeid());
            Page<Iteminfo> pages = PageUtil.setPageObject(map);
            List<IteminfoVO> list =null;
            if(Integer.valueOf(userinfo.getEmployeeinfo().getBranch()) ==(CONST.EMPLOYEEBRANCH.COMPANYMAN)){ //如果是电信
                String localcode = userinfo.getCompanyinfo().getLocalcode();
                map.put("localcode", localcode);
                list = iteminfoMapper.selectHnqcxdItemInfoList(map);
            }else if(Integer.valueOf(userinfo.getEmployeeinfo().getBranch()) == (CONST.EMPLOYEEBRANCH.PROVIDERMAN)){ //如果是外协
                Long providerid = userinfo.getProviderinfo().getProviderid();
                map.put("providerid", providerid);
                list = iteminfoMapper.selectHnqcxdItemInfoList(map);
            }
            if(list==null){
                list  = new ArrayList<IteminfoVO>();
            }
            JsonResult jr = ResultUtil.getJsonResult(true, 0, "查询成功",list);
            return jr;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.getJsonResult(false, 0, "查询失败",null);
        }
    }

将查询关键字包含在map集合中。我们再看xml文件中的拼接sql。

  <select id="selectHnqcxdItemInfoList" parameterType="java.util.Map" resultMap="hnqcxditemlistMap">
    select i.itemid,
          i.itemcode,
          i.itemname,
          i.companyid,
          i.itemtype,
          c.companyname,
          i.subprojectmanid,
          to_char(to_date(i.itemdate, 'yyyymmdd'), 'yyyy-mm-dd') as itemdate,
          (case when i.itemtype=0 then (select title from fieldcheck where tablename='iteminfo' and tablefield='itemtype' and value=0)
                when i.itemtype=1 then (select title from fieldcheck where tablename='iteminfo' and tablefield='itemtype' and value=1)
          end)as itemtypename
     from iteminfo i
     left join companyinfo c
       on c.companyid = i.companyid
     left join itemoperatetrace ip
       on ip.itemid = i.itemid
      and ip.employeeid = #{employeeid,jdbcType=DECIMAL}
     left join iteminfotrace it
      on it.itemid = i.itemid
       and it.employeeid = #{employeeid,jdbcType=DECIMAL}

    where i.isqcxd = 1 and i.ismainitem =0
    <!-- 电信人员查询根据localcode -->
    <if test="localcode!=null">
        and c.localcode like #{localcode,jdbcType=VARCHAR}||'%'
    </if>

    <!--供应商根据providerid -->
    <if test="providerid!=null">
        and exists(select 1 from entrustinfo where itemid=i.itemid and providerid=#{providerid,jdbcType=DECIMAL})
    </if>

    <!-- 搜索条件 -->
    <if test="searchkey!=null and searchkey!=''">
        and (upper(i.itemcode)  like '%'||upper(#{searchkey,jdbcType=VARCHAR})||'%' or upper(i.itemname) like  '%'||upper(#{searchkey,jdbcType=VARCHAR})||'%')
    </if>
  </select>

通过oracle 的upper函数将关键字以及搜索列全部转换成大写。

这里有个注意点

'%'||upper(#{searchkey,jdbcType=VARCHAR})||'%' 

就是入参必须要使用 | | 来拼接参数以及模糊查询的通配符%,否则就会报错。

2在表中数据量比较大的情况下,如果使用like 进行模糊匹配的话,效率会很低。建议在大量数据的情况下采用oralce的内置函数
这里写图片描述
通过这个函数可以提升性能。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值