springBoot配置mybatis链接数据库
添加pom包,修改 pom.xml
文件
<!--mybatis-spring适配器 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
修改配置文件application.yml
#启动端口
server:
port: 8001
spring:
#配置数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
dbcp2:
#验证连接的有效性
test-while-idle: true
#验证语句
validation-query: SELECT 1
#空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
time-between-eviction-runs-millis: 300000
#连接池空闲连接的有效时间 ,设置30分钟
soft-min-evictable-idle-time-millis: 1800000
hikari:
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired)
# max-lifetime: 60000
#连接池中允许的最大连接数
maximum-pool-size: 15
#<!-- 生效超时 -->
validation-timeout: 3000
#连接只读数据库时配置为true, 保证安全
read-only: false
#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException
connection-timeout: 60000
#一个连接idle状态的最大时长(毫秒),超时则被释放(retired)
idle-timeout: 60000
# 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQLwait_timeout参数(show variables like '%timeout%';)
max-lifetime: 120000
#mybatis配置
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml #修改为对应的mapper文件路径
config-location: classpath:mybatis/mybatis-config.xml