MyBatis Parameter Not Found

Gonzalo picture Gonzalo · Jan 7, 2019 · Viewed 9.5k times · Source

I'm triying to calling a rest application but I get a 500 error. The problem maybe be on the MyBatis call but still can't fix it.

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

Answer

QiQiLing picture QiQiLing · Jan 7, 2019

@Param annotation has two, one belongs to spring, one belongs to mybatis.Their usage is different.

  • 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.

  • 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.