zoukankan      html  css  js  c++  java
  • mybatis 添加一条新数据并返回此数据的ID(主键)

    mybatis 添加一条新数据并返回此数据的ID(主键)


    利用Mybatis 的 selectKey来获得:

    1. <!-- 添加部门 返回部门ID -->  
    2. <insert id="addDept" parameterType="com.demo.model.Department" keyProperty="id">  
    3.     <selectKey keyProperty='id' resultType='int' order='AFTER'  >  
    4.         select LAST_INSERT_ID();  
    5.     </selectKey>  
    6.      insert into department(<include refid="departmentAllField"/>)   
    7.         values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});  
    8.  </insert

    1. <insert id="addDept" parameterType="com.demo.model.Department" useGeneratedKeys="true" keyProperty="id">  
    2.   insert into department(<include refid="departmentAllField"/>)   
    3.    values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});  
    4. </insert>
    注意:insert 标签中的 keyProperty  和  selectKey标签块中的 LAET_INSERT_ID() ,另外 order属性 对于 oracl为 BEFORE; mysql为AFTER
     
    方法三:
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.edu.archive.business.domain.grade.pojo.model.ApplicationRecord" useGeneratedKeys="true">

    insert into bj_application_record (parent_id, class_no, student_name,
    kinsfolk_relation, `status`, refuse_remark,
    create_time, update_time, version
    )
    values (#{parentId,jdbcType=INTEGER}, #{classNo,jdbcType=VARCHAR}, #{studentName,jdbcType=VARCHAR},
    #{kinsfolkRelation,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{refuseRemark,jdbcType=VARCHAR},
    #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{version,jdbcType=INTEGER}
    )
    </insert>

  • 相关阅读:
    关于UltraISO打开iso文件后只有部分文件问题
    CollectionViewController 集合视图
    SDWebImage, 关于第三方异步加载图片的一些方法
    NSFileManager(文件管理类)
    UIReview(UI总结)
    sqlite( 轻量级数据库)
    DataPersistence(数据持久化: 文件读写, NSUserDefault, 归档)
    第三方(SDWebImage, 网络类AFNetWorking)
    网络编程(GET, POST)
    数据解析(SAX, JSON)
  • 原文地址:https://www.cnblogs.com/tubashu/p/14755065.html
Copyright © 2011-2022 走看看