1. 程式人生 > >Mybatis中int insertSelective()的相關問題

Mybatis中int insertSelective()的相關問題

語句 use val 但是 tis mar key ive name

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。

Mybatis中int insertSelective()的相關問題