zoukankan      html  css  js  c++  java
  • java mybatis中insert 操作 返回主键的小技巧。。。。

    第一种方式:

    在实体类的映射文件 "*Mapper.xml" 这样写:

    <insert id="insertvmatedic" keyColumn="mdid" useGeneratedKeys="true" keyProperty="mdid" parameterType="Vmaterialdictionary">
          insert into vmaterialdictionary values(testsq.nextval,#{mdname},#{mdtype},#{mdunit},#{mdcost})
     </insert>
    keyColumn="mdid"  表示数据库中哪一列
    useGeneratedKeys="true" 表示给主键设置自增长
    keyProperty="mdid"  表示将自增长后的Id赋值给实体类中的mdid字段。
    这里提醒下,<insert></insert> 中没有resultType属性,不要乱加。
    实体类中mdid要有getter() and setter(); 方法

    第二种方式:

    同样在实体类的映射文件 "*Mapper.xml" 但是要这样写:

       <insert id="insertProduct" parameterType="domain.model.ProductBean" >
           <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="productId">
              SELECT LAST_INSERT_ID()
          </selectKey>
            INSERT INTO t_product(productName,productDesrcible,merchantId)values(#{productName},#{productDesrcible},#{merchantId});
        </insert>
    <insert></insert> 中没有resultType属性,但是<selectKey></selectKey> 标签是有的。

    order="AFTER" 表示先执行插入语句,之后再执行查询语句。

    可被设置为 BEFORE 或 AFTER。

    如果设置为 BEFORE,那么它会首先选择主键,设置 keyProperty 然后执行插入语句。

    如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素-这和如 Oracle 数据库相似,可以在插入语句中嵌入序列调用
    keyProperty="userId"  表示将自增长后的Id赋值给实体类中的userId字段。
  • 相关阅读:
    微信开发-微信红包实例;
    微信支付-商户调用支付接口失败,已完成交易接口升级的用户应使用新接口进行交易;
    JS 获取当前时间
    微信支付-退款之CURL 52
    Binniabia is what?
    WIN7 自动同步服务器上备份文件
    WIN7 自动同步服务器上备份文件
    CSS之clearfix清除浮动
    解决IOS iframe不滚动问题
    CSS文字不换行,溢出省略
  • 原文地址:https://www.cnblogs.com/bekeyuan123/p/7440266.html
Copyright © 2011-2022 走看看