Mybatis中insert和insertSelective区别

两者的区别在于如果选择insert 那么所有的字段都会添加一遍即使没有值
  •  

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

     

inserSelective就会只给有值的字段赋值
  •  

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

     

用一个例子说明一下
  1. 前提Goods商品表里面有三个字段:id,name,price

  2. 1.此时我只设置了一个字段名字:

  3. Goods g = new Goods();

  4. g.setName("手机");

  5. insertSelective(g);

  6. insertSelective执行对应的sql语句的时候,只插入对应的name字段;

  7. (主键是自动添加的,默认插入为空)insert into tb_goods (id,name) value (null,"手机");

  8. 注意:此时是没有price什么事的

  9. 2、如果使用insert则是不论你设置多少个字段,统一都要添加一遍,不论你设置几个字段,即使是一个。

  10. Goods g=new Goods();

  11. g.setName("冰箱");

  12. insert(g)

  13. insert执行对应的sql语句的时候,统一都要添加一遍;

  14. insert into tb_goods (id,name,price) value (null,"冰箱",null);

  15. 注意:price也在哦!!

转自:https://blog.csdn.net/hello_word2/article/details/80560725

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值