Springboot(八)——配置JDBC資料來源
springData簡介
對於資料訪問層,無論是 SQL(關係型資料庫) 還是 NOSQL(非關係型資料庫),Spring Boot 底層都是採用 Spring Data 的方式進行統一處理。
Spring Boot 底層都是採用 Spring Data 的方式進行統一處理各種資料庫,Spring Data 也是 Spring 中與 Spring Boot、Spring Cloud 等齊名的知名專案。
Sping Data 官網:https://spring.io/projects/spring-data
資料庫相關的啟動器 :可以參考官方文件:
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
JDBC資料來源
一、新建springboot專案,引入相關模組
二、專案建好之後,發現自動幫我們匯入瞭如下的啟動器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
三、新建application.yaml配置檔案連線資料庫
spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解決時區的報錯
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
四、編寫測試類進行測試
@SpringBootTest
class Springboot04DataApplicationTests {
//注入資料來源
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
//檢視資料來源
System.out.println(dataSource.getClass());
//獲得資料庫連線
Connection connection = dataSource.getConnection();
System.out.println(connection);
//關閉資料來源連線
connection.close();
}
}
JDBCTemplate
1、有了資料來源(com.zaxxer.hikari.HikariDataSource),然後可以拿到資料庫連線(java.sql.Connection),有了連線,就可以使用原生的 JDBC 語句來操作資料庫;
2、即使不使用第三方第資料庫操作框架,如 MyBatis等,Spring 本身也對原生的JDBC 做了輕量級的封裝,即JdbcTemplate。
3、資料庫操作的所有 CRUD 方法都在 JdbcTemplate 中。
4、Spring Boot 不僅提供了預設的資料來源,同時預設已經配置好了 JdbcTemplate 放在了容器中,程式設計師只需自己注入即可使用
5、JdbcTemplate 的自動配置是依賴 org.springframework.boot.autoconfigure.jdbc 包下的 JdbcTemplateConfiguration 類
JdbcTemplate主要提供以下幾類方法:
-
execute方法:可以用於執行任何SQL語句,一般用於執行DDL語句;
-
update方法及batchUpdate方法:update方法用於執行新增、修改、刪除等語句;batchUpdate方法用於執行批處理相關語句;
-
query方法及queryForXXX方法:用於執行查詢相關語句;
-
call方法:用於執行儲存過程、函式相關語句。
CURD的基本操作
編寫一個Controller,注入 jdbcTemplate,編寫測試方法進行訪問測試;
package com.study.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
//查詢資料庫的所有資訊
//沒有實體類,資料庫中的東西怎麼獲得? map
@GetMapping("/userList")
public List<Map<String, Object>> userList(){
String sql = "select * from mybatis.user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
//新增使用者
@GetMapping("/addUser")
public String addUser(){
String sql = "insert into mybatis.user(id,name,pwd) values (5,'小明','123456')";
jdbcTemplate.update(sql);
return "addUser-ok";
}
//根據id修改使用者
@GetMapping("/updateUser/{id}")
public String updateUser(@PathVariable("id") int id){
String sql = "update mybatis.user set name=?,pwd=? where id="+id;
//封裝資料
Object[] objects = new Object[2];
objects[0] = "小明2";
objects[1] = "456789";
jdbcTemplate.update(sql,objects);
return "updateUse-ok";
}
//根據id刪除使用者
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id){
String sql = "delete from mybatis.user where id=?";
jdbcTemplate.update(sql,id);
return "deleteUse-ok";
}
}
測試請求,結果正常