Spring boot整合使用JDBC連接配接MySQL資料庫
-
- Spring boot整合JDBC連接配接MySQL資料庫
-
- 說明
- JDBC是什麼
- 項目搭建
-
- 引入相關依賴
- 添加資料庫配置檔案和其他參數
- 建立測試類
- 執行結果
-
- 接口請求
- 列印結果
- 相關資料
-
- 資料庫表資訊
Spring boot整合JDBC連接配接MySQL資料庫
說明
庚子之初,新冠橫行。縱使集華夏之力,三月始消。
工作三年有餘,是以疫情,方覺唯有學習知識,日益進溢,才能在未來不确定的生活中,立于不敗之地。
因為最近打算更深入的學習一下MyBatis架構,苦于探索合适的方法而遲遲不進,在此之際,
突然想到大學時高數老師說的一句話:如果這道題你不會做,你就抄例題,如果抄完例題還不會,就繼續抄例題,直到抄會為止。後來,我抄了四年的例題,順利畢業。
扯遠了哈,回歸正題。既然要學習MyBatis架構,那麼原生的JDBC肯定是無法繞過的一個檻,因為MyBatis架構是對JDBC的封裝,換句話說就是MyBatis架構是根據JDBC實作的。
對此,以此案例,再次熟悉一下JDBC的内容。本案例已上傳至github
JDBC是什麼
維基百科中對JDBC的解釋是:Java 資料庫連接配接,(Java Database Connectivity,簡稱JDBC)是Java語言中用來規範用戶端程式如何來通路資料庫的應用程式接口,提供了諸如查詢和更新資料庫中資料的方法。
換句我們能聽懂的話是,JDBC是Java語言用來連接配接資料庫的橋梁,是一套規範用來提供對MySQL、Oracle、Sql Server等關系型資料庫的連接配接。JDBC并不執行SQL語句,但是JDBC提供了執行SQL語句的接口。
項目搭建
引入相關依賴
上次使用JDBC還是Java初學階段,這次使用Spring boot整合JDBC。使用Spring boot簡化了jar包的引入流程,其他方面并無差別。
建立Spring boot項目的過程這次不做說明,主要說一下比較關鍵的地方。使用JDBC連接配接資料庫,首先要引入相關依賴,我使用MySQL作為測試資料庫,
是以引入的依賴是:
<!--JDBC連接配接mysql資料庫-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
在此說明一下其他資料庫的連接配接依賴,對自己的知識點也算一種擴充
<!--JDBC連接配接Oracle資料庫-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<!--JDBC連接配接SQL Server資料庫-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
這裡貼出現在的pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.watimer.study</groupId>
<artifactId>jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jdbc</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--JDBC-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加資料庫配置檔案和其他參數
在resource檔案夾下application.properties檔案中添加相關配置參數:
# 啟動端口号
server.port=9696
建立測試類
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Controller
@RequestMapping("/jdbc")
public class TestController {
@RequestMapping("/user")
@ResponseBody
public List<Map> list() throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<Map> list = new ArrayList<>();
try {
// 1、擷取Connection
connection = DriverManager.getConnection("jdbc:mysql://192.168.1.15:3306/ivt-city?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull","root","Aa123456");
// 2、擷取Statement
statement = connection.createStatement();
// 3、準備SQL
String sql = "SELECT * FROM jj_lavendy_user_info";
// 4、執行查詢,得到ResultSet
resultSet = statement.executeQuery(sql);
// 5、處理ResultSet
Map map = new ConcurrentHashMap();
while (resultSet.next()){
map.put("id",resultSet.getString("userId"));
map.put("lavendy_user_name",resultSet.getString("lavendy_user_name"));
list.add(map);
}
} catch (SQLException e) {
// 6、關閉資料庫資源
connection.close();
e.printStackTrace();
} finally {
// 6、關閉資料庫資源
connection.close();
}
for (Map map:list){
System.out.println(map.toString());
}
return list;
}
}