• 数据库脚本

    1. DELETE FROM user;

    2. INSERT INTO user (id, name, age, email) VALUES
    3. (1, 'Jone', 18, 'test1@baomidou.com'),
    4. (2, 'Jack', 20, 'test2@baomidou.com'),
    5. (3, 'Tom', 28, 'test3@baomidou.com'),
    6. (4, 'Sandy', 21, 'test4@baomidou.com'),
    7. (5, 'Billie', 24, 'test5@baomidou.com');

    8. DROP TABLE IF EXISTS user;

    9. CREATE TABLE user
    10. (
    11. id BIGINT(20) NOT NULL COMMENT '主键ID',
    12. name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    13. age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    14. email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    15. PRIMARY KEY (id)
    16. );

    17. DROP SEQUENCE IF EXISTS SEQ_USER;
    18. CREATE SEQUENCE SEQ_USER START WITH 1000 INCREMENT BY 1;
  • pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.baomidou</groupId>
    8. <artifactId>mybatis-plus-boot-starter</artifactId>
    9. <version>3.2.0</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.projectlombok</groupId>
    13. <artifactId>lombok</artifactId>
    14. </dependency>
    15. <dependency>
    16. <groupId>com.h2database</groupId>
    17. <artifactId>h2</artifactId>
    18. <scope>runtime</scope>
    19. </dependency>

    20. <!-- for testing -->
    21. <dependency>
    22. <groupId>org.springframework.boot</groupId>
    23. <artifactId>spring-boot-starter-test</artifactId>
    24. <scope>test</scope>
    25. </dependency>
    26. </dependencies>
  • 启动类

    1. @SpringBootApplication
    2. @MapperScan("com.mq.sequence.mapper")
    3. public class SequenceApplication {

    4. public static void main(String[] args) {
    5. SpringApplication.run(SequenceApplication.class, args);
    6. }

    7. }
  • 实体类 注:Oralce这里也是INPUT,Mysql是AUTO

    1. @Data
    2. @KeySequence("SEQ_USER")
    3. public class User {
    4.  
    5. @TableId(value = "id", type = IdType.INPUT)
    6. private Long id;
    7. private String name;
    8. private Integer age;
    9. private String email;
    10. }
  • Dao层

    1. public interface UserMapper extends BaseMapper<User> {

    2. }
  • 配置类

    1. @Configuration
    2. public class MybatisPlusConfig {

    3. /**
    4. * sequence主键,需要配置一个主键生成器
    5. * 配合实体类注解 {@link KeySequence} + {@link TableId} type=INPUT
    6. * @return
    7. */
    8. @Bean
    9. public H2KeyGenerator h2KeyGenerator(){
    10. return new H2KeyGenerator();
    11. }

    12. }

    如果这里是Oracle的话也要注入,Mysql不要

    1. /**
    2. * 注册oracle的主键
    3. * @return
    4. */
    5. @Bean
    6. public OracleKeyGenerator oracleKeyGenerator(){
    7. return new OracleKeyGenerator();
    8. }
    1.  
  • application.yml

    1. # DataSource Config
    2. spring:
    3. datasource:
    4. driver-class-name: org.h2.Driver
    5. url: jdbc:h2:tcp://192.168.180.115:19200/~/mem/test
    6. username: root
    7. password: test

    8. # Logger Config
    9. logging:
    10. level:
    11. com.mp.sequence: debug
  • 测试类

    1. @RunWith(SpringRunner.class)
    2. @SpringBootTest
    3. class SequenceApplicationTests {


    4. @Autowired(required = false)
    5. UserMapper userMapper;

    6. @Test
    7. public void testInsert() {
    8. User user = new User();
    9. user.setAge(18);
    10. user.setEmail("test@baomidou.com");
    11. user.setName("sequence");
    12. userMapper.insert(user);
    13. Long id1 = user.getId();
    14. System.out.println(id1);
    15. Assert.assertTrue("sequence start with 1000", id1 >= 1000);
    16. user = new User();
    17. user.setAge(19);
    18. user.setEmail("test2@baomidou.com");
    19. user.setName("sequence2");
    20. userMapper.insert(user);
    21. Long id2 = user.getId();
    22. Assert.assertTrue("squence increment by 1", id2 - id1 == 1);
    23. }

    24. }
  • 测试结果:

