一、insert和insertSelective的区别

1、如果使用insert,那么每个字段都需要有值,即使传的是空字符串也要传。如果没有值,就会报错:无效的列类型: 1111。

@RunWith(SpringRunner.class)
@SpringBootTest
public class SdjgRestApplicationTests {
    @Resource
    private EntityypMapper entityypMapper;
    @Test
    public void contextLoads() {
        JSONObject json = new JSONObject();
        json.put("coname","天天向上制药");
        json.put("cocode","21234253465346345366");
        json.put("area","");
        json.put("address","");
        json.put("longitude","");
        json.put("latitude","");
        json.put("scaddress","");
  //    json.put("businessscope","");
        Entityyp entityyp = json.toJavaObject(Entityyp.class);
        String id = UUID.randomUUID().toString();
        entityyp.setId(id);
        entityyp.setAppKey("zwh");
        entityyp.setCreateTime(new Date());
        entityyp.setUpdateTime(new Date());
        entityypMapper.insert(entityyp);
    }
}

结果:报错

2、如果是insertSelective,那么不是每个字段都需要有值,即使不传值,也能成功插入。

我们将businessscope不传值进行测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SdjgRestApplicationTests {
    @Resource
    private EntityypMapper entityypMapper;
    @Test
    public void contextLoads() {
        JSONObject json = new JSONObject();
        json.put("coname","天天向上制药");
        json.put("cocode","21234253465346345366");
        json.put("area","");
        json.put("address","");
        json.put("longitude","");
        json.put("latitude","");
        json.put("scaddress","");
//        json.put("businessscope","");
        Entityyp entityyp = json.toJavaObject(Entityyp.class);
        String id = UUID.randomUUID().toString();
        entityyp.setId(id);
        entityyp.setAppKey("zwh");
        entityyp.setCreateTime(new Date());
        entityyp.setUpdateTime(new Date());
        entityypMapper.insertSelective(entityyp);
    }
}

插入成功。

3、网上对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>

二、updateByPrimaryKey与updateByPrimaryKeySelective的区别

1、updateByPrimaryKeySelective只能更新不为空的字段,如果有些字段没有传值或者传空字符串,也能执行成功。当表中原来有值的字段,如果不传值进行修改,则无法删除。

@RunWith(SpringRunner.class)
@SpringBootTest
public class SdjgRestApplicationTests {
    @Resource
    private EntityypMapper entityypMapper;
    @Test
    public void contextLoads() {
        JSONObject json = new JSONObject();
        json.put("coname","天天向上制药");
        json.put("cocode","21234253465346345377");
        json.put("area","");
        json.put("address","");
        json.put("longitude","");
        json.put("latitude","");
        json.put("scaddress","");
//        json.put("businessscope","");
        Entityyp entityyp = json.toJavaObject(Entityyp.class);
        entityyp.setId("5a128102-b59d-4188-86f8-a091dfcea2de");
        entityyp.setAppKey("zwh1");
        entityyp.setCreateTime(new Date());
        entityyp.setUpdateTime(new Date());
        entityypMapper.updateByPrimaryKeySelective(entityyp);
    }
}

修改成功

2、updateByPrimaryKey每个字段都需要有值,即使是空字符串,如果没有传值,则会报错。

@RunWith(SpringRunner.class)
@SpringBootTest
public class SdjgRestApplicationTests {
    @Resource
    private EntityypMapper entityypMapper;
    @Test
    public void contextLoads() {
        JSONObject json = new JSONObject();
        json.put("coname","天天向上制药");
        json.put("cocode","21234253465346345377");
        json.put("area","");
        json.put("address","");
        json.put("longitude","");
        json.put("latitude","");
        json.put("scaddress","");
//        json.put("businessscope","");
        Entityyp entityyp = json.toJavaObject(Entityyp.class);
        entityyp.setId("5a128102-b59d-4188-86f8-a091dfcea2de");
        entityyp.setAppKey("zwh");
        entityyp.setCreateTime(new Date());
        entityyp.setUpdateTime(new Date());
        entityypMapper.updateByPrimaryKey(entityyp);
    }
}

报错:无效的列类型: 1111

不仅可以更新不为空的字段,如果某个字段你传递的是空字符串,则原来的表中存在的值被删除。

如:如果原来businessscope有值,此时给businessscope传递一个空字符串过去,那么businessscope原来的值会被清空

@RunWith(SpringRunner.class)
@SpringBootTest
public class SdjgRestApplicationTests {
    @Resource
    private EntityypMapper entityypMapper;
    @Test
    public void contextLoads() {
        JSONObject json = new JSONObject();
        json.put("coname","天天向上制药");
        json.put("cocode","21234253465346345377");
        json.put("area","");
        json.put("address","");
        json.put("longitude","");
        json.put("latitude","");
        json.put("scaddress","");
        json.put("businessscope","");
        Entityyp entityyp = json.toJavaObject(Entityyp.class);
        entityyp.setId("5a128102-b59d-4188-86f8-a091dfcea2de");
        entityyp.setAppKey("zwh1");
        entityyp.setCreateTime(new Date());
        entityyp.setUpdateTime(new Date());
        entityypMapper.updateByPrimaryKey(entityyp);
    }
}

发现businessscope被清空。

总结:

1)、没有带selective的话,就必须传值,即使是空字符串(不能是null)。如果带了selective,那么不管传不传值,都能执行成功。

2)、对于updateByPrimaryKey,当传递空字符串的时候,会将换来存在的值清空。

posted on 2021-10-13 16:31  周文豪  阅读(1002)  评论(0编辑  收藏  举报