栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot数据源配置【druid、Hikari】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot数据源配置【druid、Hikari】

测试配置的代码

@SpringBootTest(classes = App.class)
public class AppTest 
{

    @Autowired
    DataSource dataSource;
    @Test
    public void testIsConnect() throws SQLException {
        System.out.println(dataSource.getClass());

        Connection connection = dataSource.getConnection();
        //输出结果:HikariProxyConnection@157783888 wrapping com.mysql.cj.jdbc.ConnectionImpl@9b9a327,能够获取连接
        System.out.println(connection);

        connection.close();
    }

}

(1)Hikari配置

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/yeb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: 123456
    hikari:
      #连接池名
      pool-name: DateHikariCP
      #最小空闲连接数
      minimum-idle: 5
      #空闲连接存活最大时间,默认600000(10分钟)
      idle-timeout: 180000
      #最大连接数,默认10
      maximum-pool-size: 10
      #从连接池返回的连接的自动提交
      auto-commit: true
      #连接最大存活时间,0表示永久存活,默认1800000 (30分钟)
      max-lifetime: 1800000
      #连接超时时|间,默认30000 (30秒)
      connection-timeout: 30000
      #测试连接是否可用的查询语句
      connection-test-query: SELECt 1
#Mybatis-plus配置
mybatis-plus:
  #配置Mapper映射文件
  mapper-locations: classpath*:/mapper/*Mapper.xml
  #配置MyBatis数据返回类型别名(默认别名是类名>
  type-aliases-package: com.dlnu.pojo
  configuration:
    #自动驼峰命名
    map-underscore-to-camel-case : false
logging:
  level:
    com.dlnu.mapper: debug
jwt:
  # Jwt存储的请求头
  tokenHeader: Authorization
  # Jwt加密秘钥
  secret: yeb-secret
  # Jwt 的超期限时间(60*60)*24
  expiration: 604800
  # Jwt负载中拿到开头
  tokenHead: Bearer

运行结果:

Druid配置:

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/yeb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    #指定必须保持连接的最小值
    minIdle: 5
    #指定连接池中最大的活跃连接数.
    maxActive: 20
    #指定连接池等待连接返回的最大等待时间,毫秒单位.
    maxWait: 60000
    #空闲连接回收的时间间隔,与test-while-idle一起使用,设置1分钟
    timeBetweenEvictionRunsMillis: 60000
    #连接池空闲连接的有效时间 ,设置5分钟
    minEvictableIdleTimeMillis: 300000
    #测试连接是否可用的查询语句
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    #获取连接时候验证,会影响性能
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#Mybatis-plus配置
mybatis-plus:
  #配置Mapper映射文件
  mapper-locations: classpath*:/mapper/*Mapper.xml
  #配置MyBatis数据返回类型别名(默认别名是类名>
  type-aliases-package: com.dlnu.pojo
  configuration:
    #自动驼峰命名
    map-underscore-to-camel-case : false
logging:
  level:
    com.dlnu.mapper: debug
jwt:
  # Jwt存储的请求头
  tokenHeader: Authorization
  # Jwt加密秘钥
  secret: yeb-secret
  # Jwt 的超期限时间(60*60)*24
  expiration: 604800
  # Jwt负载中拿到开头
  tokenHead: Bearer

输出结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/724040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号