OceanBase學習之路4|SpringBoot 連線示例

老樹樁12發表於2023-01-16

本文介紹 SpringBoot 連線示例。

環境配置

JDK1.8 和 OceanBase 3.x(mysql模式/oracle模式)

示例程式碼

Pom.xml 檔案

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
         xmlns:xsi="
         xsi:schemaLocation="
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.alipay.test</groupId>
    <artifactId>SpringBootOracleJPA</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>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>12.1.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.oceanbase</groupId>
        <artifactId>oceanbase-client</artifactId>
        <version>3.2.3</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.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-juli</artifactId>
        <version>9.0.37</version>
    </dependency>
    </dependencies>
</project>

application.yml 檔案

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

測試類

相關程式碼如下:

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
// oracle 沒有自增策略,新增該註解可以自動生成一個序列,提供自增主鍵,若資料庫已有相關序列,可以忽 //略該註解。
    @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);
    }
}

執行上面的程式碼,如下日誌表示啟動成功。

SpringBoot 連線示例

然後透過   就可以訪問資料庫,  xxxxx 就是對應的資料庫  id 欄位,如果資料庫存在就可以查詢到結果。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70026075/viewspace-2932334/,如需轉載,請註明出處,否則將追究法律責任。

相關文章