找不到 MyBatis 参数

MyBatis Parameter Not Found

我正在尝试调用休息应用程序,但出现 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.execute(RelacioServiceImpl.java:61)\r\n\tat es.bcn.idj.representaciorest.business.impl.RelacioServiceImpl.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.Their,用法不同

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