java - Mybatis插入mysql數(shù)據(jù)庫返回自增主鍵的問題?
問題描述
在mapper.xml文件配置如下:
<mapper namespace='com.uiyllong.cims.dao.QuestMapper'> <resultMap type='com.uiyllong.cims.model.Selecter' id='resultSel'><id column='selp_id' property='selpId' /><result column='oid' jdbcType='INTEGER' property='oid' /><result column='content' jdbcType='VARCHAR' property='content' /><result column='selseq' jdbcType='INTEGER' property='selseq' /> </resultMap> <resultMap type='com.uiyllong.cims.model.Quest'><id column='qp_id' jdbcType='INTEGER' property='qpId' /><result column='q_content' jdbcType='VARCHAR' property='content' /><result column='qtype' jdbcType='INTEGER' property='qtype' /><result column='seq' jdbcType='INTEGER' property='seq' /><result column='s_oid' jdbcType='INTEGER' property='sOid' /><collection property='selecters' ofType='com.uiyllong.cims.model.Selecter' column='qseq_id' resultMap='resultSel'></collection> </resultMap>
<!-- 插入問題 --> <insert parameterType='com.uiyllong.cims.model.Quest'useGeneratedKeys='true' keyProperty='qpId'>insert into quest_t<trim prefix='(' suffix=')' suffixOverrides=','> <if test='qpId != null'>qp_id, </if> <if test='content != null'>q_content, </if> <if test='qtype != null'>qtype, </if> <if test='seq != null'>seq, </if> <if test='sOid != null'>s_oid, </if></trim><trim prefix='values (' suffix=')' suffixOverrides=','> <if test='qpId != null'>#{qpId,jdbcType=INTEGER}, </if> <if test='content != null'>#{content,jdbcType=VARCHAR}, </if> <if test='qtype != null'>#{qtype,jdbcType=INTEGER}, </if> <if test='seq != null'>#{seq,jdbcType=INTEGER}, </if> <if test='sOid != null'>#{sOid,jdbcType=INTEGER}, </if></trim> </insert>
然后控制器調用后返回的居然一直是1 ,并沒有實現(xiàn)返回主鍵去網(wǎng)上找了一下 都是這樣加了兩個屬性而已useGeneratedKeys='true' keyProperty='qpId'
問題解答
回答1:你可能理解錯了,mybatis返回主鍵并不是通過返回值的形式,而是通過set到實體的id上。你可以輸出Quest對象的qpId值,查看自增主鍵。
回答2:那你數(shù)據(jù)庫對應的表本身有沒有設置主鍵自增呢?或者支不支持?
相關文章:
1. mysql - 新浪微博中的關注功能是如何設計表結構的?2. android-studio - Android Studio 運行項目的時候一堆警告,跑步起來!?3. MySQL數(shù)據(jù)庫中文亂碼的原因4. angular.js使用$resource服務把數(shù)據(jù)存入mongodb的問題。5. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?6. angular.js - 關于$apply()7. dockerfile - [docker build image失敗- npm install]8. 表單提交驗證,沒反應,求老師指點9. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???10. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?
