-
数据库脚本
DELETE FROM user; INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com'); DROP TABLE IF EXISTS user; CREATE TABLE user ( id BIGINT(20) NOT NULL COMMENT '主键ID', name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名', age INT(11) NULL DEFAULT NULL COMMENT '年龄', email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (id) ); DROP SEQUENCE IF EXISTS SEQ_USER; CREATE SEQUENCE SEQ_USER START WITH 1000 INCREMENT BY 1;
-
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <!-- for testing --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
启动类
@SpringBootApplication @MapperScan("com.mq.sequence.mapper") public class SequenceApplication { public static void main(String[] args) { SpringApplication.run(SequenceApplication.class, args); } }
-
实体类 注:Oralce这里也是INPUT,Mysql是AUTO
@Data @KeySequence("SEQ_USER") public class User { @TableId(value = "id", type = IdType.INPUT) private Long id; private String name; private Integer age; private String email; }
-
Dao层
public interface UserMapper extends BaseMapper<User> { }
-
配置类
@Configuration public class MybatisPlusConfig { /** * sequence主键,需要配置一个主键生成器 * 配合实体类注解 {@link KeySequence} + {@link TableId} type=INPUT * @return */ @Bean public H2KeyGenerator h2KeyGenerator(){ return new H2KeyGenerator(); } }
如果这里是Oracle的话也要注入,Mysql不要
/** * 注册oracle的主键 * @return */ @Bean public OracleKeyGenerator oracleKeyGenerator(){ return new OracleKeyGenerator(); }
-
application.yml
# DataSource Config spring: datasource: driver-class-name: org.h2.Driver url: jdbc:h2:tcp://192.168.180.115:19200/~/mem/test username: root password: test # Logger Config logging: level: com.mp.sequence: debug
-
测试类
@RunWith(SpringRunner.class) @SpringBootTest class SequenceApplicationTests { @Autowired(required = false) UserMapper userMapper; @Test public void testInsert() { User user = new User(); user.setAge(18); user.setEmail("test@baomidou.com"); user.setName("sequence"); userMapper.insert(user); Long id1 = user.getId(); System.out.println(id1); Assert.assertTrue("sequence start with 1000", id1 >= 1000); user = new User(); user.setAge(19); user.setEmail("test2@baomidou.com"); user.setName("sequence2"); userMapper.insert(user); Long id2 = user.getId(); Assert.assertTrue("squence increment by 1", id2 - id1 == 1); } }
-
测试结果:
SpringBoot整合MybatisPlus3.X之Sequence(二)
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot整合Minio
SpringBoot整合Minio
java 文件名 SpringBoot Minio -
MybatisPlus|整合SpringBoot
SpringBoot中整合mybatisplus
Java学习 MybatisPlus 数据库 mybatis java -
MyBatisPlus3.x代码生成器自定义模板配置
场景MyBatisPlus3.x中使用代码生成器(全注释):https://
自定义模板配置 自定义 java 代码生成器 -
SpringBoot整合MyBatisPlus(十四)
主要内容:1. SpringBoot整合MyBatis-plus2. 基本的CRUD用法3. 自动填充用法4. 逻辑删除用法 5. 分页查询用法
spring boot java mysql spring 封装