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

MyBatis 示例-主键回填的更多相关文章

  1. MyBatis中主键回填的两种实现方式

    主键回填其实是一个非常常见的需求,特别是在数据添加的过程中,我们经常需要添加完数据之后,需要获取刚刚添加的数据 id,无论是 Jdbc 还是各种各样的数据库框架都对此提供了相关的支持,本文我就来和和大 ...

  2. 3、SpringBoot+Mybatis整合------主键回填

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870 ...

  3. MyBatis返回主键,MyBatis Insert操作返回主键

    MyBatis返回主键,MyBatis Insert操作返回主键 >>>>>>>>>>>>>>>>> ...

  4. 2017.9.15 mybatis批量插入后实现主键回填

    参考来自:mybatis mysql 批量insert 返回主键 注意:必须要在mybatis3.3.1及其以上才能实现. 1.service List branchEntryList = (Arra ...

  5. 开启事务时mybatis返回主键id

    先说一下没有注解的 先给出实体类: public class City { private int city_id; private String city_name; public int getC ...

  6. postgresql + mybatis insert主键自增方法

    postgresql + mybatis插入记录时设置自增主键方法: 一.数据库设置主键自增 1.数据库中id字段选择serial4类型后,会在默认值中生成 nextval('app_id_seq': ...

  7. mybatis plus 主键生成 Twitter雪花算法 id 及修改id为字符型

    mybatis plus配置主键生成策略为2,就是 使用Twitter雪花算法 生成id spring boot中配置为: GlobalConfiguration conf = new GlobalC ...

  8. Mybatis设置主键自增

    <insert id="insertArea" useGeneratedKeys="true" keyProperty="areaId" ...

  9. MyBatis返回主键

    网上给的例子都很简单 , 只要用useGeneratedKey就行了. @Insert({ "INSERT INTO money_record_increasement (id, creat ...

随机推荐

  1. css定位中的百分比

    ----转载自自己在牛人部落中的相关文章--- 在前端css定位中经常面对的一个问题是,百分比定位究竟是针对于谁定位? 一.margin,padding的百分比 首先从css的设计意图说起,在浏览器默 ...

  2. 跨域方案JSONP与CORS的各自优缺点以及应用场景

    转自 https://www.zhihu.com/question/41992168/answer/217903179 首先明确:JSONP与CORS的使用目的相同,并且都需要服务端和客户端同时支持, ...

  3. java课后实验性问题3

    一 .生成随机数 import java.util.*; public class Test1 { public static void main(String[] args) { //建立一个生产随 ...

  4. appStore上传苹果应用程序软件发布

    首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution(1)图中加号是灰色 ...

  5. socket常见问题

    socket编程中主动关闭VS被动关闭 tcp中server,client都可能是主动关闭方或者被动关闭方,现阐述下两者之间的关系: 客户端(client)                       ...

  6. gateway启动报错:org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found

    将pom.xml中关于spring-boot-start-web模块的jar依赖去掉. 错误分析: 根据上面描述(Description)中信息了解到GatewayAutoConfiguration这 ...

  7. Python 标准库之 fcntl

    在 linux 环境下用 Python 进行项目开发过程中经常会遇到多个进程对同一个文件进行读写问题,而此时就要对文件进行加锁控制,在 Python 的 linux 版本下有个 fcntl 模块可以方 ...

  8. 移植Fatfs文件系统到工程中

    下载Fatfs文件管理系统:http://elm-chan.org/fsw/ff/archives.html 下载最新版本 在工程中新建Fatfs文件夹,把fatfs文件中的全部复制过来 由于Fatf ...

  9. 使用mybatis的resultMap进行复杂查询

        记录下mybatis的集合查询中碰到的问题 https://jaychang.iteye.com/blog/2357143   MyBatis ofType和javaType区别 https: ...

  10. Linux 系统中用Systemd 管理系统服务

    Systemd  命令详解: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-syste ...