Mybatis like 模糊查询问题
来源:互联网 发布:网络贷款受法律保护吗 编辑:程序博客网 时间:2024/05/17 23:41
转载自:http://elang0705.iteye.com/blog/1700796
大家好,我只是IT行业的一只菜鸟,最近做的项目要求用Mybatis技术,在做一个模糊查询的时候,遇到点麻烦,经过1天的研究,反复尝试,终于找到问题所在,借此机会与大家分享一下,谢谢!
Mybatis配置如下:
<select id="getAll" resultMap="OaEmplyeeInfoResultMap"
parameterType="com.deppon.oa.module.oaEmplyeeInfo.domain.OaEmplyeeInfo">
<![CDATA[
SELECT
]]>
<include refid="select_param" />
<![CDATA[
FROM OM_EMPLOYEE
]]>
<where>
<if test="empcode!=null">
AND EMPCODE = #{empcode}
</if>
<if test="empname!=null">
AND EMPNAME LIKE #{empname}
</if>
</where>
</select>
Dao层如下:
public List<OaEmplyeeInfo> getAllByName(OaEmplyeeInfo oaEmplyeeInfo,String empname){
List<OaEmplyeeInfo> list=null;
Map map=new HashMap();
map.put("empname", empname);
list=(List<OaEmplyeeInfo>) getSqlSession().selectList("com.deppon.oa.module.oaEmplyeeInfo.domain.OaEmplyeeInfo.getAll", map);
return list;
}
测试代码如下:
public void testQuery(){
List<OaEmplyeeInfo> fos=null;
fos = oaEmplyeeInfoDao.getAllByName(null,"王");
for(OaEmplyeeInfo li:fos){
System.out.println(li.getEmpname());
}
Assert.assertNotNull(fos.get(0).getCardno());
}
发现怎么都查不出来数据,最后查询资料才得知:
原来Mybatis用like进行模糊查询的时候,配置与所用的数据库有关系,总结了下,具体如下:
1、mysql :LIKE CONCAT('%',#{empname},'%' ) 或者 LIKE CONCAT('%',‘${empname}’,'%' )
2、oracle:LIKE '%'||#{empname}||'%'
我用的是oracle 把配置文件改为:
<select id="getAll" resultMap="OaEmplyeeInfoResultMap"
parameterType="com.deppon.oa.module.oaEmplyeeInfo.domain.OaEmplyeeInfo">
<![CDATA[
SELECT
]]>
<include refid="select_param" />
<![CDATA[
FROM OM_EMPLOYEE
]]>
<where>
<if test="empcode!=null">
AND EMPCODE = #{empcode}
</if>
<if test="empname!=null">
AND EMPNAME LIKE '%'||#{empname}||'%'
</if>
</where>
</select>
运行测试,查询OK
- Mybatis like 模糊查询问题
- Mybatis like 模糊查询问题
- mybatis like 模糊查询问题
- Mybatis用like进行模糊查询问题
- Mybatis LIKE 模糊查询
- MyBatis 模糊查询 like %
- Mybatis的like模糊查询
- mybatis做like模糊查询
- mybatis做like模糊查询
- mybatis做like模糊查询
- MyBatis的like模糊查询
- Mybatis中的模糊查询(like)
- mybatis做模糊查询 like
- mybatis做like模糊查询
- Mybatis中like模糊查询
- mybatis做like模糊查询
- mybatis做like模糊查询
- mybatis做like模糊查询
- vim中的加密文件
- glusterfs文件系统性能测试
- from zhucheng大牛的poj题目分类
- 学位论文检测系统的说明与修改
- EGL接口介绍
- Mybatis like 模糊查询问题
- 缘
- POJ 2486 Apple Tree
- Servlet运行机制
- Device mapper
- C++11与编译器支持
- LeetCode 122 Best Time to Buy and Sell Stock II
- VC2010和OpenCV2.4.9中imread这个函数不能正确读入图片的解决
- java内存区域