SpringBoot整合MybatisPlus3.X之Sequence(二)的更多相关文章

  1. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  2. SpringBoot整合MyBatis-Plus3.1详细教程

    作者:Sans_ juejin.im/post/5cfa6e465188254ee433bc69 一.说明 Mybatis-Plus是一个Mybatis框架的增强插件,根据官方描述,MP只做增强不做改 ...

  3. SpringBoot整合MybatisPlus3.X之Wrapper(五)

    官方文档说明: 以下出现的第一个入参boolean condition表示该条件是否加入最后生成的sql中 以下代码块内的多个方法均为从上往下补全个别boolean类型的入参,默认为true 以下出现 ...

  4. SpringBoot整合持久层技术--(二)MyBatis

    简介: 原名iBatis,SpringBoot中使用MyBatis: pom.xml <dependency> <groupId>org.springframework.boo ...

  5. SpringBoot整合MybatisPlus3.X之SQL执行分析插件(十四)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  6. SpringBoot整合MybatisPlus3.X之乐观锁(十三)

    主要适用场景 意图: 当要更新一条记录的时候,希望这条记录没有被别人更新 乐观锁实现方式: 取出记录时,获取当前version 更新时,带上这个version 执行更新时, set version = ...

  7. SpringBoot整合MybatisPlus3.X之自定义Mapper(十)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  8. SpringBoot整合MybatisPlus3.X之SQL注入器(九)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  9. SpringBoot整合MybatisPlus3.X之分页插件(四)

    注:详细请看2.X博客中,3.X直接上代码. 建议装一个MybatisX插件,可以在Mapper和Xml来回切换 pom.xml <dependencies> <dependency ...

随机推荐

  1. 【Django】ESRTful APi

    如何利用rest framework搭建Django API框架!   环境:win10 python3.6 思路步骤: 创建一个可以序列化的类 去数据库取数据交给序列化的类处理 把序列化的数据返回前 ...

  2. Java源码跟踪阅读技巧

    转:https://www.jianshu.com/p/ab865109070c 本文基于Eclipse IDE 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl ...

  3. 极光推送消息——RegistrationID方式

    1.工具类 package com.test.util; import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang ...

  4. SUSE CaaS Platform 4 - 使用 Ceph RBD 作为持久存储(动态)

    图1 架构图 图2 各存储插件对动态供给方式的支持状况 1.所有节点安装 # yum install ceph-common 复制 ceph.conf 到 worker 节点上 # scp admin ...

  5. Zookeeper学习笔记之 Zab协议(Zookeeper Atomic Broadcast)

    Zab协议(Zookeeper Atomic Broadcast): 广播模式: Leader将所有更新(称为proposal),顺序发送给Follower 当Leader收到半数以上的Followe ...

  6. Laravel Entrust 权限管理扩展包的使用笔记

    简介 Entrust 是一个简洁而灵活的基于角色进行权限管理的 Laravel 扩展包.针对 Laravel 5,官方推荐的安装版本是 5.2.x-dev.它的详细使用方法请查看 Entrust Gi ...

  7. Activity初学乍练

    1.Activity的概念与Activity的生命周期图: 注意事项: onPause()和onStop()被调用的前提是: 打开了一个新的Activity!而前者是旧Activity还可见的状态:后 ...

  8. CentOS 8 网卡设置

    本次测试环境是在虚拟机上测试 网卡配置文件路径:/etc/sysconfig/network-scripts/ifcfg-ens33 [root@localhost ~]# cd /etc/sysco ...

  9. display:none和visibility:hidden的区别?

    css控制元素不可见的方法 { display: none; /* 不占据空间,无法点击 */ } /************************************************* ...

  10. 从0开始学FreeRTOS-(消息队列)-5

    ## 问题解答 曾经有人问我,FreeRTOS那么多API,到底怎么记住呢? 我想说,其实API不难记,就是有点难找,因为FreeRTOS的API很多都是带参宏,所以跳来跳去的比较麻烦,而且注释也很多 ...