简体   繁体   English

MyBatis 参数未找到

[英]MyBatis Parameter Not Found

I'm triying to calling a rest application but I get a 500 error.我正在尝试调用 rest 应用程序,但出现 500 错误。 The problem maybe be on the MyBatis call but still can't fix it.问题可能出在 MyBatis 调用上,但仍然无法解决。

This is where I call the execution of 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);
        }
    });

The error that I'm getting is我得到的错误是

{
"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

But I debugged and saw that the variable that appears to be the problem is filled correctly so why is MyBatis not founding the variable?但是我调试并看到似乎是问题的变量已正确填充,那么为什么 MyBatis 没有创建该变量?

@Param annotation has two, one belongs to spring, one belongs to mybatis.Their usage is different. @Param注释有两个,一个属于spring,一个属于mybatis,它们的用法不同。

  • org.springframework.data.repository.query.Param 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/> 
    It's based on the order of the parameters, and starts from 0. 它基于参数的顺序,从0开始。

  • org.apache.ibatis.annotations.Param 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/> 
    Is based on the parameter name. 基于参数名称。

So, check that the annotations you introduced in mapper.java are consistent with the usage in mapper.xml. 因此,请检查您在mapper.java中引入的注释是否与mapper.xml中的用法一致。

Check whether value that is subject to some condition in Mybatis are sent from the Mapper class.检查Mybatis中符合条件的值是否是从Mapper class发送过来的。 I was facing the same issue due to this code由于此代码,我面临同样的问题

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

The problem was I hadn't sent fruit from Mapper Class.问题是我没有从 Mapper Class 发送水果。 Adding @Param("fruit") in the mapper class fixed the issue for me.在映射器 class 中添加@Param("fruit")为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM