首页 > 技术文章 > MybatisPlus中insert方法与insertAllColumn方法的区别

ccxyyds 2019-09-30 16:13 原文

实现

insert方法在插入时,会根据实体类的每个属性进行非空判断,只有非空的属性所对应的字段才会出现在SQL语句中。

insertAllColumn方法在插入时,不管属性是否为空,属性所对应的字段都会出现在

SQL语句中。

二者的执行结果是一样的,区别在于,前者会根据实体类的每一个属性值进行一个非空校验,
在插入的sql语句中不会出现实体类属性为空的字段;

测试insert()插入语句

/***
  * 测试insert
  */
 @Test
 public void testInsert() {
  Employee employee = new Employee();
  employee.setName("insert测试");
  employee.setAge(23);
  int result = employeeMapper.insert(employee);
  System.out.println("************************"+result);
  Integer id = employee.getId();
  System.out.println("*********************"+id);
 }

结果:
在这里插入图片描述
测试insertAllColumn()插入:

/***
  * 测试insertAllColumn
  */
 @Test
 public void testInsertAllColumn() {
  Employee employee = new Employee();
  employee.setName("insertAllColumn测试");
  employee.setAge(23);
  int result = employeeMapper.insertAllColumn(employee);
  System.out.println("************************"+result);
  Integer id = employee.getId();
  System.out.println("*********************"+id);
 }

结果:
在这里插入图片描述
数据库中对比效果:
在这里插入图片描述
二者的执行结果是一样的,区别在于,前者会根据实体类的每一个属性值进行一个非空校验,在插入的sql语句中不会出现实体类属性为空的字段;

本文转载自:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89436153

推荐阅读