java - Mybatis插入mysql数据库返回自增主键的问题?
问题描述
在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 ,并没有实现返回主键去网上找了一下 都是这样加了两个属性而已useGeneratedKeys='true' keyProperty='qpId'
问题解答
回答1:你可能理解错了,mybatis返回主键并不是通过返回值的形式,而是通过set到实体的id上。你可以输出Quest对象的qpId值,查看自增主键。
回答2:那你数据库对应的表本身有没有设置主键自增呢?或者支不支持?
相关文章:
1. macos - mac下docker如何设置代理2. angular.js - ng-grid 和tabset一起用时,grid width默认特别小3. apache - 本地搭建wordpress权限问题4. 热切期待朱老师的回复,网页视频在线播放器插件配置错误5. docker 下面创建的IMAGE 他们的 ID 一样?这个是怎么回事????6. java - Spring Mvc全局异常处理器@ControllerAdvice不起作用?7. Whitelabel错误页面发生意外错误(类型=未找到,状态= 404)/WEB-INF/views/home.jsp8. javascript - web网页版app返回上一页按钮在ios设备失效怎么办?安卓上可以,代码如下,请大神帮助,万分感谢。9. Android下,rxJava+retrofit 并发上传文件和串行上传文件的效率为什么差不多?10. css3 - transition属性当鼠标一开的时候设置的时间不起作用
