Springboot-Druid-Mybatis

Springboot-Druid-Mybatis

Springboot+Druid+Mybatis整合

  1. 添加依赖

在pom.xml中,确保已经添加了相关依赖。例如:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
  1. 配置application.yml文件

配置数据源、Druid和Mybatis相关设置。例如

spring:
    application:
        name: sprringboot-druid-mybatis-demo
    datasource: #druid数据源配置
        druid:
            url: jdbc:mysql://127.0.0.1/test?useSSL=false&useUnicode=true&characterEncoding=utf8
            username: test
            password: 123456
            driver-class-name: com.mysql.cj.jdbc.Driver
            max-wait: 10000 #获取连接时最大等待时间,单位毫秒
            initial-size: 10 #初始化时建立物理连接的个数
            max-active: 100 #最大连接池数量
            min-idle: 10 #最小连接数
            time-between-eviction-runs-millis: 3600000 #检测连接的间隔时间
            min-evictable-idle-time-millis: 300000 #连接的最小生存时间
            test-while-idle: true  #检测连接是否有效
            test-on-borrow: false #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
            validation-query: SELECT 1 #用来检测连接是否有效的sql
            filters: stat #监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
            stat-view-servlet:
                url-pattern: /druid/*
                reset-enable: true #允许清空统计数据
                login-username: root #监控后台管理账号和密码
                login-password: root
            web-stat-filter:
                url-pattern: /*
                exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
mybatis:
    mapper-locations: classpath*:mapper/*Mapper.xml

logging:
    file:
        name: /var/log/${spring.application.name}.log
    logback:
        rollingpolicy:
            file-name-pattern: /var/log/${spring.application.name}.%d{yyyy-MM-dd}.%i.gz