• 数据库脚本

    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(二)的更多相关文章

  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. python习题作业合集(持续更新……)

    作业: 1.简述位,字节关系 2.请写出“天才”分别用utf-8和gbk编码所占位数 3.如果有一个变量num = 14,请使用int的方法,得到改变量最少可以用多少个二进制位表示 4.写代码,有如下 ...

  2. Spring 梳理 - javaConfig在App和webApp中的应用

    package com.dxz.demo.configuration; import org.springframework.context.annotation.Configuration; @Co ...

  3. 一个有意思的js块作用域问题

    1.问题 首先把问题放出来,昨天看了一个掘友发的一个问题,然后跟我同事一起研究了一下,没找出来是为什么,然后我回来一直在想为什么,然后各种找资料研究,从各个方面找为什么,比如js上下文,作用域,js垃 ...

  4. Kafka 异步消息也会阻塞?记一次 Dubbo 频繁超时排查过程

    线上某服务 A 调用服务 B 接口完成一次交易,一次晚上的生产变更之后,系统监控发现服务 B 接口频繁超时,后续甚至返回线程池耗尽错误 Thread pool is EXHAUSTED.因为服务 B ...

  5. python正则表达式贪婪算法与非贪婪算法与正则表达式子模式的简单应用

    先引入一下百度百科对于正则表达式的概念: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种 ...

  6. aircrack-ng wifi密码破解

    wifi密码破解 步骤1:查看网卡信息 ifconfig 找到你要用到的网卡 步骤2:启动网卡监听模式 airmon-ng start wlan0 我的是wlp2s0 步骤三:查看网卡变化 wlan0 ...

  7. Openshift 部署第一个应用hello-openshift

    Openshift 部署第一个应用hello-openshift: cd /opt/ wget https://github.com/openshift/origin/releases/downloa ...

  8. spring boot参数验证

    必须要知道 简述 JSR303/JSR-349,hibernate validation,spring validation 之间的关系 JSR303 是一项标准,JSR-349 是其的升级版本,添加 ...

  9. Spring Boot WebFlux 增删改查完整实战 demo

    03:WebFlux Web CRUD 实践 前言 上一篇基于功能性端点去创建一个简单服务,实现了 Hello .这一篇用 Spring Boot WebFlux 的注解控制层技术创建一个 CRUD ...

  10. Requests实践详解

    Requests是什么 Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库 如果你看过上篇文章关于urllib库的使用,你会发现, ...