Spring連接Mysql數(shù)據(jù)庫全過程
Spring連接Mysql數(shù)據(jù)庫
創(chuàng)建一個Maven項目
導(dǎo)入坐標(biāo)
在pom.xml加入如下坐標(biāo),并且點擊右上角刷新。
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è)置賬號 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è)置賬號 d.setPassword("root"); //設(shè)置密碼 return d; } }
測試
創(chuàng)建一個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); } }
控制臺出現(xiàn)如下代碼,即為連接成功。
Spring和Mysql數(shù)據(jù)庫的連接及測試--Jdbc
普遍的開發(fā)中, 通常使用會用到Spring框架和Mysql數(shù)據(jù)庫 , 下面分享個人的Mysql在Spring中的配置以及它的連接測試.
本人在開發(fā)中 , 使用的是Maven管理工具 . ( 關(guān)于Maven百度有詳細(xì)安裝配置教程, )
創(chuàng)建Maven Web 的java工程
( 本人前面有文章講過創(chuàng)建Maven Web項目 ), 創(chuàng)建完成后在pom.xml中加入mysql驅(qū)動相關(guān)的jar包
pom.xml中的配置為:
?? ?<!-- 數(shù)據(jù)庫驅(qū)動 --> ?? ?<dependency> ?? ??? ?<groupId>mysql</groupId> ?? ??? ?<artifactId>mysql-connector-java</artifactId> ?? ??? ?<version>5.1.38</version> ?? ?</dependency> ?? ? ?? ?<!--spring對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測試包 --> ?? ?<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中 ,與 ${ } 的值相對應(yīng))
<span style="white-space:pre">?? ?</span>#在這里如果引入的mysql jar包為6.0版本 , 驅(qū)動值為 : ?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)建一個測試類: 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); ?? ??? ?} ?? ? ?? ?}
祝大家配置成功!!!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot創(chuàng)建多模塊項目的全過程記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot創(chuàng)建多模塊項目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01SpringBoot3整合mybatis-plus的實現(xiàn)
MyBatis-Plus是一個MyBatis的增強工具,在MyBatis的基礎(chǔ)上只做增強不做改變,本文主要介紹了Mybatis-Plus3.x的具體使用,具有一定的參考價值,感興趣的可以了解一下2023-10-10解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?jav
這篇文章主要介紹了解決IDEA啟動springboot項目報錯java.lang.ClassNotFoundException:?javax.servlet.ServletContext問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤
本篇文章主要介紹了淺談Spring-cloud 之 sleuth 服務(wù)鏈路跟蹤,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01SWT(JFace)體驗之ApplicationWindow
SWT(JFace)體驗之ApplicationWindow2009-06-06Spring Boot 整合持久層之JdbcTemplate
持久層是 Java EE 中訪問數(shù)據(jù)庫的核心操作,Spring Boot 中對常見的持久層框架都提供了自動化配置,例如 JdbcTemplate 、 JPA 等,Mybatis 的自動化配置則是 Mybatis 官方提供的2022-08-08