All Products
Search
Document Center

ApsaraDB for OceanBase:Connect to an OceanBase database by using SpringBoot

Last Updated:Aug 01, 2023

This topic provides a SpringBoot connection example.

Environment configuration

JDK 1.8 and OceanBase Database V3.x (in MySQL mode)

Sample code

Pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.alipay.test</groupId>
    <artifactId>SpringBootMySQLJPA</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-json</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.oceanbase</groupId>
            <artifactId>oceanbase-client</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>
</project>

application.yml

server:
  port: 8081
spring:
  jpa:
    database: mysql
    show-sql: true
  datasource:
      driver-class-name: com.oceanbase.jdbc.Driver
      url: jdbc:oceanbase://xxx.xxx.xxx.xxx:3306/test?characterEncoding=UTF-8
      username: a****
      password: ******
#spring.jpa.hibernate.ddl-auto=update
jackson:
  serialization:
    indent_output: true

Test class

Sample code:

package com.alipay;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Date;

/*
 * CREATE TABLE A(id integer, flightNo varchar2(50))
 *
 */
@Entity
@Table( name = "A" )
public class A implements Serializable {
    public Integer getFlightId() {
        return flightId;
    }

    public void setFlightId(Integer flightId) {
        this.flightId = flightId;
    }

    public String getFlightNo() {
        return flightNo;
    }

    public void setFlightNo(String flightNo) {
        this.flightNo = flightNo;
    }

    @Id
    //@GeneratedValue
    @Column(name = "id")
    private Integer flightId;

    @Column( name = "flightNo" )
    private String flightNo;

}
package com.alipay;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AMapper extends JpaRepository<A, Integer> {

}
package com.alipay;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/")
public class Test {
    @Autowired
    private AMapper a;
    @GetMapping("/{id}")
    public String getOne(@PathVariable int id) {

        Optional<A> obj = a.findById(id);
        String result = "A.id is:" + obj.get().getFlightId() + ", no is :" + obj.get().getFlightNo();
        System.out.println(result);
        return result;
    }
}
package com.alipay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

Run the preceding code. If the following log is returned, the startup is successful.

SpringBoot-日志结果

Then access the database by using the address http://localhost:8081/xxxxx, where xxxxx is the value of the id field of the database. The query result can be found if the database exists.