软件编程
位置:首页>> 软件编程>> java编程>> MyBatis插入Insert、InsertSelective的区别及使用心得

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

作者:啊~~噙!  发布时间:2023-08-25 04:34:28 

标签:MyBatis,Insert,InsertSelective

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>

来源:https://blog.csdn.net/huluwa10526/article/details/90232350

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com