山东大学 暑期实训 云主机服务比价与预测系统(七)数据库连接
制作一个系统,一个优秀的数据库是必不可少的,数据库的连接在Spring boot 中又是怎样的
由于我们采用了Springboot架构,因此我们首先可以将数据库的相关信息写入yml配置文件中,关于配置文件我们在前面讲到过有properties和yml大致两种形式,且当创立maven工程后在resources文件夹下有一个初始的properties配置文件,但是由于yml具有:式的树状结构,方便简洁,因此我们采用yml文件,我们在同一目录新建一个yml配置文件。
在文件中写入数据库配置信息如下。
spring:
datasource:
username: xxxxxx
password: xxxxxx
url: jdbc:mysql://xxx.xx.xxx.xx:3306/webspider?serverTimezone=UTC&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
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,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
#切换数据源
type: com.alibaba.druid.pool.DruidDataSource
druid:
test-on-borrow: true
test-while-idle: true
jpa:
hibernate:
naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
freemarker:
suffix=: html
mvc:
hiddenmethod:
filter:
enabled: true
#spring.datasource.max-idle=10
写入配置信息后准备将后端与数据库相连接。