Spring連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
一、創(chuàng)建一個(gè)Maven項(xiàng)目

二、導(dǎo)入坐標(biāo)
在pom.xml加入如下坐標(biāo),并且點(diǎn)擊右上角刷新。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>

三、托管DataSource類
創(chuàng)建名為AppConfig類。托管DataSource類,加上@Configuration注解。注意設(shè)置所指定的連接數(shù)據(jù)庫(kù)的url,用戶名,和密碼。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource(){
DriverManagerDataSource d = new DriverManagerDataSource() ;
d.setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC"); //設(shè)置url
// 上述的test為你的數(shù)據(jù)庫(kù)名
d.setUsername("root"); //設(shè)置賬號(hào)
d.setPassword("root"); //設(shè)置密碼
return d;
}
}

四、測(cè)試
創(chuàng)建一個(gè)Test類 。通過(guò)DataSource獲取數(shù)據(jù)庫(kù)連接。并且輸出。
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) throws SQLException {
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
DataSource d = (DataSource) ac.getBean("dataSource");
Connection c = d.getConnection(); //獲取連接
System.out.println(c);
}
}
控制臺(tái)出現(xiàn)如下代碼,即為連接成功。

到此這篇關(guān)于Spring連接Mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Spring連接Mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot高級(jí)教程之Spring Boot連接MySql數(shù)據(jù)庫(kù)
- spring boot配置MySQL數(shù)據(jù)庫(kù)連接、Hikari連接池和Mybatis的簡(jiǎn)單配置方法
- SpringBoot連接MYSQL數(shù)據(jù)庫(kù)并使用JPA進(jìn)行操作
- springboot配置mysql連接的實(shí)例代碼
- 教你用springboot連接mysql并實(shí)現(xiàn)增刪改查
- Springboot2.0配置JPA多數(shù)據(jù)源連接兩個(gè)mysql數(shù)據(jù)庫(kù)方式
- SpringBoot集成Druid連接池連接MySQL8.0.11
相關(guān)文章
springboot集成fastDfs過(guò)程代碼實(shí)例
這篇文章主要介紹了springboot集成fastDfs過(guò)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
基于Protobuf動(dòng)態(tài)解析在Java中的應(yīng)用 包含例子程序
下面小編就為大家?guī)?lái)一篇基于Protobuf動(dòng)態(tài)解析在Java中的應(yīng)用 包含例子程序。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
springboot如何使用logback-spring配置日志格式,并分環(huán)境配置
這篇文章主要介紹了springboot如何使用logback-spring配置日志格式,并分環(huán)境配置的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
基于ReentrantLock的實(shí)現(xiàn)原理講解
這篇文章主要介紹了ReentrantLock的實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
spring mvc高級(jí)技術(shù)實(shí)例詳解
前面學(xué)習(xí)了簡(jiǎn)單的Spring Web知識(shí),接著學(xué)習(xí)更高階的Web技術(shù)。下面這篇文章主要給大家介紹了spring mvc高級(jí)技術(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起看看吧2018-09-09
解決Java的InputMismatchException異常
這篇文章介紹了解決Java的InputMismatchException異常的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

