zoukankan      html  css  js  c++  java
  • 【转】mybatis 自增主键配置

    mybatis自增主键配置(?)

    mybatis进行插入操作时,如果表的主键是自增的,针对不同的数据库相应的操作也不同。基本上经常会遇到的就是Oracle Sequece 和 MySQL 自增主键,至于其他的手动生成唯一主键的问题在这里就不讨论了,这里主要说明下在mybatis中对于自增主键的配置。

    不返回自增主键值

    如果考虑到插入数据的主键不作为其他表插入数据的外键使用,那么可以考虑使用这种方式。

    1. Oracle Sequence 配置

      1. <sql id='TABLE_NAME'>TEST_USER</sql>
      2. <sql id='TABLE_SEQUENCE'>SEQ_TEST_USER_ID.nextval</sql>
      3. <!-- 注意这里直接调用sequence的nextval函数 -->
      4. <insert id="insert" parameterType="User">
      5. insert into <include refid="TABLE_NAME" /> (ID,NAME,AGE)
      6. values ( <include refid="TABLE_SEQUENCE" /> ,#{name}, #{age} )
      7. </insert>

      当插入语句如上配置时,那么针对如下语句

      1. User user = new User();
      2. user.setName("test");
      3. user.setAge(24);
      4. userMapper.insert(user);
      5. System.out.println(user.id); // user.id 为空

      user.id为空,也就是说如上的配置并不能在完成插入操作后将插入时的主键值存放到保存的对象中。

    2. Mysql自增主键配置

      由于mysql数据库中,可以设置表的主键为自增,所以对于Mysql数据库在mybatis配置插入语句时,不指定插入ID字段即可。主键的自增交由Mysql来管理。

      1. <sql id='TABLE_NAME'>TEST_USER</sql>
      2. <!-- 注意这里的插入SQL中是没有指明ID字段的! -->
      3. <insert id="insert" parameterType="User">
      4. insert into <include refid="TABLE_NAME" /> (NAME,AGE)
      5. values (#{name}, #{age} )
      6. </insert>

      同样,针对Mysql如此配置mybaits,插入完成后user.id为空。

    插入后获取自增主键值

    上述的情况能满足大部分情况,但有时候我们会遇到类似一对多的那种表结构,在插入多端数据时,需要获取刚刚保存了的一段的主键。那么这个时候,上述的配置就无法满足需要了。为此我们需要使用mybatis提供的<selectKey />来单独配置针对自增逐渐的处理。

    1. Oracle Sequence 配置

      1. <sql id='TABLE_NAME'>TEST_USER</sql>
      2. <sql id='TABLE_SEQUENCE'>SEQ_TEST_USER_ID.nextval</sql>
      3. <!-- 注意这里需要先查询自增主键值 -->
      4. <insert id="insert" parameterType="User">
      5. <selectKey keyProperty="id" resultType="int" order="BEFORE">
      6. select <include refid="TABLE_SEQUENCE" /> from dual
      7. </selectKey>
      8. insert into <include refid="TABLE_NAME" /> (ID,NAME,AGE)
      9. values ( #{id}, #{name}, #{age} )
      10. </insert>

      当使用了<selectKey />后,在实际的插入操作时,mybatis会执行以下两句SQL:

      1. select SEQ_TEST_USER_ID.nextval from dual; // 语句1
      2. insert into (ID,NAME,AGE) values ( ?, ?, ? ); // 语句2

      在执行插入 语句2 之前,会先执行 语句1 以获取当前的ID值,然后mybatis使用反射调用User对象的setId方法,将 语句1 查询出的值保存在User对象中,然后才执行 语句2 这样就保证了执行完插入后

      1. User user = new User();
      2. user.setName("test");
      3. user.setAge(24);
      4. userMapper.insert(user);
      5. System.out.println(user.id); // user.id 不为空

      user.id是有值的。

    2. Mysql自增主键配置

      针对于Mysql这种自己维护主键的数据库,可以直接使用以下配置在插入后获取插入主键,

      1. <sql id='TABLE_NAME'>TEST_USER</sql>
      2. <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="User">
      3. insert into <include refid="TABLE_NAME" /> ( NAME, AGE )
      4. values ( #{name}, #{age} )
      5. </insert>

      当然,由于Mysql的自增主键可以通过SQL语句

      1. select LAST_INSERT_ID();

      来获取的。因此针对Mysql,Mybatis也可配置如下:

      1. <sql id='TABLE_NAME'>TEST_USER</sql>
      2. <!-- 注意这里需要先查询自增主键值 -->
      3. <insert id="insert" parameterType="User">
      4. <selectKey keyProperty="id" resultType="int" order="BEFORE">
      5. SELECT LAST_INSERT_ID()
      6. </selectKey>
      7. insert into <include refid="TABLE_NAME" /> (ID,NAME,AGE)
      8. values ( #{id}, #{name}, #{age} )
      9. </insert>

      只不过该中配置需要额外的一条查询SQL!

    小结

    1. 当数据插入操作不关心插入后数据的主键(唯一标识),那么建议使用 不返回自增主键值 的方式来配置插入语句,这样可以避免额外的SQL开销.

    2. 当执行插入操作后需要立即获取插入的自增主键值,比如一次操作中保存一对多这种关系的数据,那么就要使用 插入后获取自增主键值 的方式配置.

  • 相关阅读:
    faster with MyISAM tables than with InnoDB or NDB tables
    w-BIG TABLE 1-toSMALLtable @-toMEMORY
    Indexing and Hashing
    MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
    controlling the variance of request response times and not just worrying about maximizing queries per second
    Variance
    Population Mean
    12.162s 1805.867s
    situations where MyISAM will be faster than InnoDB
    1920.154s 0.309s 30817
  • 原文地址:https://www.cnblogs.com/dirgo/p/6805144.html
Copyright © 2011-2022 走看看