SpringBoot+Druid+Mybatis连接Oracle

Easter79
• 阅读 519

最近有一个新项目需要开发搭建了个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

点赞
收藏
评论区
推荐文章
光头强的博客 光头强的博客
2个月前
Java面向对象试题
1、 请创建一个Animal动物类,要求有方法eat()方法,方法输出一条语句“吃东西”。 创建一个接口A,接口里有一个抽象方法fly()。创建一个Bird类继承Animal类并实现 接口A里的方法输出一条有语句“鸟儿飞翔”,重写eat()方法输出一条语句“鸟儿 吃虫”。在Test类中向上转型创建b对象,调用eat方法。然后向下转型调用eat()方
刚刚好 刚刚好
2个月前
css问题
1、 在IOS中图片不显示(给图片加了圆角或者img没有父级) <div<img src""/</div div {width: 20px; height: 20px; borderradius: 20px; overflow: h
blmius blmius
1年前
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:SQL Mode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。 全局s
小森森 小森森
2个月前
校园表白墙微信小程序V1.0 SayLove -基于微信云开发-一键快速搭建,开箱即用
后续会继续更新,敬请期待2.0全新版本 欢迎添加左边的微信一起探讨!项目地址:](https://www.aliyun.com/activity/daily/bestoffer?userCodesskuuw5n) \2. Bug修复更新日历 2. 情侣脸功能大家不要使用了,现在阿里云的接口已经要收费了(土豪请随意), \ \ 和 注意
晴空闲云 晴空闲云
2个月前
css中box-sizing解放盒子实际宽高计算
我们知道传统的盒子模型,如果增加内边距padding和边框border,那么会撑大整个盒子,造成盒子的宽度不好计算,在实务中特别不方便。boxsizing可以设置盒模型的方式,可以很好的设置固定宽高的盒模型。 盒子宽高计算假如我们设置如下盒子:宽度和高度均为200px,那么这会这个盒子实际的宽高就都是200px。但是当我们设置这个盒子的边框和内间距的时候,那
艾木酱 艾木酱
1个月前
快速入门|使用MemFire Cloud构建React Native应用程序
> MemFire Cloud是一款提供云数据库,用户可以创建云数据库,并对数据库进行管理,还可以对数据库进行备份操作。它还提供后端即服务,用户可以在1分钟内新建一个应用,使用自动生成的API和SDK,访问云数据库、对象存储、用户认证与授权等功能,可专
Stella981 Stella981
1年前
SpringBoot+Druid+Mybatis连接Oracle
最近有一个新项目需要开发搭建了个SpringBoot框架 记录一下! Oracle连接jar编译到maven仓库参考:[Maven编译jar包到本地仓库](https://www.oschina.net/action/GoToLink?url=https%3A%2F%2Fblog.csdn.net%2Fwzsy_ll%2Farticle%2Fdetails
Wesley13 Wesley13
1年前
MySQL查询按照指定规则排序
1.按照指定(单个)字段排序 select * from table_name order id desc; 2.按照指定(多个)字段排序 select * from table_name order id desc,status desc; 3.按照指定字段和规则排序 selec
Wesley13 Wesley13
1年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
#### 背景描述 # Time: 2019-01-24T00:08:14.705724+08:00 # User@Host: **[**] @ [**] Id: ** # Schema: sentrymeta Last_errno: 0 Killed: 0 # Query_time: 0.315758 Lock_
helloworld_28799839 helloworld_28799839
2个月前
常用知识整理
# Javascript ## 判断对象是否为空 ```js Object.keys(myObject).length === 0 ``` ## 经常使用的三元运算 > 我们经常遇到处理表格列状态字段如 `status` 的时候可以用到 ``` vue