zoukankan      html  css  js  c++  java
  • mybatis 主键回显

    1.如果要插入数据的表的主键字段有自增的规则

    示例:

    <insert id="save"  keyProperty="id" useGeneratedKeys="true" parameterType="student">
        INSERT into<include refid="tableName"/>(studentName,phone,birth) 
        VALUE (#{studentName},#{phone},#{birth});
    </insert>

    用法:
          在<insert>标签中添加keyProperty=“主键字段”,useGeneratedKeys=”true”

    字段解析:
           keyProperty:表示指定的属性作为主键

          useGeneratedKeys:如果为true,会使mybatis使用Jdbc的getGeneratedKeys()的方法来获取数据库内部生成得到主键。例如,mysql和sql server自动递增的字段,oracle的序列等,但是使用它的前提是必须要给出KeyProperty属性


    2.自定义主键规则,并回填

    假如我们要插入数据的表没有设置主键自增的规则,但是我们也要实现上面通样的效果,这时我们可以自己定义自增的规则。

    示例:如果表里没有记录,主键初始值为1,如果存在记录,每次+2递增

    <insert id="save"  keyProperty="id" useGeneratedKeys="true" parameterType="student">
            <selectKey keyProperty="id" resultType="int" order="BEFORE">
                select if(max(id) is null,1,max(id)+2)  from student;
            </selectKey>
            INSERT into<include refid="tableName"/>(id,studentName,phone,birth) 
            VALUE (#{id},#{studentName},#{phone},#{birth});
    </insert>

    讲解:这里用到了selectKey子标签

  • 相关阅读:
    记忆化搜索 E
    网络流 O
    线段树 B数据结构 牛客练习赛28
    N
    线段树 G
    K
    F
    补一下昨天的博客 J
    selenium-1-python
    selenium入门知识
  • 原文地址:https://www.cnblogs.com/chenny3/p/10226172.html
Copyright © 2011-2022 走看看