• MyBatis 示例-主键回填


    测试类: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 示例-插件

    求关注,求点赞,《架构学习》持续更新、完善、纠正 https://www.yuque.com/yinjianwei/vyrvkf
  • 相关阅读:
    三年Android开发经验,挥泪整理字节跳动、微软中国凉经,你不看看吗?
    App怎么做才能永不崩溃
    做了八年的Android开发,谁不是一边崩溃,一边默默坚守!
    阿里员工年年绩效A,晒出收入后感叹:996虽然痛苦,发钱时候真香
    2021阅读书单
    不动产测绘概念
    Elasticsearch 集成
    Elasticsearch 环境
    Elasticsearch 优化
    Elasticsearch入门
  • 原文地址:https://www.cnblogs.com/yinjw/p/11757056.html
Copyright © 2020-2023  润新知