看了网上很多关于SpringBoot+Mybatis+Druid+Mysql(Oracle)多数据源监控配置demo,但是始终不能找到一个合适并且完整的搭建过程,以及分析下搭建demo的问题。
配置
1.项目结构
config目录下是demo主要的配置
2.pom.xml文件中引入依赖
<!-- springboot版本依赖 1.4.1.RELEASE-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<properties>
<!--<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> -->
<!-- jdk1.8以上这里一定要配置上java的版本,如果是1.7版本的可不用配置 -->
</properties>
<dependencies>
<!-- springboot核心包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!-- springboot-aop包,AOP切面注解,Aspectd等相关注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.1关键的依赖包版本dmeo中适配中可能会碰到的问题
- ojdbc6 包与 druid 包匹配,经过demo检验不会出现程序异常的问题
- 引用druid-spring-boot-starter 而不是 druid 包 ,此处包出错的话,会导致程序可以正常启动,但是Druid SQL Stat无法监控到SQL
- mysql 包 与druid 包,引用demo的版本可正常启动
- druid-spring-boot-starter 包 demo版本比较新
3.修改src/main/resources/application.properties文件
#servername
server.context-path=/zhpt
#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#mybatis
mybatis.type-aliases-package=com.erxi.ms.domain
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=3000
mybatis.mapperLocations=classpath:com/erxi/ms/dao/*.xml
#druid
spring.datasource.type=com.alibaba.druid.poo