zoukankan      html  css  js  c++  java
  • MyBatis插入时候获取自增主键方法

    方法一:(Oralce不支持这种写法)

      useGeneratedkeys 默认值为 false,含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。

      keyProperty 的值必须为数据库中主键且是自动增长的字段。(一般是 id 字段)

    <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
            insert into person(name,pswd) values(#{name},#{pswd})
    </insert>

    方法二:

    <insert id="insert" parameterType="Person">
        <selectKey keyProperty="id" resultType="long">
          select LAST_INSERT_ID()
       </selectKey>
      insert into person(name,pswd) values(#{name},#{pswd})
    </insert>

    如上是个User实体类,有id,name,pswd 三个属性。id为字段增长的主键。在插入操作时如以上的写法主键id就不需要赋值,会自动赋值。

  • 相关阅读:
    Jzoj4721 LCS
    Jzoj4721 LCS
    Bzoj3196 二逼平衡树
    Bzoj3196 二逼平衡树
    Jzoj4715 树上路径
    Jzoj4715 树上路径
    031下一个排列
    汉诺塔问题【递归】
    求全排列和组合问题
    030串联所有单词并匹配
  • 原文地址:https://www.cnblogs.com/tongxuping/p/7833551.html
Copyright © 2011-2022 走看看