• 数据库脚本

    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(二)_实体类