MyBatis 示例-主键回填

本文介绍了在MyBatis中如何处理主键回填,特别是针对自增长列的情况。通过设置`keyProperty`和`useGeneratedKeys`属性,可以方便地获取数据库自动生成的主键ID。同时,对于非自增长列,当有特殊需求时,MyBatis也提供了解决方案。文章还提到了批量新增数据时如何应用这些方法。
摘要由CSDN通过智能技术生成

测试类:com.yjw.demo.PrimaryKeyTest

自增长列

数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了实现的方法。

StudentMapper.xml

<insert id="insertByAutoInc" parameterType="studentDO" keyProperty="id" 
    useGeneratedKeys="true">
    insert into t_student (name, sex, selfcard_no, note)
    values (
        #{name,jdbcType=VARCHAR},
        #{sex,jdbcType=TINYINT},
        #{selfcardNo,jdbcType=BIGINT},
        #{note,jdbcType=VARCHAR}
    )
</insert>

通过配置两个属性(keyProperty、useGeneratedKeys)获取表中生成的自增长主键。

  • keyProperty:表示以哪个列作为属性的主键,不能和 keyColumn 同时使用,如果你是联合主键可以用逗号将其隔开;
  • useGeneratedKeys:这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键,例如,MySQL 和 SQL Server 自动递增字段,Oracle 的序列等,但是使用它就必须要给 keyProperty 或者 keyColumn 赋值。useGeneratedKeys 的取值为布尔值,true/false,默认值为 false;

批量新增数据,也可以采用和上面一样的方式。

<insert id="batchInsertByAutoInc" parameterType="list" keyProperty="id" 
        useGeneratedKeys="true">
  insert into t_student (name, sex, selfcard_no, note)
    values 
    <foreach collection="list" item="item" index="index" separator=",">
        (
            #{item.name,jdbcType=VARCHAR},
            #{item.sex,jdbcType=TINYINT},
            #{item.selfcardNo,jdbcType=BIGINT},
            #{item.note,jdbcType=VARCHAR}
        )
    </foreach>
</insert>
非自增长列

假设我们取消表 t_student 的 id 自增的规则,我们的要求是:如果表 t_student 没有记录,则我们需要设置 id=1,否则我们就取最大 id 加2,来设置新的主键。对于一些特殊的要求,MyBatis 也提供了对应方法。

<insert id="insertByNoAutoInc" parameterType="studentDO">
    <selectKey keyProperty="id" resultType="long" order="BEFORE">
        select if(max(id) is null, 1, max(id) + 2) as newId from t_student
    </selectKey>
    insert into t_student (id, name, sex, selfcard_no, note)
    values (
        #{id,jdbcType=BIGINT},
        #{name,jdbcType=VARCHAR},
        #{sex,jdbcType=TINYINT},
        #{selfcardNo,jdbcType=BIGINT},
        #{note,jdbcType=VARCHAR}
    )
</insert>

批量新增数据,也可以采用和上面一样的方式。

<insert id="batchInsertByNoAutoInc" parameterType="list">
    <selectKey keyProperty="id" resultType="long" order="BEFORE">
        select if(max(id) is null, 1, max(id) + 2) as newId from t_student
    </selectKey>
    insert into t_student (name, sex, selfcard_no, note)
    values
    <foreach collection="list" item="item" index="index" separator=",">
        (
        #{item.name,jdbcType=VARCHAR},
        #{item.sex,jdbcType=TINYINT},
        #{item.selfcardNo,jdbcType=BIGINT},
        #{item.note,jdbcType=VARCHAR}
        )
    </foreach>
</insert>

 

MyBatis 实用篇

MyBatis 概念

MyBatis 示例-简介

MyBatis 示例-类型处理器

MyBatis 示例-传递多个参数

MyBatis 示例-主键回填

MyBatis 示例-动态 SQL

MyBatis 示例-联合查询

MyBatis 示例-缓存

MyBatis 示例-插件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值