请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

欣东微医疗技术论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 23|回复: 0

Configuring Spring Boot for Oracle

[复制链接]

455

主题

557

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
37489
QQ
发表于 2017-10-10 16:09:32 | 显示全部楼层 |阅读模式
转自https://springframework.guru/configuring-spring-boot-for-oracle/   

    When you start with Spring Boot, it will automatically support H2 if no other data sources have been defined and H2 is found on the classpath. I’ve been using H2 for development for sometime now. It works very well.  All modern relational databases are going to support ANSI SQL. But each is going to have its own nuances and extensions. One thing of the things I like about H2 is its Oracle compatibility mode. It allows H2 to act like a Oracle database. It’s not perfect, but it does do a pretty good job.
The Spring Framework is the most popular Java framework used for building enterprise class applications. Oracle is the most popular database used in the enterprise. So chances are, if you developing Spring Applications, sooner or later, you’re going to be persisting to an Oracle database.
Oracle Database Driver    The Oracle JDBC drivers are not in public Maven repositories due to legal restrictions. This is really rather annoying. Oracle, if you’re reading this – really? Come on, fix this. Please.
So, if you are in a company, chances are you will have a nexus installation with the Oracle JDBC jar installed. But if you are not, you will need to download the JDBC driver from Oracle (after accepting the terms and conditions you probably won’t read). And then you can install it into your local Maven repository manually.
You can install a JAR into your Maven repository using this Maven command. You may need to adjust the version and name depending on the JDBC driver version you download.
mvn install:install-file -Dfile=ojdbc7.jar  -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.1 -Dpackaging=jar
1

mvn install:install-file -Dfile=ojdbc7.jar  -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.1 -Dpackaging=jar



Spring Boot Configuration for OracleMaven DependencyYou will need to add the Oracle Driver to your Maven (or Gradle) dependencies.
  <dependency>            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1</version>
        </dependency>
        <dependency>            <groupId>com.oracle</groupId>            <artifactId>ojdbc7</artifactId>            <version>12.1.0.1</version>        </dependency>




Oracle DatasourceThe easiest approach is to create a configuration bean in the package structure of your Spring Boot application. This will create a new Oracle datasource for your Spring Boot application. Once you specify a data source, Spring Boot will no longer create the H2 data source for you automatically.
    @Bean    DataSource dataSource() throws SQLException {        OracleDataSource dataSource = new OracleDataSource();        dataSource.setUser(username);        dataSource.setPassword(password);        dataSource.setURL(url);        dataSource.setImplicitCachingEnabled(true);        dataSource.setFastConnectionFailoverEnabled(true);        return dataSource;    }
1
2
3
4
5
6
7
8
9
10
11

    @Bean
    DataSource dataSource() throws SQLException {

        OracleDataSource dataSource = new OracleDataSource();
        dataSource.setUser(username);
        dataSource.setPassword(password);
        dataSource.setURL(url);
        dataSource.setImplicitCachingEnabled(true);
        dataSource.setFastConnectionFailoverEnabled(true);
        return dataSource;
    }




Spring Boot Basic Configuration for OracleSpring Boot PropertiesConfiguring a different datasource in Spring Boot is very simple. When you supply datasource properties in Spring Boot’s application.properties file, Spring Boot will use them to configure the datasource. To configure Spring Boot for Oracle, add the following lines to your properties file.
#Basic Spring Boot Config for Oraclespring.datasource.url= jdbcracle:thin//spring.guru.csi0i9rgj9ws.us-east-1.rds.amazonaws.com:1521/ORCLspring.datasource.username=systemspring.datasource.password=managerspring.datasource.driver-class-name=oracle.jdbc.OracleDriver#hibernate configspring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
1
2
3
4
5
6
7
8

#Basic Spring Boot Config for Oracle
spring.datasource.url= jdbcracle:thin//spring.guru.csi0i9rgj9ws.us-east-1.rds.amazonaws.com:1521/ORCL
spring.datasource.username=system
spring.datasource.password=manager
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect



Spring Boot Advanced Configuration for OracleOracle is a highly advanced and highly configurable RDBMS. There is a reason Oracle is the #1 database in the enterprise. The basic example above will work for just about any JDBC data source you need to configure for use with Spring Boot. They will all have a url, user name, password, and driver class. But with Oracle, there are a number of advanced properties you may need to set. Especially if you’re using Oracle RAC.
Spring Boot will set vendor specific properties using spring.datasource.<property name>. And you absolutely can go this route. However, based on my experience, it might be time to switch to a Java based configuration. Spring Boot will create the data source from just the properties file, or will forgo the automatic data source creation if you’re a more traditional method in Spring to define the data source bean.
In this section, I’m going to show you how to use a Spring configuration bean to create the Oracle JDBC datasource.
Oracle PropertiesIn this example, I’m going to show you how to externalise the Oracle connection properties to a properties file.
In our Spring Boot application.properties file we want to set the following properties.
#Oracle connectionoracle.username=systemoracle.password=manageroracle.url=jdbcracle:thin//spring.guru.csi0i9rgj9ws.us-east-1.rds.amazonaws.com:1521/ORCL
1
2
3
4

