zoukankan      html  css  js  c++  java
  • 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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    用 ArcMap 发布 ArcGIS Server FeatureServer Feature Access 服务 PostgreSQL 版本
    ArcMap 发布 ArcGIS Server OGC(WMSServer,MapServer)服务
    ArcScene 创建三维模型数据
    ArcMap 导入自定义样式Symbols
    ArcMap 导入 CGCS2000 线段数据
    ArcMap 导入 CGCS2000 点坐标数据
    ArcGis Server manager 忘记用户名和密码
    The view or its master was not found or no view engine supports the searched locations
    python小记(3)操作文件
    pytest(2) pytest与unittest的区别
  • 原文地址:https://www.cnblogs.com/mmh760/p/10945082.html
Copyright © 2011-2022 走看看