当前位置: 首页 > web >正文

MyBatis 获取插入数据后的自增 ID 值

1.说明

<insert>标签中设置useGeneratedKeys="true"keyProperty="id",MyBatis 会自动将自增 ID 赋值给传入参数对象的对应属性。

2.案例说明

2.1.数据传输层

RpCompanyconfigMapper.xml,设置useGeneratedKeys="true" keyProperty="companyId"

    <insert id="insertRpCompanyconfig" parameterType="RpCompanyconfig" useGeneratedKeys="true" keyProperty="companyId">insert into rp_companyconfig<trim prefix="(" suffix=")" suffixOverrides=","><if test="companyName != null">company_name,</if><if test="companyCode != null">company_code,</if><if test="createBy != null">create_by,</if><if test="createTime != null">create_time,</if><if test="updateBy != null">update_by,</if><if test="updateTime != null">update_time,</if><if test="remark != null">remark,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="companyName != null">#{companyName},</if><if test="companyCode != null">#{companyCode},</if><if test="createBy != null">#{createBy},</if><if test="createTime != null">#{createTime},</if><if test="updateBy != null">#{updateBy},</if><if test="updateTime != null">#{updateTime},</if><if test="remark != null">#{remark},</if></trim></insert>

 2.2 数据访问层类

public interface RpCompanyconfigMapper

    public int insertRpCompanyconfig(RpCompanyconfig rpCompanyconfig);

2.3.服务层接口类

public interface IRpCompanyconfigService 

    public int insertRpCompanyconfig(RpCompanyconfig rpCompanyconfig);

2.4.服务层实现类

@Service
public class RpCompanyconfigServiceImpl implements IRpCompanyconfigService 

     */@Overridepublic int insertRpCompanyconfig(RpCompanyconfig rpCompanyconfig){rpCompanyconfig.setCreateTime(DateUtils.getNowDate());int ttt=rpCompanyconfigMapper.insertRpCompanyconfig(rpCompanyconfig);return ttt;}

3.效果,查看companyId

4.扩展

方法二:使用 selectKey 标签(适用于不支持自动返回主键的数据库)

对于不支持自动返回主键的数据库(如 Oracle),可以使用<selectKey>标签获取自增 ID。

xml

<insert id="insertUser" parameterType="User"><selectKey keyProperty="id" resultType="long" order="AFTER">SELECT LAST_INSERT_ID()</selectKey>INSERT INTO user (username, email)VALUES (#{username}, #{email})
</insert>

order="AFTER"表示在 INSERT 语句执行后执行 SELECT LAST_INSERT_ID ()。

方法三:通过注解方式(Java 注解配置)

如果你使用 Java 注解而非 XML 配置,可以这样写:

@Insert("INSERT INTO user (username, email) VALUES (#{username}, #{email})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insertUser(User user);

使用 @Options 注解设置 useGeneratedKeys 和 keyProperty 属性。

注意事项

  1. keyProperty:指定实体类中对应的主键属性名。
  2. resultType:指定返回主键的类型。
  3. order 属性
    • AFTER:适用于 MySQL 等支持自动生成主键的数据库。
    • BEFORE:适用于 Oracle 等需要先获取序列值的数据库。
http://www.xdnf.cn/news/12755.html

相关文章:

  • GoC指令测试卷 A
  • 【AI学习】wirelessGPT多任务无线基础模型摘要
  • 安卓小说阅读软件推荐
  • c++ 静态成员变量
  • JavaScript中的函数总结
  • 人工智能赋能高中学科教学的应用与前景研究
  • Macbook M3 使用 VMware Fusion 安装 openEuler24.03LTS
  • 言思集交流社区(SpringBoot)
  • leetcodeT3170
  • MIT 6.S081 Lab10 mmap
  • java报错ncapp生成主子表单据时报错,CarrierRuntimeException
  • 关于Qt阻断样式继承的解决办法
  • yolov12-区域注意力:让计算机“看见”更智能
  • Java 中 synchronized 和 ReentrantLock 的全面对比解析
  • ELK日志管理框架介绍
  • 在C语言中使用UUID作为AES加密密钥
  • python打卡第47天
  • 快速排序算法详解:从理论到实践的全方位指导
  • 从零开始制作小程序简单概述
  • JavaScript ES6 解构:优雅提取数据的艺术
  • 论文略读:Efficient Reasoning for LLMs through Speculative Chain-of-Thought
  • vue中的派发事件与广播事件,及广播事件应用于哪些场景和一个表单验证例子
  • Android 视图系统入门指南
  • C++常用的企业级日志库
  • 绘制饼图详细过程
  • qt使用笔记二:main.cpp详解
  • STM32的系统滴答定时器简述
  • fast-reid部署
  • LangChain面试内容整理-知识点1:LangChain架构与核心理念
  • 高并发下的缓存击穿/雪崩解决方案