关于mybatis中基本类型条件判断问题

今天继续使用mybatis搭建我的网站,在sql这块有个问题

在sql的mapper中如下方式
<
select id="countByCate" parameterType="int" resultType="int"> select count(*) from article where <if test="cate!=0"> cate = #{cate} and </if> deletetype=0; </select>
接口方法 :int countByCate(int cate);

按照上述方式提供服务时会报如下错误:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'cate' in 'class java.lang.Integer'
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
    at $Proxy11.selectOne(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:95)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
    at $Proxy12.countByCate(Unknown Source)

这是说我的参数中没有get方法?

据说这个是用ognl来判断的 ,这个问题的解决方法在我看来只能用hashmap啦。

其实mybatis里有对这个进行处理

只需要吧接口改成如下形式就ok啦:

int countByCate(@Param(value = "cate") int cate);

在参数中指定一下 param 。 大功告成,单元测试顺利执行。

据说mybatis要在下一个版本中去掉hashmap 大概就是想用这个东西来替代吧

 

posted @ 2013-02-23 22:09  jiang nan  阅读(14688)  评论(1编辑  收藏  举报