欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

MybatisMapperXML文件-插入,更新,删除详解(insert,updateanddelete),

来源: javaer 分享于  点击 23458 次 点评:209

MybatisMapperXML文件-插入,更新,删除详解(insert,updateanddelete),


目录
  • Mybatis Mapper XML文件-插入,更新,删除(insert, update and delete)
    • 数据修改语句(插入、更新和删除)在实现上非常相似
    • Insert, Update and Delete 属性
    • 插入、更新和删除语句的示例
      • sql
  • 总结

    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>

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持3672js教程。

    您可能感兴趣的文章:
    • MyBatis Mapper.xml入参List使用in函数问题
    • Mybatis mapper.xml使用全局变量的三种实现方法
    • MyBatis Mapper.XML 标签使用小结
    • mybatis-config.xml文件中的mappers标签使用
    • Mybatis映射文件详解之mapper.xml文件
    • 修改SpringBoot 中MyBatis的mapper.xml文件位置的过程详解
    相关栏目:

    用户点评