2016-01-13 61 views
3

我在這裏有一個角落的情況。我想通過註釋配置MyBatis插入到MySql表中,並返回自動遞增鍵。我的方法是隻能用原語雖然不是POJO:MyBatis插入和返回鍵 - 註解/主要/ MySql

@Insert("INSERT into my_list (created_by_id, name) values (#{userId}, #{name})") 
@Options(useGeneratedKeys=true) 
long insert(@Param("userId") long userId, @Param("name") String name); 

如何判斷的MyBatis的關鍵映射回該方法返回參數(而不是一個POJO成員變量)?

,我發現了異常:

org.mybatis.spring.MyBatisSystemException: nested exception is 
org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting 
result to parameter object. 
Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available 
parameters are [userId, name, param1, param2] 
+0

您是否曾經能夠在不使用POJO解決方案的情況下做到這一點? – Rob

回答

0

的MyBatis生成ID並將其設置爲id字段的POJO。但是在這裏你沒有爲mybatis設置id的pojo。 如果你沒有pojo,你可以使用地圖,把所有參數放到地圖上,mybatis會把id放到地圖上。

+0

是的 - 因此,這個問題。 – Kong