SpringBoot+Druid+Mybatis连接Oracle

Stella981
• 阅读 950

最近有一个新项目需要开发搭建了个SpringBoot框架 记录一下!

Oracle连接jar编译到maven仓库参考:Maven编译jar包到本地仓库

1、SpringBoot版本:

2.1.6.RELEASE

2、pom配置

  1. org.springframework.boot

  2. spring-boot-starter-jdbc

  3. org.springframework.boot

  4. spring-boot-starter-web

  5. com.alibaba

  6. druid

  7. 1.1.10

  8. org.oracle

  9. ojdbc6

  10. 11.2.0.2.0

  11. org.mybatis.spring.boot

  12. mybatis-spring-boot-starter

  13. 2.0.1

  14. org.springframework.boot

  15. spring-boot-starter-aop

  16. log4j

  17. log4j

  18. 1.2.17

3、Druid配置

Druid配置类

  1. package com.jst.tjgx.supervisionservice.datasource;

  2. import com.alibaba.druid.pool.DruidDataSource;

  3. import com.alibaba.druid.support.http.StatViewServlet;

  4. import com.alibaba.druid.support.http.WebStatFilter;

  5. import org.slf4j.Logger;

  6. import org.slf4j.LoggerFactory;

  7. import org.springframework.boot.context.properties.ConfigurationProperties;

  8. import org.springframework.boot.web.servlet.FilterRegistrationBean;

  9. import org.springframework.boot.web.servlet.ServletRegistrationBean;

  10. import org.springframework.context.annotation.Bean;

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

  12. import javax.sql.DataSource;

  13. import java.sql.SQLException;

  14. @Configuration

  15. public class DruidConfiguration {

  16. private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);

  17. private static final String DB_PREFIX = "spring.datasource";

  18. @Bean

  19. public ServletRegistrationBean druidServlet() {

  20. logger.info( "init Druid Servlet Configuration ");

  21. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

  22. // IP白名单 (没有配置或者为空,则允许所有访问)

  23. servletRegistrationBean.addInitParameter( "allow", "");

  24. // IP黑名单(共同存在时,deny优先于allow)

  25. //servletRegistrationBean.addInitParameter("deny", "192.168.1.100");

  26. //控制台管理用户

  27. servletRegistrationBean.addInitParameter( "loginUsername", "admin");

  28. servletRegistrationBean.addInitParameter( "loginPassword", "admin");

  29. //是否能够重置数据 禁用HTML页面上的“Reset All”功能

  30. servletRegistrationBean.addInitParameter( "resetEnable", "false");

  31. return servletRegistrationBean;

  32. }

  33. @Bean

  34. public FilterRegistrationBean filterRegistrationBean() {

  35. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

  36. filterRegistrationBean.addUrlPatterns( "/*");

  37. filterRegistrationBean.addInitParameter( "exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");

  38. return filterRegistrationBean;

  39. }

  40. @ConfigurationProperties(prefix = DB_PREFIX)

  41. class IDataSourceProperties {

  42. private String url;

  43. private String username;

  44. private String password;

  45. private String driverClassName;

  46. private int initialSize;

  47. private int minIdle;

  48. private int maxActive;

  49. private int maxWait;

  50. private int timeBetweenEvictionRunsMillis;

  51. private int minEvictableIdleTimeMillis;

  52. private String validationQuery;

  53. private boolean testWhileIdle;

  54. private boolean testOnBorrow;

  55. private boolean testOnReturn;

  56. private boolean poolPreparedStatements;

  57. private int maxPoolPreparedStatementPerConnectionSize;

  58. private String filters;

  59. private String connectionProperties;

  60. @Bean

  61. public DataSource dataSource() {

  62. DruidDataSource datasource = new DruidDataSource();

  63. datasource.setUrl(url);

  64. datasource.setUsername(username);

  65. datasource.setPassword(password);

  66. datasource.setDriverClassName(driverClassName);

  67. //configuration

  68. datasource.setInitialSize(initialSize);

  69. datasource.setMinIdle(minIdle);

  70. datasource.setMaxActive(maxActive);

  71. datasource.setMaxWait(maxWait);

  72. datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

  73. datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

  74. datasource.setValidationQuery(validationQuery);

  75. datasource.setTestWhileIdle(testWhileIdle);

  76. datasource.setTestOnBorrow(testOnBorrow);

  77. datasource.setTestOnReturn(testOnReturn);

  78. datasource.setPoolPreparedStatements(poolPreparedStatements);

  79. datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

  80. try {

  81. datasource.setFilters(filters);

  82. logger.info( "druid configuration initialization success! ");

  83. } catch (SQLException e) {

  84. System.err.println( "druid configuration initialization filter: " + e);

  85. }

  86. datasource.setConnectionProperties(connectionProperties);

  87. return datasource;

  88. }

  89. public String getUrl() {

  90. return url;

  91. }

  92. public void setUrl(String url) {

  93. this.url = url;

  94. }

  95. public String getUsername() {

  96. return username;

  97. }

  98. public void setUsername(String username) {

  99. this.username = username;

  100. }

  101. public String getPassword() {

  102. return password;

  103. }

  104. public void setPassword(String password) {

  105. this.password = password;

  106. }

  107. public String getDriverClassName() {

  108. return driverClassName;

  109. }

  110. public void setDriverClassName(String driverClassName) {

  111. this.driverClassName = driverClassName;

  112. }

  113. public int getInitialSize() {

  114. return initialSize;

  115. }

  116. public void setInitialSize(int initialSize) {

  117. this.initialSize = initialSize;

  118. }

  119. public int getMinIdle() {

  120. return minIdle;

  121. }

  122. public void setMinIdle(int minIdle) {

  123. this.minIdle = minIdle;

  124. }

  125. public int getMaxActive() {

  126. return maxActive;

  127. }

  128. public void setMaxActive(int maxActive) {

  129. this.maxActive = maxActive;

  130. }

  131. public int getMaxWait() {

  132. return maxWait;

  133. }

  134. public void setMaxWait(int maxWait) {

  135. this.maxWait = maxWait;

  136. }

  137. public int getTimeBetweenEvictionRunsMillis() {

  138. return timeBetweenEvictionRunsMillis;

  139. }

  140. public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {

  141. this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;

  142. }

  143. public int getMinEvictableIdleTimeMillis() {

  144. return minEvictableIdleTimeMillis;

  145. }

  146. public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {

  147. this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;

  148. }

  149. public String getValidationQuery() {

  150. return validationQuery;

  151. }

  152. public void setValidationQuery(String validationQuery) {

  153. this.validationQuery = validationQuery;

  154. }

  155. public boolean isTestWhileIdle() {

  156. return testWhileIdle;

  157. }

  158. public void setTestWhileIdle(boolean testWhileIdle) {

  159. this.testWhileIdle = testWhileIdle;

  160. }

  161. public boolean isTestOnBorrow() {

  162. return testOnBorrow;

  163. }

  164. public void setTestOnBorrow(boolean testOnBorrow) {

  165. this.testOnBorrow = testOnBorrow;

  166. }

  167. public boolean isTestOnReturn() {

  168. return testOnReturn;

  169. }

  170. public void setTestOnReturn(boolean testOnReturn) {

  171. this.testOnReturn = testOnReturn;

  172. }

  173. public boolean isPoolPreparedStatements() {

  174. return poolPreparedStatements;

  175. }

  176. public void setPoolPreparedStatements(boolean poolPreparedStatements) {

  177. this.poolPreparedStatements = poolPreparedStatements;

  178. }

  179. public int getMaxPoolPreparedStatementPerConnectionSize() {

  180. return maxPoolPreparedStatementPerConnectionSize;

  181. }

  182. public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {

  183. this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;

  184. }

  185. public String getFilters() {

  186. return filters;

  187. }

  188. public void setFilters(String filters) {

  189. this.filters = filters;

  190. }

  191. public String getConnectionProperties() {

  192. return connectionProperties;

  193. }

  194. public void setConnectionProperties(String connectionProperties) {

  195. this.connectionProperties = connectionProperties;

  196. }

  197. }

  198. }

