springboot配置数据库相关信息

1. pom.xml文件引入相关jar包

<!-- mysql连接 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
</dependency>

<!-- druid连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.20</version>
</dependency>

如果引入

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.20</version>
</dependency>

会有警告,提示:Cannot resolve configuration property 'spring.datasource.XXX'。

网上都说没什么影响,但是终归不美观,不优雅。

2. application.xml配置

# 端口
server.port=12116

# 数据库访问配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/wxsd-lantern-festival?useUnicode=true&characterEncoding=utf-8&useSSL=false

# innoDB引擎
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# 指定目标数据库
spring.jpa.database=mysql
# 指定DDL mode (none, validate, update, create, create-drop)【慎用】
#spring.jpa.hibernate.ddl-auto=none
# 无修改命名
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# 遇到大写字母 加”_”的命名
#spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
# 是否打印sql
spring.jpa.show-sql=true
# 默认为true,可以在视图渲染期间执行数据库查询
spring.jpa.open-in-view=false

# 下面为连接池的补充设置,应用到上面所有数据源中 
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
# 配置获取连接等待超时的时间
spring.datasource.druid.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.druid.filters=stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.druid.use-global-data-source-stat=true

目前没有配置redis,后期有需要再加。

2020-07-13 添加redis配置

  1. pom.xml文件引入

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
</dependency>

  2. application.xml配置

# redis配置
spring.redis.host=localhost
spring.redis.port=6379
# redis数据库索引(默认为0)
spring.redis.password=6369
# redis数据库索引(默认为0)
spring.redis.database=0
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=30000
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=100
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=20
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000
# 禁用redis的repositories
spring.data.redis.repositories.enabled=false
  •  为什么要加 commons-pool2 依赖呢?
    • 因为redis在1.x版本的时候,默认使用的是Jedis;在2.x版本中,默认是使用lettuce。
  • 他们的区别是什么呢?
    • Jedis和Lettuce都是Redis Client;
    • Jedis 是直连模式,在多个线程间共享一个 Jedis 实例时是线程不安全的。如果想要在多线程环境下使用 Jedis,需要使用连接池。每个线程都去拿Jedis 实例,当连接数量增多时,物理连接成本就较高了;
    • Lettuce 是基于 netty 的,netty 是一个多线程、事件驱动的 I/O 框架,连接实例可以在多个线程间共享,通过异步的方式可以让我们更好的利用系统资源,而不用浪费线程等待网络或磁盘I/O;

 对了,这篇文章的配置介绍挺全的,附上链接 https://www.cnblogs.com/toughzcf/p/9835867.html

胸藏文墨怀若谷,腹有诗书气自华。

原文地址:https://www.cnblogs.com/wq-code/p/12090098.html