Backend Development 11 min read

Upgrading Spring Boot 2.x with Java 11 and Integrating Baidu UID Generator

This tutorial walks through upgrading a Spring Boot 2.x project to Java 11, updating dependencies such as MyBatis and MySQL connector, configuring the Baidu UID generator database schema, adjusting MySQL settings, and building a uid‑provider microservice with full Maven and code examples.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Upgrading Spring Boot 2.x with Java 11 and Integrating Baidu UID Generator

Because upgrading to Spring Boot 2.x with Java 11 caused configuration failures, the author performed a step‑by‑step upgrade, added the required dependencies, and adjusted the injection source, finally achieving a successful build. The source code is available at https://github.com/foxiswho/java-spring-boot-uid-generator-baidu .

Partial Upgrade Details

The upgrade focuses on the official code dependencies. The official repository is https://github.com/baidu/uid-generator . The following components were upgraded:

Spring Boot version: 2.0.7.RELEASE

MyBatis and mybatis‑spring version

MySQL connector Java version: 8.0.12

JUnit version

Create Database Table

Import the official SQL script WORKER_NODE.sql , which creates a single table. The example uses a demo database:

DROP TABLE IF EXISTS WORKER_NODE;
CREATE TABLE WORKER_NODE (
    ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',
    HOST_NAME VARCHAR(64) NOT NULL COMMENT 'host name',
    PORT VARCHAR(64) NOT NULL COMMENT 'port',
    TYPE INT NOT NULL COMMENT 'node type: ACTUAL or CONTAINER',
    LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',
    MODIFIED TIMESTAMP NOT NULL COMMENT 'modified time',
    CREATED TIMESTAMP NOT NULL COMMENT 'created time',
    PRIMARY KEY(ID)
) COMMENT='DB WorkerID Assigner for UID Generator' ENGINE=INNODB;

If an error occurs, it is usually due to strict TIMESTAMP handling in older MySQL versions. Two solutions are provided:

Replace TIMESTAMP with DATETIME .

Execute SET sql_mode="NO_ENGINE_SUBSTITUTION"; before running the script.

MySQL Configuration Changes

After upgrading to MySQL 8.x, modify the uid/mysql.properties file in the uid-generator test resources to use the new driver:

mysql.driver=com.mysql.cj.jdbc.Driver

Configure the remaining database parameters so that unit tests can run successfully.

Example Use Case

The goal is to expose a globally unique ID generation service for other microservices. A new Maven sub‑project uid-provider is created alongside the official uid-generator module.

Create Sub‑project uid-provider

The pom.xml for the sub‑project is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>java-spring-boot-uid-generator-baidu</artifactId>
        <groupId>com.foxwho.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>uid-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- for MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.12</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.16</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.foxwho.demo</groupId>
            <artifactId>uid-generator</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Copy Mapper

Create a mapper folder under uid-provider/src/main/resources and copy META-INF/mybatis/mapper/WORKER_NODE.xml from the official uid‑generator resources into it.

Cache ID Configuration

Create a uid folder under the same resources path and copy uid/cached-uid-spring.xml from the official test resources.

Create Spring Boot Entry Point

Add the @MapperScan("com.baidu.fsg.uid") annotation so MyBatis can locate the mapper classes:

package com.foxwho.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
@MapperScan("com.baidu.fsg.uid")
public class ConsumerApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ConsumerApplication.class).run(args);
    }
}

Configuration Class

package com.foxwho.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations = {"classpath:uid/cached-uid-spring.xml"})
public class UidConfig {
}

Service Layer

package com.foxwho.demo.service;

import com.baidu.fsg.uid.UidGenerator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class UidGenService {
    @Resource(name = "cachedUidGenerator")
    private UidGenerator uidGenerator;

    public long getUid() {
        return uidGenerator.getUID();
    }
}

Controller

package com.foxwho.demo.controller;

import com.foxwho.demo.service.UidGenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UidController {
    @Autowired
    private UidGenService uidGenService;

    @GetMapping("/uidGenerator")
    public String UidGenerator() {
        return String.valueOf(uidGenService.getUid());
    }

    @GetMapping("/")
    public String index() {
        return "index";
    }
}

Application Properties

server.port=8080

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

Run the Project

Start the application from the main class, then access http://localhost:8080/uidGenerator in a browser. The page will display a generated unique ID such as 13128615512260612 .

Additional Information

The author has also published two series of articles on MyBatis and Spring Boot advanced topics, which are compiled into a book. Readers can reply with the keywords Mybatis 进阶 or Spring Boot 进阶 on the public account to receive them for free.

backendJavaMavenSpring BootMySQLuid-generator
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.