广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatis插入Insert、InsertSelective的区别及使用心得
  • 292
分享到

MyBatis插入Insert、InsertSelective的区别及使用心得

2024-04-02 19:04:59 292人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis插入Insert、InsertSelective的区别这里说一下两者的区别笔者反思但是为什么会有这两种方法呢Mybatis选择插入,选择更新 Update

MyBatis插入Insert、InsertSelective的区别

逆向自动生成的mybatis对应配置Mapper文件里面,有两个方法,分别为insert和insertSelective。这两个方法均是插入对象的方法。为什么会有两个插入方法呢?

这里说一下两者的区别

首先我们看代码如下:

insert方法


<insert id="insert" parameterType="demo.pojo.domain.HspMediaInf" >
    insert into MEDIA_INF (MED_SEQ, EMG_SEQ, MED_CLASS, 
      MED_NAME, MED_FILE, MED_EXTEND, 
      MED_TYPE, MED_DATE, MED_SIZE, 
      MED_DATA)
    values (#{medSeq,jdbcType=VARCHAR}, #{emgSeq,jdbcType=VARCHAR}, #{medClass,jdbcType=VARCHAR}, 
      #{medName,jdbcType=VARCHAR}, #{medFile,jdbcType=VARCHAR}, #{medExtend,jdbcType=VARCHAR}, 
      #{medType,jdbcType=VARCHAR}, #{medDate,jdbcType=TIMESTAMP}, #{medSize,jdbcType=DECIMAL}, 
      #{medData,jdbcType=BLOB})
  </insert>

insertSelective方法


<insert id="insertSelective" parameterType="activetech.zyyhospital.pojo.domain.HspMediaInf" >
    insert into HSP_MEDIA_INF
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="medSeq != null" >
        MED_SEQ,
      </if>
      <if test="emgSeq != null" >
        EMG_SEQ,
      </if>
      <if test="medClass != null" >
        MED_CLASS,
      </if>
      <if test="medName != null" >
        MED_NAME,
      </if>
      <if test="medFile != null" >
        MED_FILE,
      </if>
      <if test="medExtend != null" >
        MED_EXTEND,
      </if>
      <if test="medType != null" >
        MED_TYPE,
      </if>
      <if test="medDate != null" >
        MED_DATE,
      </if>
      <if test="medSize != null" >
        MED_SIZE,
      </if>
      <if test="medData != null" >
        MED_DATA,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="medSeq != null" >
        #{medSeq,jdbcType=VARCHAR},
      </if>
      <if test="emgSeq != null" >
        #{emgSeq,jdbcType=VARCHAR},
      </if>
      <if test="medClass != null" >
        #{medClass,jdbcType=VARCHAR},
      </if>
      <if test="medName != null" >
        #{medName,jdbcType=VARCHAR},
      </if>
      <if test="medFile != null" >
        #{medFile,jdbcType=VARCHAR},
      </if>
      <if test="medExtend != null" >
        #{medExtend,jdbcType=VARCHAR},
      </if>
      <if test="medType != null" >
        #{medType,jdbcType=VARCHAR},
      </if>
      <if test="medDate != null" >
        #{medDate,jdbcType=TIMESTAMP},
      </if>
      <if test="medSize != null" >
        #{medSize,jdbcType=DECIMAL},
      </if>
      <if test="medData != null" >
        #{medData,jdbcType=BLOB},
      </if>
    </trim>
  </insert>

从上面连段代码我们会发现insertSelective对应的sql语句加入了NULL校验,只会插入数据不为null的字段值。insert则会插入所有字段,会插入null。

笔者反思但是为什么会有这两种方法呢

总结:由于真实开发中,我们在修改功能的from表单,或者一个类的某些模块编辑的from表单中不会将所有的数据都查询出来放到from表单中然后再insert到数据库,所以我们经常在这些功能中使用insertSelective方法,此时只针对我们操作的属性进行insert操作,而如果使用insert方法,将会把非表单内的内容置为null从而影响数据安全

Mybatis选择插入,选择更新 Update insert Mapper示例


<update id="updateMethod" parameterType="com.zjl.domain">
    update table_name
    <set>
      <if test="id != null">
        id = #{id,jdbcType=BIGINT},
      </if>
      <if test="creater != null">
        creater = #{creater,jdbcType=VARCHAR},
      </if>
      <if test="modifiedDate != null">
        modified_date = #{modifiedDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where task_id = #{taskId,jdbcType=BIGINT}
  </update>

<insert id="insertSelective" parameterType="com.zjl.domain">
    insert into table_name
    <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
    id,
    </if>
    <if test="createDate != null">
    create_date,
    </if>
    <if test="modifiedDate != null">
    modified_date,
    </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
    #{id,jdbcType=BIGINT},
    </if>
    <if test="createDate != null">
    #{createDate,jdbcType=TIMESTAMP},
    </if>
    <if test="modifiedDate != null">
    #{modifiedDate,jdbcType=TIMESTAMP},
    </if>
    </trim>
</insert>

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

--结束END--

本文标题: MyBatis插入Insert、InsertSelective的区别及使用心得

本文链接: https://www.lsjlt.com/news/159036.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作