search

Home  >  Q&A  >  body text

java - Mybatis inserts into the mysql database and returns the auto-incrementing primary key?

Configure the mapper.xml file as follows:

<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"
ringa_leeringa_lee2772 days ago754

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-17 10:04:01

    You may have misunderstood. Mybatis returns the primary key not in the form of a return value, but by setting it to the entity's ID. You can output the qpId value of the Quest object to view the auto-incrementing primary key.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-17 10:04:01

    Then do you have the primary key auto-increment set in the table corresponding to your database? Or is it supported?

    reply
    0
  • Cancelreply