簡體   English   中英

MyBatis 參數未找到

[英]MyBatis Parameter Not Found

我正在嘗試調用 rest 應用程序,但出現 500 錯誤。 問題可能出在 MyBatis 調用上,但仍然無法解決。

這就是我調用 MyBatis 執行的地方

@Override
public List<IdentitatBDTO> searchIdentitatsRepresentantsByRelacioIdentitatRepresentat(final String representatIdentificador, final Date dateFi) {

    List<Identitat> identitats = myBatisTemplate.execute(RelacioDao.class, new MyBatisDaoCallback<List<Identitat>>() {
        @Override
        public List<Identitat> execute(MyBatisDao dao) {
            return ((RelacioDao) dao).searchIdentitatsRepresentantsByRelacioIdentitatRepresentat(representatIdentificador, dateFi);
        }
    });

我得到的錯誤是

{
"errorUrl": 
 "http://localhost:8080/idjrepresentaciorest/rest/representacio/representants/12340002L",
  "errorMessage": "\r\n### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'representatIdentificador' not found. Available parameters are [1, 0, param1, param2]\r\n### Cause: org.apache.ibatis.binding.BindingException: Parameter 'representatIdentificador' not found. Available parameters are [1, 0, param1, param2]",
  "errorStackTrace": "org.apache.ibatis.exceptions.PersistenceException: \r\n### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'representatIdentificador' not found. Available parameters are [1, 0, param1, param2]\r\n### Cause: org.apache.ibatis.binding.BindingException: Parameter 'representatIdentificador' not found. Available parameters are [1, 0, param1, param2]\r\n\tat org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)\r\n\tat org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)\r\n\tat org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)\r\n\tat org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)\r\n\tat org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)\r\n\tat org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)\r\n\tat com.sun.proxy.$Proxy85.searchIdentitatsRepresentantsByRelacioIdentitatRepresentat(Unknown Source)\r\n\tat es.bcn.idj.representaciorest.business.impl.RelacioServiceImpl$1.execute(RelacioServiceImpl.java:61)\r\n\tat es.bcn.idj.representaciorest.business.impl.RelacioServiceImpl$1.execute(RelacioServiceImpl.java:1)\r\n\tat net.opentrends.openframe.services.persistence.mybatis.template.impl.MyBatisTemplateImpl.execute(MyBatisTemplateImpl.java:64)\r\n\tat

但是我調試並看到似乎是問題的變量已正確填充,那么為什么 MyBatis 沒有創建該變量?

@Param注釋有兩個,一個屬於spring,一個屬於mybatis,它們的用法不同。

  • org.springframework.data.repository.query.Param
     User getUserById(@Param("id") Integer id); <select id="getUserById" resultMap="userMap"> select name,age from user where id=#{0, jdbcType=INTEGER} <select/> 
    它基於參數的順序,從0開始。

  • org.apache.ibatis.annotations.Param
     User getUserById(@Param("id") Integer id); <select id="getUserById" resultMap="userMap"> select name,age from user where id=#{id, jdbcType=INTEGER} <select/> 
    基於參數名稱。

因此,請檢查您在mapper.java中引入的注釋是否與mapper.xml中的用法一致。

檢查Mybatis中符合條件的值是否是從Mapper class發送過來的。 由於此代碼,我面臨同樣的問題

<if test="fruit != null">
    and table.fruit_name = #{fruit}
</if>

問題是我沒有從 Mapper Class 發送水果。 在映射器 class 中添加@Param("fruit")為我解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM