日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

insert 和 insertSelective的区别

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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>
如果不明白的話提供一個簡單的例子,再結合上面的源碼體會一下

前提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的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。