您的位置:首页 > 数据库 > Oracle

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

2017-04-12 16:27 591 查看
在使用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的内置函数



通过这个函数可以提升性能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