查看“Spring Boot连MySQL”的源代码
←
Spring Boot连MySQL
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV19K4y1L7MT/?p=60 === pom.xml === ==== 继承 ==== <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> …… <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.0</version> </parent> …… </project> </syntaxhighlight> ==== 添加依赖 ==== <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> …… <dependencies> …… <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> …… </dependencies> …… </project> </syntaxhighlight> spring-boot-starter-data-jdbc 提供了 spring 对于连接关系型数据库进行封装的类库,但并没有提供具体连接数据库的驱动(driver)。 具体使用什么驱动、哪个驱动又取决于要连哪个数据库、什么版本的数据库。 比如:本例连 MySQL 数据库,就是 mysql-connector-java。 如果要连其他数据库,比如 MS SQL Server,就在 pom.xml 继承的 spring-boot-starter-parent 继承的 spring-boot-dependencies 里找,找到:<syntaxhighlight lang="xml"> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>${mssql-jdbc.version}</version> </dependency> </syntaxhighlight>就是了,又比如 PostgreSQL ,就是:<syntaxhighlight lang="xml"> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> </dependency> </syntaxhighlight> 引入当前项目就可以了。 因为本例中 spring-boot-dependencies 里配置的 mysql-connector-java 版本是:<syntaxhighlight lang="xml"> <mysql.version>8.0.22</mysql.version> </syntaxhighlight> ===== 有的地方说还要指定 mysql-connector-java 的 version,才可以良好兼容,但其实不用 ===== 比如:要连的 MySQL Server 版本是 5.7.35-log,就要找对应的 version 为 5 或 5.7 后子版本号最大值的,但其实可以在 mysql-connector-java 的 pom 中提供的官网:<syntaxhighlight lang="xml"> <url>http://dev.mysql.com/doc/connector-j/en/</url> </syntaxhighlight>中有说明: [[文件:Spring Boot连MySQL1.png|无|缩略图|900x900像素]] 所以就这么使用没问题。 === application.xml === <syntaxhighlight lang="yaml"> spring: datasource: url: jdbc:mysql://127.0.0.1:3306/guns username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver # type: com.zaxxer.hikari.HikariDataSource </syntaxhighlight> ===== 去哪找 driver-class-name? ===== 引入的驱动包 mysql-connector-java 里 META-INF 里 services 的 java.sql.Driver 文本文件中有:<syntaxhighlight lang="console"> com.mysql.cj.jdbc.Driver </syntaxhighlight> ===== 去哪找 url? ===== 驱动包的 readMe、docs 的文本文件 官网的示例里:https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-examples.html 默认使用的 数据源/连接池 实现就是 Hikari 可以查看 spring-boot-autoconfigure-x.x.x.jar 中的自动配置类:<syntaxhighlight lang="java"> package org.springframework.boot.autoconfigure.jdbc; …… abstract class DataSourceConfiguration { @Configuration(proxyBeanMethods = false) @ConditionalOnClass(HikariDataSource.class) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true) static class Hikari { @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") HikariDataSource dataSource(DataSourceProperties properties) { HikariDataSource dataSource = createDataSource(properties, HikariDataSource.class); if (StringUtils.hasText(properties.getName())) { dataSource.setPoolName(properties.getName()); } return dataSource; } } …… </syntaxhighlight> === 注入使用 === 默认注入 Spring 容器的数据源(默认使用了 hikari 的数据库连接池实现)的 bean name 是 dataSource
返回至
Spring Boot连MySQL
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息