mybatismybatis-generator

What is the difference between insert and insertSelective in MyBatis Generator?


insertSelective allows us to insert only into specified columns, but isn't it the same as insert, when we keep an object's attributes null which we do not want to insert into the columns?

For example, if a User

class User{
  Integer id;
  String name;
  String address;
}

What is the difference between insert(User{null,"Jack",null} and insertSelective(User{null,"Jack",null}?


Solution

  • The difference between insert() and insertSelective() is that insert() will insert all columns specified in the object, including null values, while insertSelective() will only insert non-null values.

    So, in your example, if you used insert(User{null,"Jack",null}), it would insert a new row with null values for the id and address columns. If you used insertSelective(User{null,"Jack",null}), it would only insert a new row with the non-null value of "Jack" for the name column.