首页 文章

获取mybatis中最后插入记录的ID

提问于
浏览
7

我是mybatis的新手 . 我想获取最后插入记录的ID . 我的数据库是mysql,我的mapper是xml

<insert id="insertSelective"  parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" >
  <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
  SELECT LAST_INSERT_ID() as id
</selectKey>
 insert into fileAttachment
<trim prefix="(" suffix=")" suffixOverrides="," >
  <if test="name != null" >
    name,
  </if>
  <if test="attachmentFileSize != null" >
    size,
  </if>      
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
  <if test="name != null" >
    #{name,jdbcType=VARCHAR},
  </if>
 <if test="attachmentFileSize != null" >
    #{attachmentFileSize,jdbcType=INTEGER},
  </if>
 </trim>
 </insert>

我认为这里写的语句'SELECT LAST_INSERT_ID()as id'应该返回最后插入记录的id但是我在插入记录后总是1 .

我的mapper.java类我有方法

int insertSelective(FileAttachment record);

在我正在使用的dao课程中

int id = fileAttachmentMapper.insertSelective(fileAttachment);

插入新记录时,我的ID值始终为1 . 我的Id字段自动递增,记录正确插入 .

7 回答

  • 6

    id被注入对象:

    int num_of_record_inserted = fileAttachmentMapper.insertSelective(fileAttachment);
    
    int id = fileAttachment.getId();
    

    selectKey 的作用是在您插入的对象中设置id,在本例中为 fileAttachment ,在其属性 id 中插入AFTER记录 .

  • 1

    你只需要使用

    <insert id="insert" parameterType="com.mycom.myproject.db.mybatis.model.FileAttachment" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    

    不需要在MyBatis中的insert标记内执行select查询 . 它为插入操作提供了新参数 . 在此定义useGeneratedKeys =“true”,keyProperty =“id”,keyColumn =“id” . 您可以通过以下方式检索id的值

    FileAttachment fileAttachment=fileAttachmentMapper.insertSelective(fileAttachment);
      Integer id=fileAttachment.getId();
    

    使用fileAttachment.getId()是因为在插入标记中定义了keyColumn =“id”,您将获得FileAttachment的fileAttachment引用中的所有返回值 .

  • 0

    事实上,MyBatis已经提供了这个功能 . 您可以使用选项:“useGeneratedKeys”来获取最后一个插入ID .

    以下是MyBatis的解释 . (如果您想了解更详细的信息,可以访问MyBatis官方页面) .

    useGeneratedKeys(仅插入和更新)这告诉MyBatis使用JDBC getGeneratedKeys方法来检索数据库内部生成的密钥(例如,像MySQL或SQL Server这样的RDBMS中的自动增量字段) . 默认值:false

    如果您使用的是xml:

    <insert id="" parameterType="" useGeneratedKeys="true">
    

    如果您使用注释:

    @Insert("your sql goes here")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(FileAttachment fileAttachment) throws Exception;
    

    完成插入操作后,fileAttachment的 setId() method will be invoked, and is set to id of last inserted record . 您可以使用fileAttachment的 getId() 来获取最后一个插入ID .

    我希望这会对你有所帮助 .

  • 4

    我认为返回的1是指更新/插入的记录数 . 我认为id是在传递给insertSelective调用的fileAttachment参数上设置的 .

  • 0

    我希望在作者中,你可以拥有一个自定义复合编写器,你可以在那里获得插入的ID .

  • 0

    (1)添加到Ruju的答案,在insert语句中你需要定义属性useGeneratedKeys = true,parameterType =“object”,keyProperty =“objectId”和keyColumn =“objectId”应该设置相同的主键列名(objectId) )来自存储对象记录的表 . 主键列(objectId)应在数据库表中设置为AUTO_INCREMENT .

    (2)当触发insert语句时,新生成的主键(objectId)将存储在对象中,并且您可以通过使用此方法(object.getObjectId()或object.objectId)访问objectId属性来检索它 . 现在,这应该给出确切的和新生成的主要 . 它对我有用....

  • 14

    使用codeGenerator配置需求:

    <table schema="catalogue" tableName="my_table" >
            <generatedKey column="my_table_id" sqlStatement="JDBC" identity="true" />
            <columnOverride column="my_table_id" isGeneratedAlways="true"/>
        </table>
    

    http://www.mybatis.org/generator/configreference/generatedKey.html

    在代码生成之后,插入包括id字段的自动更新

相关问题