zoukankan      html  css  js  c++  java
  • 如何获取数据表中自增主键的值

    一、MySql数据库

      当向数据库中插入一条数据的时候,默认是拿不到自增主键的值的, 需要设置如下两个属性才可以拿到主键值!

      设置userGeneratedKeys属性值为true:使用自动增长的主键。使用keyProperty设置把主键值设置给哪一个属性

    1   <insert id="addEmp" parameterType="com.neuedu.mybatis.bean.Employee" useGeneratedKeys="true" keyProperty="id" databaseId="mysql">
    2     insert into tbl_employee(last_name,email,gender) 
    3     values(#{lastName},#{gender},#{email})
    4   </insert>

    二、Oracle数据库

      1、需要在oracle数据库中创建自增序列

          create sequence emp_seq
          start with 1;

       2、在sql映射文件中配置:

          order="BEFORE" :设置selectKey中包含的语句先执行

          resultType:指定返回类型

          keyProperty:将返回值赋值给指定的列

    1   <!-- Oracle数据库获取主键自增 -->
    2   <insert id="insert" databaseId="oracle">
    3     <selectKey keyProperty="id" resultType="int" order="before">
    4       select emp_seq.nextval from dual
    5     </selectKey>
    6     insert into tbl_emp values(#{id},#{lastName},#{gender},#{email})
    7   </insert>



  • 相关阅读:
    Arch Linux中安装Anaconda
    Windows下使用Diskpart格式化U盘
    Jupyter Notebook的安装
    Docker的脚本安装
    pip无法正常使用卸载并重新安装
    Arch更新时failed to prepare transaction
    Privoxy将Socks代理转化HTTP代理
    Arch Linux下Visual Stdio Code在格式化C代码时报错
    GNOME 3.28 启用桌面图标
    Appium入门(8)__控件定位
  • 原文地址:https://www.cnblogs.com/java-zmj/p/8081831.html
Copyright © 2011-2022 走看看