insert和insertSelective区别

网友投稿 283 2022-09-22

insert和insertSelective区别

使用逆向工程生成的代码做一个添加时通常都会给出两个答案,如题目想要增加一条数据会让你选择insert或者insertSelective

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

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

但是如果使用inserSelective就会只给有值的字段赋值(会对传进来的值做非空判断)

insert into tb_content_category id, parent_id, name, status, sort_order, is_parent, created, updated, #{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT}, #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP},

如果不明白的话提供一个简单的例子,再结合上面的源码体会一下

前提Goods商品表里面有三个字段:id,name,price 1.此时我只设置了一个字段名字: Goods g = new Goods(); g.setName("手机"); insertSelective(g); insertSelective执行对应的sql语句的时候,只插入对应的name字段; (主键是自动添加的,默认插入为空)insert into tb_goods (id,name) value (null,"手机"); 注意:此时是没有price什么事的 2、如果使用insert则是不论你设置多少个字段,统一都要添加一遍,不论你设置几个字段,即使是一个。 Goods g=new Goods(); g.setName("冰箱"); insert(g) insert执行对应的sql语句的时候,统一都要添加一遍; insert into tb_goods (id,name,price) value (null,"冰箱",null); 注意:price也在哦!!

结束!!!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Framework Runtime
下一篇:休克文案:重新认识下品牌!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~