您现在的位置是:首页 > 博客日记 > Java Java

springBoot配置mybatis链接数据库

2019-07-10 18:49:46 【Java】 人已围观

springBoot配置mybatis链接数据库

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


关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()