spring boot连接mysql数据库
Posted 田海超
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot连接mysql数据库相关的知识,希望对你有一定的参考价值。
一、现在大部分持久化层ORM实现框架用的都是mybatis,spring Boot开始使用之后,mybatis官网也发布了mybatis-spring-boot-starter包来支持springboot集成开发,在jdbc连接数据库的spring-boot-starter-jdbc包中,mybatis默认选择了引入第三方数据库连接池工具HiKariCp
这样做应该是考虑到Hikari是目前公认的连接性能最强的连接池工具。
二、如果不单单考虑性能,按综合能力考量 ,阿里的Druid其实是功能最强大的数据库连接池工具。当然,阿里也为我们提供了支持springboot开发的starter包Druid-spring-boot-starter。
我个人比较偏向集成Druid做数据库连接池工作,虽然需要单独引入Druid的starter包,但是他的监控功能确实做的很好。
三、下面来具体分析下
1、首先分析下Hikari和Druid
参考博文 :https://blog.csdn.net/qq_17085463/article/details/90486515
总结来说,Hikari速度快,Druid功能强
2、具体使用:
参考博文:https://blog.csdn.net/weixin_38187317/article/details/81562571
github路径:mybatis-spring-boot-starter https://github.com/mybatis/spring-boot-starter
Druid-spring-boot-starter https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
关于Druid的中文说明:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
如果使用mybatis的默认连接池Hikari,只需要引入pom依赖和配置application.properties文件如下:
#spring.datasource.driver-class-name:com.mysql.cj.jdbc.Driver #mybaits扫描设置 mybatis.config-location=classpath:config/mybatis-config.xml mybatis.mapper-locations=classpath*:dbmapper/*.xml # 解析结果集对应的model路径,当然,如果在mapper.xml中写全限定名就不用配置这里 # mybatis.type-aliases-package=com.example.mybatisdemo.db.model # mybatis.configuration.mapped-statements=com.example.mybatisdemo.db.dao #数据连接设置 spring.datasource.url:jdbc:mysql://192.*.100.29:3306/testdb?useUnicode=true&characterEncoding=utf-8 spring.datasource.username:root spring.datasource.password:root
<properties> <java.version>1.8</java.version> <plugin.mybatis.generator>1.3.1</plugin.mybatis.generator> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${plugin.mybatis.generator}</version> <configuration> <configurationFile>src/main/resources/config/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build>
如果使用mybatis外接连接池Druid,则需要配置如下:
#spring.datasource.driver-class-name:com.mysql.cj.jdbc.Driver #mybaits扫描设置 mybatis.config-location=classpath:config/mybatis-config.xml mybatis.mapper-locations=classpath*:dbmapper/*.xml #数据连接设置 server.port=8080 spring.application.name=springboot-test-exam1 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url:jdbc:mysql://192.*.*.29:3306/testdb?useUnicode=true&characterEncoding=utf-8 spring.datasource.username:root spring.datasource.password:root spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.maxActive=20 spring.datasource.druid.maxWait=60000 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000 spring.datasource.druid.minEvictableIdleTimeMillis=300000 spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL spring.datasource.druid.testWhileIdle=true spring.datasource.druid.testOnBorrow=false spring.datasource.druid.testOnReturn=false spring.datasource.druid.poolPreparedStatements=true spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.druid.filters=stat,wall,slf4j spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/* spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,192.168.163.1 spring.datasource.druid.stat-view-servlet.deny=192.168.1.73 spring.datasource.druid.stat-view-servlet.reset-enable=false spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=123456
<properties> <java.version>1.8</java.version> <plugin.mybatis.generator>1.3.1</plugin.mybatis.generator> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- DruidDataSource --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${plugin.mybatis.generator}</version> <configuration> <configurationFile>src/main/resources/config/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build>
Druid监控信息访问路径:http://localhost:8080/druid/
3、源码底层分析
通过配置错误的数据库连接信息会发现
1)、项目启动过程中,Druid在初始化Datasource时会对数据库进行测试连接而Hikari并没有。
2)、两者都是在解析完sql之后进行的数据库连接,如果解析错误,则不进行数据库连接。
我写的一篇浅跟mybaits中使用的连接池就是Druid
下面在来简单看看Hikari的源码跟踪:
以上是关于spring boot连接mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章