为了账号安全,请及时绑定邮箱和手机立即绑定

Mybatis插入mysql数据库返回自增主键的问题?

Mybatis插入mysql数据库返回自增主键的问题?

跃然一笑 2019-03-01 11:06:53
在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 id="BaseResultMap" 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 id="insertSelective" 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"
查看完整描述

2 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

你可能理解错了,mybatis返回主键并不是通过返回值的形式,而是通过set到实体的id上。你可以输出Quest对象的qpId值,查看自增主键。

查看完整回答
反对 回复 2019-03-01
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

那你数据库对应的表本身有没有设置主键自增呢?或者支不支持?

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 492 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号