两者的区别在于如果选择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也在哦!!

  


insert和insertSelective插入数据库后,在数据库中的效果是一样的,只是sql语句不同。


insert和insertSelective

insert就是把所有值插入,此时数据库中有default值,default值就不起作用了

insertSelective不会忽略default值。

insert和insertSelective区别的更多相关文章

  1. 使用TK框架中 insert与insertSelective区别

    insertSelective会对字段进行判断再更新(如果为Null就忽略更新),如果你只想插入某些字段,可以用这个方法. insert对你注入的字段全部插入

  2. updateByPrimaryKey和updateByPrimaryKeySelective insert和insertSelective

    这两个update都是使用generator生成的mapper.xml文件中,对dao层的更新操作 updateByPrimaryKey对你注入的字段全部更新(不判断是否为Null) updateBy ...

  3. INSERT IGNORE 与INSERT INTO的区别

      INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以 ...

  4. 表复制语句select into from 与 insert into select 区别鉴赏

    select into from 与 insert into select 区别鉴赏 1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,fi ...

  5. mysql中 REPLACE INTO 和 INSERT INTO 的区别

    mysql中 REPLACE INTO 和 INSERT INTO 的区别 REPLACE INTO 和 INSERT INTO 功能类似,都是像表中插入数据,不同点在于:REPLACE INTO 首 ...

  6. INSERT IGNORE 与INSERT INTO的区别,以及replace的用法

    INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据. 这样就可以保 ...

  7. MongoDB save()方法和insert()方法的区别

    MongoDB save()方法和insert()方法的区别 首先看官方文档怎么说的 Updates an existing document or inserts a new document, d ...

  8. INSERT IGNORE 与 INSERT INTO的区别

    例 insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据: insert ignore into table(name)  select  name from table2 例 ...

  9. MongoDB的save 和insert函数的区别

    mongodb的save和insert函数都可以向collection里插入数据,但两者是有两个区别: 一.使用save函数里,如果原来的对象不存在,那他们都可以向collection里插入数据,如果 ...

随机推荐

  1. P3964 [TJOI2013]松鼠聚会

    传送门 首先题意就是求一个点到所有其他点的切比雪夫距离和最小 考虑枚举所有点作为答案,那么我们需要快速计算切比雪夫距离和,发现不太好算 根据一些奇怪的套路,我们把坐标系变化,把 $(x,y)$ 变成 ...

  2. VSCode使用Remote-SSH远程服务器

    VSCode的Remote-SSH 之前一直使用的xshell5,现在在window上必须要升级方可使用,在mac上没法安装学习版.于是就想着vscode能不能实现这一需求. 微软开发了一个VSCod ...

  3. 使用了框架iframe的页面如何跳出框架

    "window.location.href"."location.href"是本页面跳转. "parent.location.href" 是 ...

  4. 剑指offer-2:斐波那契数列

    二.斐波那契数列 题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 1.递归法 1). 分析 斐波那契数列的标准公式为 ...

  5. spring boot引入thymeleaf导致中文乱码

    加上下面这句代码 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" / ...

  6. GUID在安全中作用及生成方法

    参考改进于http://blog.csdn.net/jcicheng/article/details/743934 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多 ...

  7. 日语能力考试N2级核心词汇必备—副词

                                                                         日语能力考试N2级核心词汇必备—副词 ABAB型的副词 あちこ ...

  8. error: (-215) !empty() in function detectMultiScale

    tips: pip install opencv-python or https://www.lfd.uci.edu/~gohlke/pythonlibs/ 原因确实是找不到 opencv 的 xml ...

  9. ARIMA模型

    ARIMA模型(英语:Autoregressive Integrated Moving Average model),差分整合移动平均自回归模型,又称整合移动平均自回归模型(移动也可称作滑动),时间序 ...

  10. SIP协议 会话发起协议(一)

    会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协同工作,通过Internet控制多媒体通信会话. SIP - 概述 以下是有关SIP的几点注意事项 - S ...