找不到MyBatis参数

时间:2019-01-07 08:30:12

标签: java spring-mybatis

我正在尝试调用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为什么找不到该变量?

1 个答案:

答案 0 :(得分:0)

@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中的用法一致。