当前位置 主页 > 网站技术 > 代码类 >

    SpringBoot整合Druid数据源过程详解

    栏目:代码类 时间:2019-12-02 12:07

    这篇文章主要介绍了SpringBoot整合Druid数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    1.数据库结构

    2.项目结构

    3.pom.xml文件

    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
      </dependency>
     
      <!--引入druid数据源 -->
      <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.8</version>
      </dependency>
     
      <!-- https://mvnrepository.com/artifact/log4j/log4j -->
      <!-- 如果 不加入这依赖    配置监控统计拦截的filters时  这个会报错 filters: stat,wall,log4j  -->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
     
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
     
     
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
     
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build> 

    4.application.yml配置文件

    spring:
     datasource:
      username: root
      password: wangqing
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
     
     
     #  数据源其他配置
      initialSize: 5
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
    #  配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,log4j
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
      # 合并多个DruidDataSource的监控数据
      #useGlobalDataSourceStat: true
       
    mybatis:
     # 指定全局配置文件位置
     #config-location: classpath:mybatis/mybatis-config.xml
     # 指定sql映射文件位置
     mapper-locations: classpath:mapper/*.xml      #如src/main/resources下的mappers文件下的TUserMapper.xml
     
    #  schema:
    #   - classpath:sql/department.sql     #根据department.sql 的sql语句创建表
    #   - classpath:sql/employee.sql