insert 和 insertSelective的区别
使用逆向工程生成的代碼做一個添加時通常都會給出兩個答案,如題目想要增加一條數據會讓你選擇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>
如果不明白的話提供一個簡單的例子,再結合上面的源碼體會一下
前提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也在哦!!
--------------------- 
作者:風泊月 
來源:CSDN 
原文:https://blog.csdn.net/hello_word2/article/details/80560725 
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
轉載于:https://www.cnblogs.com/mmh760/p/10945082.html
總結
以上是生活随笔為你收集整理的insert 和 insertSelective的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 景点旅游信息系统——C/C++程序设计、
 - 下一篇: 光纤色散是什么?如何色散补偿?