#Oracle connection
oracle.username=system
oracle.password=manager
oracle.url=jdbcracle:thin//spring.guru.csi0i9rgj9ws.us-east-1.rds.amazonaws.com:1521/ORCL



Next, on our Configuration class for Oracle, we want to add the following annotation:
@ConfigurationProperties("oracle")
1

@ConfigurationProperties("oracle")



This tells Spring to look for the property prefix of Oracle when binding properties. Now if our configuration class has a property called ‘whatever’, Spring would try to bind the property value of ‘oracle.whatever’ to the property in the configuration class.
Now if we add the following properties to our configuration class, Spring will use them in the creation of our Oracle data source.
    @NotNull    private String username;    @NotNull    private String password;    @NotNull    private String url;    public void setUsername(String username) {        this.username = username;    }    public void setPassword(String password) {        this.password = password;    }    public void setUrl(String url) {        this.url = url;    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

    @NotNull
    private String username;

    @NotNull
    private String password;

    @NotNull
    private String url;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUrl(String url) {
        this.url = url;
    }



The final Oracle configuration class looks like this:
OracleConfiguration.classpackage guru.springframework.configuration;import oracle.jdbc.pool.OracleDataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Profile;import javax.sql.DataSource;import javax.validation.constraints.NotNull;import java.sql.SQLException;@Configuration@ConfigurationProperties("oracle")public class OracleConfiguration {    @NotNull    private String username;    @NotNull    private String password;    @NotNull    private String url;    public void setUsername(String username) {        this.username = username;    }    public void setPassword(String password) {        this.password = password;    }    public void setUrl(String url) {        this.url = url;    }    @Bean    DataSource dataSource() throws SQLException {        OracleDataSource dataSource = new OracleDataSource();        dataSource.setUser(username);        dataSource.setPassword(password);        dataSource.setURL(url);        dataSource.setImplicitCachingEnabled(true);        dataSource.setFastConnectionFailoverEnabled(true);        return dataSource;    }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

package guru.springframework.configuration;

import oracle.jdbc.pool.OracleDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;
import javax.validation.constraints.NotNull;
import java.sql.SQLException;

@Configuration
@ConfigurationProperties("oracle")
public class OracleConfiguration {
    @NotNull
    private String username;

    @NotNull
    private String password;

    @NotNull
    private String url;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Bean
    DataSource dataSource() throws SQLException {

        OracleDataSource dataSource = new OracleDataSource();
        dataSource.setUser(username);
        dataSource.setPassword(password);
        dataSource.setURL(url);
        dataSource.setImplicitCachingEnabled(true);
        dataSource.setFastConnectionFailoverEnabled(true);
        return dataSource;
    }
}



Hibernate ConfigurationWe will want to tell Hibernate to use the Oracle dialect. We do this by adding the following property to the Spring Boot application.properties file.
Requiredspring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
1

spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect



OptionalIf you’re used to using the H2 database, database tables will automatically be generated by Hibernate. If you want the same behavior in Oracle, you’ll need to set the ddl-auto property of Hibernate to ‘create-drop’. The Spring Boot documentation has additional information about database initialization. To have tables automatically created in Oracle, set the following property in your application.properties file.
spring.jpa.hibernate.ddl-auto=create-drop
1

spring.jpa.hibernate.ddl-auto=create-drop



Amazon RDSIn testing the code for this post, I spooled up an Oracle instance using Amazon RDS. This makes creating an Oracle database crazy easy. If you want to test this out yourself, I’ve checked in the code on GitHub here. You can check it out, and setup your own Oracle instance on Amazon RDS. Just update the connection properties in application.properities. I branched the code from my tutorial series on building a web application with Spring Boot. Everything will work – EXCEPT create and save. Oracle handles ID generation a little differently and I did not update the JPA mapping for this.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|eastwill.org  

GMT+8, 2017-10-23 01:17 , Processed in 0.032704 second(s), 14 queries , Apc On.

Powered by Eastwill! X3.2

© 2001-2013 Eastwill Inc.

快速回复 返回顶部 返回列表