Mybatis 文档篇 3.2:Mapper XML 之 ins

2019-03-23  本文已影响0人  兆雪儿

1 Mapper XML

2 insert, update and delete

2.1 insert, update 和 delete 的属性

The data modification statements insert, update and delete are very similar in their implementation:
数据变更语句 insert、update 和 delete 是很相似的:

<insert
  id="insertAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  keyProperty=""
  keyColumn=""
  useGeneratedKeys=""
  timeout="20">

<update
  id="updateAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  timeout="20">

<delete
  id="deleteAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  timeout="20">

insert, update 和 delete 语句的例子:

<insert id="insertAuthor">
  insert into Author (id,username,password,email,bio)
  values (#{id},#{username},#{password},#{email},#{bio})
</insert>

<update id="updateAuthor">
  update Author set
    username = #{username},
    password = #{password},
    email = #{email},
    bio = #{bio}
  where id = #{id}
</update>

<delete id="deleteAuthor">
  delete from Author where id = #{id}
</delete>

2.2 insert 额外的属性和子元素

As mentioned, insert is a little bit more rich in that it has a few extra attributes and sub-elements that allow it to deal with key generation in a number of ways.
如上所述,insert 语句的配置规则更加丰富,它里面有几个额外的属性和子元素可以使它有几种方式来处理主键生成。

2.2.1 useGeneratedKeys

First, if your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property and you're done. For example, if the Author table above had used an auto-generated column type for the id, the statement would be modified as follows:
首先,如果你的数据库支持主键自动生成(如 MySQL 和 SQL Server),你可以简单地设置 useGeneratedKeys="true",然后再把 keyProperty 设置到目标属性上就可以了。例如,如果上面的 Anthor 表使用了一个主键自动生成的列 id,那么语句可以修改为下面这样:

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
  insert into Author (username,password,email,bio)
  values (#{username},#{password},#{email},#{bio})
</insert>

If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys.
如果你的数据库也支持多行插入,你可以传入一个 list 或者一个 Anthor 数组,并且返回自动生成的主键。

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
  insert into Author (username, password, email, bio) values
  <foreach item="item" collection="list" separator=",">
    (#{item.username}, #{item.password}, #{item.email}, #{item.bio})
  </foreach>
</insert>

2.2.2 selectKey

MyBatis has another way to deal with key generation for databases that don't support auto-generated column types, or perhaps don't yet support the JDBC driver support for auto-generated keys.
对于不支持自动生成主键的数据库或者目前不支持自动生成主键的 JDBC 驱动,MyBatis 还有另外一个方式来处理数据库的主键生成。

Here's a simple (silly) example that would generate a random ID (something you'd likely never do, but this demonstrates the flexibility and how MyBatis really doesn't mind):
这是一个生成随机 ID 的简单(甚至很傻)的例子(你最好不要这么做,这里仅是为了展示 MyBatis 的灵活度和 MyBatis 关心的范围)。

<insert id="insertAuthor">
  <selectKey keyProperty="id" resultType="int" order="BEFORE">
    select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
  </selectKey>
  insert into Author
    (id, username, password, email,bio, favourite_section)
  values
    (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>

In the example above, the selectKey statement would be run first, the Author id property would be set, and then the insert statement would be called. This gives you a similar behavior to an auto-generated key in your database without complicating your Java code.
在上面的例子中,selectKey 语句会被先运行,Author 的 id 属性会被设置,之后 insert 语句才会被调用。这提供给你一个不使 Java 代码变复杂的同时与数据库自动生成主键功能类似的方式。

selectKey 的属性:

<selectKey
  keyProperty="id"
  resultType="int"
  order="BEFORE"
  statementType="PREPARED">

最后

说明:MyBatis 官网提供了简体中文的翻译,但个人觉得较为生硬,甚至有些地方逻辑不通,于是自己一个个重新敲着翻译的(都不知道哪里来的自信...),有些地方同官网翻译有出入,有些倔强地保留了自己的,有的实在别扭则保留了官网的,这些都会在实践中一一更正。鉴于个人英文能力有限,文章中保留了官方文档原英文介绍(个别地方加以调整修剪),希望有缘看到这里的朋友们能够有自己的理解,不会被我可能错误或不合理的翻译带跑偏(〃'▽'〃),欢迎指正!

当前版本:mybatis-3.5.0
官网文档:MyBatis
官网翻译:MyBatis 简体中文
项目实践:MyBatis Learn

上一篇 下一篇

猜你喜欢

热点阅读