今天繼續使用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 大概就是想用這個東西來替代吧