博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2.14. Spring boot with Oracle
阅读量:5900 次
发布时间:2019-06-19

本文共 11301 字,大约阅读时间需要 37 分钟。

2.14.1. Maven

首先到oracle官网,根据你的Oracle数据库,下载ojdbc6.jar(Oracle 11) 或者 ojdbc7.jar (Oracle 12)

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

另一种方案是在项目根目录下创建一个/lib文件夹,将下载的驱动放入该文件夹中。然后pom.xml 加入下面代码

ojdbc6.jar 例子

com.oracle
ojdbc6
11.2.0.3
system
${basedir}/lib/ojdbc6.jar

ojdbc7.jar 例子

com.oracle
ojdbc7
12.1.0.1
system
${basedir}/lib/ojdbc7.jar

例 2.1. Example Spring boot with Oracle

4.0.0
netkiller.cn
api.netkiller.cn
0.0.1-SNAPSHOT
jar
api.netkiller.cn
http://maven.apache.org
UTF-8
1.8
org.springframework.boot
spring-boot-starter-parent
1.3.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-data-mongodb
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-test
test
org.springframework.data
spring-data-mongodb
org.springframework.data
spring-data-oracle
1.0.0.RELEASE
com.oracle
ojdbc6
11.2.0.3
system
${basedir}/lib/ojdbc6.jar
mysql
mysql-connector-java
com.google.code.gson
gson
compile
junit
junit
test
src
org.springframework.boot
spring-boot-maven-plugin
maven-compiler-plugin
3.3
maven-war-plugin
2.6
WebContent
false

2.14.2. application.properties

spring.datasource.driver-class-name=oracle.jdbc.OracleDriverspring.datasource.url=jdbc:oracle:thin:@//192.168.4.9:1521/orcl.example.comspring.datasource.username=wwwspring.datasource.password=123123spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialectspring.jpa.show-sql=true#spring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.ddl-auto=create-drop

其他配置

spring.datasource.connection-test-query="SELECT 1 FROM DUAL"				spring.datasource.test-while-idle=true				spring.datasource.test-on-borrow=true

Oracle RAC

jdbc:oracle:thin@(DESCRIPTION=				(LOAD_BALANCE=on)				(ADDRESS=(PROTOCOL=TCP)(HOST=racnode1) (PORT=1521))				(ADDRESS=(PROTOCOL=TCP)(HOST=racnode2) (PORT=1521))				(CONNECT_DATA=(SERVICE_NAME=service_name)))				jdbc:oracle:thin:@(DESCRIPTION=				(ADDRESS=(PROTOCOL=TCP)(HOST=125.22.42.68)(PORT=1521))				(LOAD_BALANCE=on)				(FAILOVER=ON)				(CONNECT_DATA=				(SERVER=DEDICATED)				(SERVICE_NAME=service_name)				(FAILOVER_MODE=(TYPE=SESSION)(METHOD=BASIC))				)				)

2.14.3. Application

package api;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import api.ApplicationConfiguration;@SpringBootApplication@EnableConfigurationProperties(ApplicationConfiguration.class)@EnableAutoConfiguration@ComponentScan({ "api.web", "api.rest", "api.service" })@EnableMongoRepositories@EnableJpaRepositoriespublic class Application {	public @Bean WebMvcConfigurer corsConfigurer() {		return new WebMvcConfigurerAdapter() {			@Override			public void addCorsMappings(CorsRegistry registry) {				registry.addMapping("/**");			}		};	}	public static void main(String[] args) {		SpringApplication.run(Application.class, args);	}}

2.14.4. CrudRepository

package api.repository;import java.util.List;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.CrudRepository;import org.springframework.data.repository.query.Param;import org.springframework.stereotype.Repository;import api.domain.Article;@Repositorypublic interface ArticleRepository extends CrudRepository
{ Page
findAll(Pageable pageable); Article findByTitle(String title); //@Query("select id,title,content from Article where id > ?1") //public List
findBySearch(@Param("id")long id); }
package api.domain;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.SequenceGenerator;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Table;@Entity@Table(name = "article")public class Article implements Serializable {	private static final long serialVersionUID = 7998903421265538801L;	@Id	@Column(name = "ID")	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")	@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")	private Long id;	private String title;	private String content;	public Article(){			}	public Article(String title, String content) {		this.title = title;		this.content = content;	}	public Long getId() {		return id;	}		public void setId(Long id) {		this.id = id;	}	public String getTitle() {		return title;	}	public void setTitle(String title) {		this.title = title;	}	public String getContent() {		return content;	}	public void setContent(String content) {		this.content = content;	}	@Override	public String toString() {		return "Article [id=" + id + ", title=" + title + ", content=" + content + "]";	}}

2.14.5. JdbcTemplate

@Autowired	private JdbcTemplate jdbcTemplate;	@RequestMapping(value = "/article")	public @ResponseBody String dailyStats(@RequestParam Integer id) {		String query = "SELECT id, title, content from article where id = " + id;		return jdbcTemplate.queryForObject(query, (resultSet, i) -> {			System.out.println(resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));			return (resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3));		});	}

2.14.6. Controller

package api.web;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import api.domain.Article;import api.repository.ArticleRepository;@Controllerpublic class IndexController {	@Autowired	private ArticleRepository articleRepository;		@RequestMapping("/mysql")	@ResponseBody	public String mysql() {		repository.deleteAll();		return "Deleted"		}		@RequestMapping("/mysql")	@ResponseBody	public String mysql() {		articleRepository.save(new Article("Neo", "Chen"));		for (Article article : articleRepository.findAll()) {			System.out.println(article);		}		Article tmp = articleRepository.findByTitle("Neo");		return tmp.getTitle();	}		/*	@RequestMapping("/search")	@ResponseBody	public String search() {				/*for (Article article : articleRepository.findBySearch(1)) {			System.out.println(article);		}*/		List
tmp = articleRepository.findBySearch(1L); tmp.forEach((temp) -> { System.out.println(temp.toString()); }); return tmp.get(0).getTitle(); } */ @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping(value = "/article") public @ResponseBody String dailyStats(@RequestParam Integer id) { String query = "SELECT id, title, content from article where id = " + id; return jdbcTemplate.queryForObject(query, (resultSet, i) -> { System.out.println(resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3)); return (resultSet.getLong(1)+","+ resultSet.getString(2)+","+ resultSet.getString(3)); }); }}

原文出处:Netkiller 系列 手札

本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

你可能感兴趣的文章
Bootstrap系列 -- 15. 下拉选择框select
查看>>
【转载】无限级分类的简单实例
查看>>
基础总结篇之一:Activity生命周期
查看>>
关于WinPE安装操作系统
查看>>
Windows 10 IoT Serials 2 - Windows 10 IoT RTM 升级教程
查看>>
使用iftop网络流量监控
查看>>
LeetCode Median of Two Sorted Arrays
查看>>
(算法)两个人是否为队友
查看>>
oschina程序开发
查看>>
mysql创建每月执行一次的event
查看>>
直接刷脸?一元就能搞定会议签到!
查看>>
kafka集群部署
查看>>
STM8串口初始化寄存器配置
查看>>
ReactNative常用组件汇总
查看>>
openfaas 安装(docker swarm 模式)
查看>>
nested exception is java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter
查看>>
“正在注册字体”问题解决
查看>>
[转]OPEN(SAP) UI5 学习入门系列之一:扫盲与热身(上)
查看>>
CSS/CSS3中的原生变量var详解以及布局响应式网页扩展
查看>>
windows10 更新后要输入2次密码才能进入系统
查看>>