MybatisPlus自带生成策略(id值)
作者:互联网
MybatisPlus自带生成策略(id值)
基于这篇博客
user类
package com.xiang.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/21 2:50
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
// @TableId(type = IdType.ID_WORKER) //数字类型的id使用,当类型为Long时默认使用,生成19位的Id值,
@TableId(type = IdType.ID_WORKER_STR)//字符串类型的id使用,需要写明注解,生成19位的Id值
//同时需要设置数据库的字段id的类型为Bigint,因为int的长度只有11位
private Long id;
private String username;
private String sex;
private int age;
private Date birthday;
}
参考网址
测试类
package com.xiang;
import com.xiang.mapper.UserMapper;
import com.xiang.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/21 16:02
*/
@SpringBootTest
public class GenerationStrategy {
@Autowired
UserMapper userMapper;
@Test
void intro() {
User user = new User();
user.setUsername("周某");
user.setSex("男");
int insert = userMapper.insert(user);
if (insert > 0) {
System.out.println("插入成功");
} else {
System.out.println("fail");
}
List<User> list = userMapper.selectList(null);
for (User listUser : list) {
System.out.println(listUser);
}
}
}
运行结果
JDBC Connection [HikariProxyConnection@1979825302 wrapping com.mysql.cj.jdbc.ConnectionImpl@48a0c8aa] will not be managed by Spring
==> Preparing: INSERT INTO user ( id, username, sex, age ) VALUES ( ?, ?, ?, ? )
==> Parameters: 1451098975287668738(Long), 周某(String), 男(String), 0(Integer)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@29a98d9f]
插入成功
JDBC Connection [HikariProxyConnection@1985280051 wrapping com.mysql.cj.jdbc.ConnectionImpl@48a0c8aa] will not be managed by Spring
==> Preparing: SELECT id,username,sex,age,birthday FROM user
==> Parameters:
<== Columns: id, username, sex, age, birthday
<== Row: 1, xiang, 男, 18, 2021-10-03
<== Row: 559, 小向, 男, 18, 2021-10-04
<== Row: 602, admin, 女, 18, 2021-10-20
<== Row: 603, testbox, 女, 18, 2021-10-02
<== Row: 604, 小一, 男, 18, 2021-10-16
<== Row: 605, 小二, 女, 18, null
<== Row: 607, 小四, 女, 21, null
<== Row: 609, 小五, 女, 0, null
<== Row: 1451097869404868609, 向某, 男, 0, null
<== Row: 1451098975287668738, 周某, 男, 0, null
<== Total: 10
标签:MybatisPlus,xiang,User,import,自带,com,id,user 来源: https://www.cnblogs.com/d534/p/15434149.html