一、insert
1、插入操作
//添加操作
@Test
public void addUser() {
User user = new User();
user.setName("嶽不羣1");
user.setAge(70);
user.setEmail("[email protected]");
int insert = userMapper.insert(user);
System.out.println("insert:"+insert);
}
執行日誌輸出結果 Parameters: 1249545169748951041(Long), 嶽不羣1(String), 70(Integer), [email protected](String)
1249545169748951041是MP自動生成的
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ab550d5] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@999674172 wrapping com.mysql.cj.jdbc.ConnectionImpl@3730ab42] will not be managed by Spring
==> Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 1249545169748951041(Long), 嶽不羣1(String), 70(Integer), [email protected](String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ab550d5]
insert:1
2、主鍵策略
(1)ID_WORKER
MyBatis-Plus默認的主鍵策略是:ID_WORKER 全局唯一ID
參考資料:分佈式系統唯一ID生成方案彙總:https://www.cnblogs.com/haoxinyue/p/5208136.html
@Data
public class User {
//@TableId(type = IdType.ID_WORKER) //mp自帶策略,生成19位值,數字類型使用這種策略,比如long
//@TableId(type = IdType.ID_WORKER_STR) //mp自帶策略,生成19位值,字符串類型使用這種策略
private Long id;
private String name;
private Integer age;
private String email;
}