2

I am writing my fist MyBatis application and I stuck around @Select. I do not know what is the problem with my @Select definition, everything seems fine but I got a Parameter not found exception.

I have followed the same pattern when I pass parameters to my my @Insert statement and it works without problem.

I use MyBatis 3.4.2.

This is my @Select:

@Select("SELECT * "
        + "FROM configuration "
        + "WHERE key_name = #{key} AND "
        +       "(#{userId} IS NULL AND user_id IS NULL) OR user_id = #{userId} AND "
        +       "status = 1")
Configuration findByKeyAndUserId(String key, Long userId);

The exception what I got:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.binding.BindingException: Parameter 'key' not found. Available parameters are [arg1, arg0, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'key' not found. Available parameters are [arg1, arg0, param1, param2]
zappee
  • 20,148
  • 14
  • 73
  • 129

2 Answers2

2

When you pass a single parameter object, properties are accessed directly through getter or key set for a map. When you want to pass multiple parameters in the method, you have to name the parameters with annotation:

Configuration findByKeyAndUserId(@Param("key") String key, @Param("userId") Long userId);

This annotation based syntax actually behave like a key-value map. Keys are provided by @Param. The name you choose for parameter variables are not visible.

blackwizard
  • 2,034
  • 1
  • 9
  • 21
2

Please try -parameters compile option provided since JDK 8. You can omit a @Param annotation.

See https://github.com/mybatis/mybatis-3/issues/549

Thanks.

Kazuki Shimizu
  • 399
  • 2
  • 7
  • 1
    Do you know how to configure maven and intellij to use "-parameters"? I added -parameters to my maven-compiler-plugin but there is no impact. Intellij: Build, Execution, Deployment > Compiler > Java Compiler > Additional command line parameters but it does not work. Maybe I need to spend more time on it :( – zappee Feb 21 '17 at 21:24
  • Maven: See https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs. – Kazuki Shimizu Feb 22 '17 at 23:54
  • IntelliJ: You are right !! However you should be rebuild your project. (execute "Build > Rebuild Project"). – Kazuki Shimizu Feb 23 '17 at 00:09
  • Maven: org.apache.maven.plugins maven-compiler-plugin -parameters It's work fine. – Kazuki Shimizu Feb 23 '17 at 00:13
  • Eclipse: Java > Compiler > "Store information about method parameters( usable via reflection) is checked – Kazuki Shimizu Feb 23 '17 at 00:23