application.properties Druid和Mybatis配置

  1. #数据库配置

  2. #连接池类型 阿里巴巴Druid

  3. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

  4. #数据库地址

  5. spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 数据库IP地址)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = 更换成你的) ))

  6. spring.datasource.driver- class-name=oracle.jdbc.driver.OracleDriver

  7. spring.datasource.username=用户名

  8. spring.datasource.password=密码

  9. # 初始化大小,最小,最大

  10. spring.datasource.initialSize= 1

  11. spring.datasource.minIdle= 3

  12. spring.datasource.maxActive= 20

  13. # 配置获取连接等待超时的时间

  14. spring.datasource.maxWait= 60000

  15. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

  16. spring.datasource.timeBetweenEvictionRunsMillis= 60000

  17. # 配置一个连接在池中最小生存的时间,单位是毫秒

  18. spring.datasource.minEvictableIdleTimeMillis= 30000

  19. spring.datasource.validationQuery= select 1 from dual

  20. spring.datasource.testWhileIdle= true

  21. spring.datasource.testOnBorrow= false

  22. spring.datasource.testOnReturn= false

  23. # 打开PSCache,并且指定每个连接上PSCache的大小

  24. spring.datasource.poolPreparedStatements= true

  25. spring.datasource.maxPoolPreparedStatementPerConnectionSize= 20

  26. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙

  27. spring.datasource.filters=stat,wall,slf4j

  28. # 通过connectProperties属性来打开mergeSql功能;慢SQL记录

  29. spring.datasource.connectionProperties=druid.stat.mergeSql= true;druid.stat.slowSqlMillis=5000

  30. #mapper.xml文件 配置

  31. #mybatis.config-location=classpath:mybatis/SQLMapConfig.xml

  32. mybatis.mapper-locations=classpath:mybatis/*.xml

查看Druid的各种信息:

配置完成后启动项目可以访问:http://localhost:8080/druid

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
3年前
SpringBoot+Druid+Mybatis连接Oracle
最近有一个新项目需要开发搭建了个SpringBoot框架记录一下!Oracle连接jar编译到maven仓库参考:Maven编译jar包到本地仓库(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fwzsy_ll%2Farticle%2Fdetails
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这