2017-02-18 461 views
0

我正在寫我的拳頭MyBatis應用程序,並卡住了@Select。我不知道我的@Select定義有什麼問題,一切看起來都很好,但我得到了一個Parameter not found異常。將參數傳遞給MyBatis @Select

當我將參數傳遞給我的@Insert語句並且工作正常時,我遵循了相同的模式。

我使用MyBatis 3.4.2。

這是我的@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); 

唯一的例外是我得到的:

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] 

回答

0

當你傳遞一個參數對象,屬性直接通過getter或鍵訪問設置爲一張地圖。當你想傳遞的方法多個參數,你有註解來命名參數:基於

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

此註釋語法實際上像一個鍵值地圖。密鑰由@Param提供。您爲參數變量選擇的名稱不可見。

0

請嘗試從JDK 8開始提供的編譯選項-parameters。您可以省略@Param註釋。

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

感謝。

+0

你知道如何配置maven和intellij來使用「-parameters」?我將-參數添加到我的maven-compiler-plugin中,但沒有影響。 Intellij:構建,執行,部署>編譯器> Java編譯器>其他命令行參數,但它不起作用。也許我需要花更多的時間在它:( – zappee

+0

Maven: 請參閱https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs。 –

+0

IntelliJ: 你是對的! !不過,您應重建項目(執行 「建設>重建項目」) –