您现在的位置是:首页 > 电脑 > 

Mybatis中insert和insertSelective区别

2025-07-22 09:09:34
两者的区别在于如果选择insert 那么所有的字段都会添加一遍即使没有值   <insert id=insert parameterType=com.ego.pojo.TbContentCategory > insert into tb_content_category (id, parent_id, name, sta
两者的区别在于如果选择insert 那么所有的字段都会添加一遍即使没有值
  •  

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

     

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

    <insert id=insertSelective parameterType=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=BIGIT},
    
    </if>
    
    <if test=parentId != null >
    
    #{parentId,jdbcType=BIGIT},
    
    </if>
    
    <if test=name != null >
    
    #{name,jdbcType=VARCHAR},
    
    </if>
    
    <if test=status != null >
    
    #{status,jdbcType=ITEGER},
    
    </if>
    
    <if test=sortOrder != null >
    
    #{sortOrder,jdbcType=ITEGER},
    
    </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.setame(手机);

  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.setame(冰箱);

  12. insert(g)

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

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

  15. 注意:price也在哦!!

转自:https:///hello_word2/article/details/80560725

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/549514.html

相关标签:无
上传时间: 2023-06-29 02:04:11
留言与评论(共有 15 条评论)
本站网友 代理服务器ip
3分钟前 发表
</if> <if test=isParent != null > is_parent
本站网友 驼峰鼻整形费用
2分钟前 发表
两者的区别在于如果选择insert 那么所有的字段都会添加一遍即使没有值   <insert id=insert parameterType=pojo.TbContentCategory > insert into tb_content_category (id
本站网友 中国航大
22分钟前 发表
jdbcType=TIMESTAMP}) </insert>   inserSelective就会只给有值的字段赋值   <insert id=insertSelective parameterType=pojo.TbContentCategory > insert into tb_content_category <trim prefix=( suffix=) suffixOverrides=
本站网友 淘美网
23分钟前 发表
jdbcType=BIGIT}
本站网友 太原市长耿彦波
19分钟前 发表
</if> <if test=sortOrder != null > #{sortOrder
本站网友 抗肿瘤药
19分钟前 发表
</if> <if test=created != null > created
本站网友 太冲准确位置图和作用
22分钟前 发表
#{sortOrder
本站网友 西安治疗阳痿
4分钟前 发表
jdbcType=ITEGER}
本站网友 东阿房屋出租
21分钟前 发表
#{name
本站网友 会不会有以后
6分钟前 发表
</if> <if test=parentId != null > parent_id
本站网友 围城内外
29分钟前 发表
#{status
本站网友 jsp开发
19分钟前 发表
如果使用insert则是不论你设置多少个字段,统一都要添加一遍,不论你设置几个字段,即使是一个
本站网友 中国北方工业集团公司
21分钟前 发表
jdbcType=ITEGER}
本站网友 黑木耳的营养价值
14分钟前 发表
jdbcType=TIMESTAMP}