insert and insertSelective difference

The difference is that if you select insert then all the fields will be added again, even if there is no value

 <insert id="insert" parameterType="com.ego.pojo.TbContentCategory" >
    insert into tb_content_category (id, parent_id, name, 
      status, sort_order, is_parent, 
      created, updated)
    values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT}, 
      #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
  </insert>

 


 

But if you use inserSelective will only have to value field assignment (do would be passed in the value of non-empty judgment)

<insert id="insertSelective" parameterType="com.ego.pojo.TbContentCategory" >
    insert into tb_content_category
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="parentId != null" >
        parent_id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="sortOrder != null" >
        sort_order,
      </if>
      <if test="isParent != null" >
        is_parent,
      </if>
      <if test="created != null" >
        created,
      </if>
      <if test="updated != null" >
        updated,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="parentId != null" >
        #{parentId,jdbcType=BIGINT},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=INTEGER},
      </if>
      <if test="sortOrder != null" >
        #{sortOrder,jdbcType=INTEGER},
      </if>
      <if test="isParent != null" >
        #{isParent,jdbcType=BIT},
      </if>
      <if test="created != null" >
        #{created,jdbcType=TIMESTAMP},
      </if>
      <if test="updated != null" >
        #{updated,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

  

If you do not understand, then provide a simple example, combined with the above code to taste

Premise Goods merchandise catalog has three fields: the above mentioned id, name,. Price 
        1. At this point I just set up a field name:   
        Goods new new G = Goods ();   
        g.setName ( "phone"); 
        insertSelective (G); 
        insertSelective execution corresponding sql statement when only inserted into the corresponding name field; 
        (primary key is automatically added default insert blank) insert into tb_goods (id, name ) value (null, " mobile phone"); 
        NOTE: At this point there is no price What's 
        2, if insert is whether you set the number of fields, unity must add it again, whether you set up several fields, even a. 
        G = Goods new new Goods (); 
        g.setName ( "refrigerator"); 
        insert (G) 
        when the insert to perform the corresponding sql statement, unity must be added again; 
        insert INTO tb_goods (the above mentioned id, name,. Price) value (null, "refrigerator", null); 
        Note: price is also Oh! !

  


After the insert and insertSelective inserted into the database, the database in effect is the same, just different sql statement.

 


 

insert和insertSelective

insert is inserted into all of the values, when the database has default values, the default value would be ineffective

insertSelective not ignore the default value.

 

Guess you like

Origin www.cnblogs.com/xinruyi/p/11267202.html