Spring Boot JDBC 連線資料庫示例

2022-09-28 05:57:10 字數 4156 閱讀 5807

文字將對在spring boot構建的web應用中,基於mysql資料庫的幾種資料庫連線方式進行介紹。

包括jdbc、jpa、mybatis、多資料來源和事務。

jdbc 連線資料庫

1、屬性配置檔案(application.properties)

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.driver

如果使用jndi,則可以替代 spring.datasource 的 url、username、password,如:

spring.datasource.jndi-name=j**a:tomcat/datasources/example

值得一提的是,無論是spring boot預設的datasource配置還是你自己的datasource bean,都會引用到外部屬性檔案中的屬性配置。所以假設你自定義的datasource bean,你可以在定義bean時設定屬性,也可以在屬性檔案中,以「spring.datasource.*」的方式使屬性配置外部化。

2、pom.xml 配置m**en依賴

mysql

mysql-connector-j**a

www.cppcns.com;org.springframework.boot&程式設計客棧lt;/groupid>

spring-boot-starter-jdbc

3、j**a**範例

studentservice.j**a

package org.springboot.sample.service;

import j**a.sql.resultset;

import j**a.sql.sqlexception;

import j**a.util.list;

import org.springboot.sample.entity.student;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.jdbc.core.jdbctemplate;

import org.springframework.jdbc.core.rowmapper;

import org.springframework.stereotype.service;

/** * studeng service

* * @author 單紅宇(365384722)

* @create 2023年1月12日

*/@service

public class studentservice

});}}

student.j**a 實體類

package org.springboot.sample.entity;

import j**a.io.serializable;

/** * 學生實體

* * @author 單紅宇(365384722)

* @create 2023年1月12日

*/public class student implements serializable

studentcontroller.j**a

package org.springboot.sample.controller;

import j**a.util.list;

import org.slf4j.logger;

import org.slf4j.logge***ctory;

import org.springboot.sample.entity.student;

import org.springboot.sample.service.studentservice;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.restcontroller;

@restcontroller

@requestmapping("/stu")

public class studentcontroller

}本文對工程新增檔案後工程結構圖:

然後啟動專案,訪問位址: http://localhost:8080/myspringboot/stu/list 響應結果如下:

[,,,,

,]連線池說明

tomcat7之前,tomcat本質應用了dbcp連線池技術來實現的jdbc資料來源,但在tomcat7之後,tomcat提供了新的jdbc連線池方案,作為dbcp的替換或備選方案,解決了許多之前使用dbcp的不利之處,並提高了效能。

spring boot為我們準備了最佳的資料庫連線池方案,只需要在屬性檔案(例如application.properties)中配置需要的連線池引數即可。

我們使用tomcat資料來源連線池,需要依賴tomcat-jdbc,只要應用中新增了spring-boot-starter-jdbc 或 spring-boot-starter-data-jpa依賴,則無需擔心這點,因為將會自動新增 tomcat-jdbc 依賴。

假如我們想用其他方式的連線池技術,只要配置自己的datasource bean,即可覆蓋spring boot的自動配置。

請看我的資料來源配置:

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.driver-class-name=com.mysql.jdbc.driver

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

spring.datasource.validation-query=select 1

spring.datasource.test-on-borrow=false

spring.datasource.test-while-idle=true

spring.datasource.time-between-eviction-runs-millis=18800

spring.datasource.jdbc-interceptors=connectionstate;slowqueryreport(threshold=0)

配置過連線池的開發人員對這些屬性的意義都有所認識。

我們開啟debug日誌輸出,logback.xml 中新增:

然後啟動專案,注意觀察日誌輸出,如下圖中會顯示自動啟用了連線池:

我在上面的資料來源配置中新增了過濾器,並設定了延遲時間為0(故意設定很低,實際專案中請修改):

複製** **如下:

spring.datasource.jdbc-interceptors=connectionstate;slowqueryreport(threshold=0)

這個時候,我們訪問 http://localhost:8080/myspringboot/stu/list 觀察日誌,會發現框架自動將大於該時間的資料查詢進行警告輸出,如下:

複製** **如下:

2016-01-12 23:27:06.710  warn 17644 --- [nio-8080-exec-1] o.a.t.j.p.interceptor.slowqueryreport    : slow query report sql=select id,name,score_sum,score_**g, age   from student; time=3 ms;

本文標題: spring boot jdbc 連線資料庫示例

本文位址:

mysql連庫 mysql遠端連庫

mysql連線遠端庫 伺服器端 1 登陸伺服器端,進入命令列,windows cmd 2 設定使用者 密碼讓指定的ip訪問 mysql u root p 或安裝的快捷方式進入 mysql command line client,使用grant命令 grant 許可權1,許可權2,許可權n on 資料...

按鍵連按和不連按

按鍵處理函式 返回按鍵值 mode 0,不支援連續按 1,支援連續按 0,沒有任何按鍵按下 1,wkup按下 wk up 注意此函式有響應優先順序,key0 key1 key2 wk up u8 key scan u8 mode else if key0 1 key1 1 key2 1 wk up ...

我今天表演加班,一連,二連,三連

是為dos提供的有力的除錯,跟蹤程式執行,檢查系統資料的工具程式,它是在字元介面下以單字元命令方式工作。要很好地使用它必須具備一定的彙編程式設計和硬體基本知識的能力,當然,它為組合語言程式設計師提供了有效的除錯手段,它的功能包括以下幾個方面。1.直接輸入 更改 跟蹤 執行匯程式設計序 2.觀察作業系...