Mybatis中int insertSelective()的相关问题

(转载)

1、selective的意思是:选择性
2、insertSelective--选择性保存数据;
比如User里面有三个字段:id,name,age,password
但是我只设置了一个字段;
User u=new user();
u.setName("张三");
insertSelective(u);
3、insertSelective执行对应的sql语句的时候,只插入对应的name字段;(主键是自动添加的,默认插入为空)
insert into tb_user (id,name) value (null,"张三");
4、而insert则是不论你设置多少个字段,统一都要添加一遍,不论你设置几个字段,即使是一个。
User u=new user();
u.setName("张三");
insertSelective(u);

insert into tb_user (id,name,age,password) value (null,"张三",null,null);

5、关于insertSelective()返回值,如果插入成功则返回1,失败返回0;对于updateByPrimaryKeySelective()的返回值,成功更新几条返回则返回相应的条数,比如更新了3条,返回值为3,如果更新失败返回0。


原文地址:https://www.cnblogs.com/kinome/p/9952233.html