java - MyBatis 参数未找到

标签 java spring-mybatis

我正在尝试调用休息应用程序,但收到 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中的用法一致。

关于java - MyBatis 参数未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54070841/

相关文章:

java - 在 pc 上初始化蓝牙连接 android(client) 到 python(server)

java - MyBatis 是否有类似于 Hibernate 中的 "dirty checking mechanism"?

java - 如何在使用注释的同时使用 mybatis 中的 hashmap 将值插入到行中?

java 和 mybatis - 误解一对多关系 - 仅注释

java - 关于BigDecimal

java - Spring Web MVC - 验证单个请求参数

java - 运行程序以监视文件 infiteley

java - 快速重新着色位图

mysql - 计算 SQL 查询执行并将行转换为 mybatis 中的 java 对象所需的时间

java - MyBatis 将结果映射到 MyBatisRepository 中的 List<String>