当前位置: 代码网 > it编程>编程语言>Java > Mybatis Mapper XML文件-插入,更新,删除详解(insert, update and delete)

Mybatis Mapper XML文件-插入,更新,删除详解(insert, update and delete)

2025年02月13日 Java 我要评论
mybatis mapper xml文件-插入,更新,删除(insert, update and delete)数据修改语句(插入、更新和删除)在实现上非常相似<insert id="inse

mybatis mapper xml文件-插入,更新,删除(insert, update and 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 and delete 属性

attribute(属性)description(描述)
id在该命名空间中的唯一标识符,可用于引用该语句。
parametertype将传递到该语句的参数的完全限定类名或别名。此属性是可选的,因为mybatis可以根据传递给该语句的实际参数计算要使用的typehandler。默认值是未设置的。
parametermap这是一种弃用的引用外部parametermap的方法。请使用内联参数映射和parametertype属性。
flushcache将此设置为true将在调用此语句时刷新第二级缓存和本地缓存。对于插入、更新和删除语句,默认值为true。
timeout这设置了驱动程序在发出请求后等待数据库返回的最长时间(以秒为单位),超过该时间将抛出异常。默认值是unset(依赖于驱动程序)。
statementtype可以选择其中的一个:statement、prepared或callable。这会让mybatis分别使用statement、preparedstatement或callablestatement。默认值为prepared。
usegeneratedkeys(仅适用于插入和更新)这告诉mybatis使用jdbc的getgeneratedkeys方法来检索数据库内部生成的键(例如,在mysql或sql server等关系数据库管理系统中的自增字段)。默认值为false。
keyproperty(仅适用于插入和更新)标识一个属性,mybatis将设置由getgeneratedkeys方法返回的键值,或者由insert语句的selectkey子元素返回的键值。默认值为unset。如果预期有多个生成的列,则可以使用逗号分隔的属性名称列表。
keycolumn(仅适用于插入和更新)设置表中具有生成键的列的名称。这只在某些数据库(如postgresql)中需要,当键列不是表中的第一列时。如果预期有多个生成的列,则可以使用逗号分隔的列名称列表。
databaseid如果配置了databaseidprovider,则mybatis将加载所有没有databaseid属性或databaseid与当前数据库标识匹配的语句。如果在具有和不具有databaseid的情况下找到了相同的语句,则后者将被丢弃。

插入、更新和删除语句的示例

<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>

如前所述,插入语句相对更丰富一些,它具有一些额外的属性和子元素,可以以多种方式处理键的生成。

如果你的数据库支持自动生成键字段(例如mysql和sql server),那么你可以简单地设置usegeneratedkeys="true"并将keyproperty设置为目标属性,就完成了键的自动生成。

例如,如果上面的author表使用了自动生成列类型作为id,则语句将修改如下:

<insert id="insertauthor" usegeneratedkeys="true"
    keyproperty="id">
  insert into author (username,password,email,bio)
  values (#{username},#{password},#{email},#{bio})
</insert>

如果你的数据库也支持多行插入操作,你可以传递一个作者列表或作者数组,并获取自动生成的键值。

<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>

对于那些不支持自动生成列类型或可能还不支持jdbc驱动程序中自动生成键的数据库,mybatis提供了另一种处理键生成的方式。

下面是一个简单(有点傻)的示例,它将生成一个随机的id(实际中可能不太会这样做,但这展示了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>

在上面的示例中,​selectkey​语句将首先执行,author对象的id属性将被设置,然后再调用 ​insert​语句。这样可以实现与数据库自动生成键类似的行为,而不会增加java代码的复杂性。

`selectkey`元素的描述如下:

<selectkey
  keyproperty="id"
  resulttype="int"
  order="before"
  statementtype="prepared">

selectkey 属性

attribute(属性)description(描述)
keyproperty生成的键应该设置在哪个目标属性上。如果期望有多个生成的列,则可以使用逗号分隔的属性名称列表。
keycolumn返回结果集中与属性匹配的列名。如果期望有多个生成的列,则可以使用逗号分隔的列名列表。
resulttype结果的类型。mybatis通常可以自动识别,但为了确保准确性,也可以显式指定类型。mybatis允许使用任何简单类型作为键,包括字符串。如果预期生成多个列,则可以使用包含预期属性的对象或映射。
order此属性可以设置为 before 或 after。如果设置为 before,则先执行 selectkey 语句,将生成的键设置到 keyproperty 上,然后再执行插入语句。如果设置为 after,则先执行插入语句,再执行 selectkey 语句。这在某些数据库(如 oracle)中常见,因为它们可能在插入语句中嵌入了序列调用。
statementtype同上,mybatis支持statement、prepared和callable语句类型,它们分别对应于statement、preparedstatement和callablestatement。

作为一种特殊情况,一些数据库允许insert、update或delete语句返回结果集(例如postgresql和mariadb的returning子句,或ms sql server的output子句)。

这种类型的语句必须被写成<select>来映射返回的数据。

<select id="insertandgetauthor" resulttype="domain.blog.author"
      affectdata="true" flushcache="true">
  insert into author (username, password, email, bio)
  values (#{username}, #{password}, #{email}, #{bio})
  returning id, username, password, email, bio
</select>

sql

该元素可用于定义可重复使用的 sql 代码片段,可以包含在其他语句中。它可以在静态时(在加载阶段)进行参数化。不同属性值可以在包含实例中变化。例如:

<sql id="usercolumns"> ${alias}.id,${alias}.username,${alias}.password </sql>

然后,这个 sql 片段可以被包含在另一个语句中,例如:

<select id="selectusers" resulttype="map">
  select
    <include refid="usercolumns"><property name="alias" value="t1"/></include>,
    <include refid="usercolumns"><property name="alias" value="t2"/></include>
  from some_table t1
    cross join some_table t2
</select>

属性值也可以在包含的`refid`属性或包含子句内的属性值中使用,例如:

<sql id="sometable">
  ${prefix}table
</sql>

<sql id="someinclude">
  from
    <include refid="${include_target}"/>
</sql>

<select id="select" resulttype="map">
  select
    field1, field2, field3
  <include refid="someinclude">
    <property name="prefix" value="some"/>
    <property name="include_target" value="sometable"/>
  </include>
</select>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com