Spring連接Mysql數(shù)據(jù)庫全過程
Spring連接Mysql數(shù)據(jù)庫
創(chuàng)建一個(gè)Maven項(xiàng)目
導(dǎo)入坐標(biāo)
在pom.xml加入如下坐標(biāo),并且點(diǎn)擊右上角刷新。
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ù)庫名 d.setUsername("root"); //設(shè)置賬號(hào) d.setPassword("root"); //設(shè)置密碼 return d; } }
托管DataSource類
創(chuàng)建名為AppConfig類。托管DataSource類,加上@Configuration注解。注意設(shè)置所指定的連接數(shù)據(jù)庫的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ù)庫名 d.setUsername("root"); //設(shè)置賬號(hào) d.setPassword("root"); //設(shè)置密碼 return d; } }
測(cè)試
創(chuàng)建一個(gè)Test類 。通過DataSource獲取數(shù)據(jù)庫連接。并且輸出。
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)如下代碼,即為連接成功。
Spring和Mysql數(shù)據(jù)庫的連接及測(cè)試--Jdbc
普遍的開發(fā)中, 通常使用會(huì)用到Spring框架和Mysql數(shù)據(jù)庫 , 下面分享個(gè)人的Mysql在Spring中的配置以及它的連接測(cè)試.
本人在開發(fā)中 , 使用的是Maven管理工具 . ( 關(guān)于Maven百度有詳細(xì)安裝配置教程, )
創(chuàng)建Maven Web 的java工程
( 本人前面有文章講過創(chuàng)建Maven Web項(xiàng)目 ), 創(chuàng)建完成后在pom.xml中加入mysql驅(qū)動(dòng)相關(guān)的jar包
pom.xml中的配置為:
?? ?<!-- 數(shù)據(jù)庫驅(qū)動(dòng) --> ?? ?<dependency> ?? ??? ?<groupId>mysql</groupId> ?? ??? ?<artifactId>mysql-connector-java</artifactId> ?? ??? ?<version>5.1.38</version> ?? ?</dependency> ?? ? ?? ?<!--spring對(duì)jdbc的支持包 --> ?? ?<dependency> ?? ??? ?<groupId>org.springframework</groupId> ?? ??? ?<artifactId>spring-jdbc</artifactId> ?? ??? ?<version>${spring.version}</version> ?? ?</dependency> ?? ? ?? ?<!-- spring連接數(shù)據(jù)庫 --> ?? ?<dependency> ?? ??? ?<groupId>org.springframework</groupId> ?? ??? ?<artifactId>spring-jdbc</artifactId> ?? ??? ?<version>4.3.2.RELEASE</version> ?? ?</dependency> ?? ? ?? ?<!-- spring測(cè)試包 --> ?? ?<dependency> ?? ??? ?<groupId>org.springframework</groupId> ?? ??? ?<artifactId>spring-test</artifactId> ?? ??? ?<version>4.3.2.RELEASE</version> ?? ?</dependency> ?? ? ?? ?<!-- springMVC --> ?? ?<dependency> ?? ??? ?<groupId>org.springframework</groupId> ?? ??? ?<artifactId>spring-webmvc</artifactId> ?? ??? ?<version>4.3.2.RELEASE</version> ?? ?</dependency> ? ? ?? ?<dependency> ?? ??? ?<groupId>org.springframework</groupId> ?? ??? ?<artifactId>spring-context-support</artifactId> ?? ??? ?<version>4.3.2.RELEASE</version> ?? ?</dependency>
配置db.properties: ( 這里的屬性值可以直接寫入spring.xml中 ,與 ${ } 的值相對(duì)應(yīng))
<span style="white-space:pre">?? ?</span>#在這里如果引入的mysql jar包為6.0版本 , 驅(qū)動(dòng)值為 : ?com.mysql.cj.jdbc.Driver <span style="white-space:pre">?? ?</span>jdbc.driverClassName=com.mysql.jdbc.Driver <span style="white-space:pre">?? ?</span>jdbc.url=jdbc:mysql://localhost:3306/usersystem <span style="white-space:pre">?? ?</span>jdbc.username=root <span style="white-space:pre">?? ?</span>jdbc.password=111
spring.xml中的配置為:
?? ?<!-- 引入外部的屬性文件 --> ? ? ?? ?<context:property-placeholder location="classpath:db.properties"/> ? ?? ?<!-- jdbc連接設(shè)置 --> ?? ?<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ?? ??? ?<property name="driverClassName" value="${jdbc.driverClassName}" /> ?? ??? ?<property name="url" value="${jdbc.url}" /> ?? ??? ?<property name="username" value="${jdbc.username}" /> ?? ??? ?<property name="password" value="${jdbc.password}" /> ?? ?</bean>
創(chuàng)建一個(gè)測(cè)試類: ConnTest.java
?? ?package com.lsy.conn.test; ? ?? ?import static org.junit.Assert.assertNotNull; ? ?? ?import java.sql.Connection; ?? ?import java.sql.SQLException; ? ?? ?import javax.sql.DataSource; ? ?? ?import org.junit.Test; ?? ?import org.junit.runner.RunWith; ?? ?import org.springframework.beans.factory.annotation.Autowired; ?? ?import org.springframework.test.context.ContextConfiguration; ?? ?import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ? ?? ?@RunWith(SpringJUnit4ClassRunner.class) ?? ?@ContextConfiguration("classpath:spring.xml") ?? ?public class ConnTest { ?? ? ?? ??? ?@Autowired ?? ??? ?private DataSource dataSource; ?? ? ?? ??? ?@Test ?? ??? ?public void testConn() { ?? ??? ??? ?Connection con = null; ?? ??? ??? ?try { ?? ??? ??? ??? ?con = dataSource.getConnection(); ?? ??? ??? ?} catch (SQLException e) { ?? ??? ??? ??? ?throw new RuntimeException("連接失敗!!!", e); ?? ??? ??? ?} ?? ??? ??? ?assertNotNull(con); ?? ??? ?} ?? ? ?? ?}
祝大家配置成功!!!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot創(chuàng)建多模塊項(xiàng)目的全過程記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot創(chuàng)建多模塊項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java RPC框架如何實(shí)現(xiàn)客戶端限流配置
這篇文章主要介紹了Java RPC框架如何實(shí)現(xiàn)客戶端限流配置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringBoot3整合mybatis-plus的實(shí)現(xiàn)
MyBatis-Plus是一個(gè)MyBatis的增強(qiáng)工具,在MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,本文主要介紹了Mybatis-Plus3.x的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Java使用延時(shí)隊(duì)列搞定超時(shí)訂單處理的場(chǎng)景
這篇文章主要介紹了Java使用延時(shí)隊(duì)列搞定超時(shí)訂單處理,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08解決IDEA啟動(dòng)springboot項(xiàng)目報(bào)錯(cuò)java.lang.ClassNotFoundException:?jav
這篇文章主要介紹了解決IDEA啟動(dòng)springboot項(xiàng)目報(bào)錯(cuò)java.lang.ClassNotFoundException:?javax.servlet.ServletContext問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤
本篇文章主要介紹了淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01SWT(JFace)體驗(yàn)之ApplicationWindow
SWT(JFace)體驗(yàn)之ApplicationWindow2009-06-06Spring Boot 整合持久層之JdbcTemplate
持久層是 Java EE 中訪問數(shù)據(jù)庫的核心操作,Spring Boot 中對(duì)常見的持久層框架都提供了自動(dòng)化配置,例如 JdbcTemplate 、 JPA 等,Mybatis 的自動(dòng)化配置則是 Mybatis 官方提供的2022-08-08