SpringBoot2.x入門(mén)教程之引入jdbc模塊與JdbcTemplate簡(jiǎn)單使用方法
前提
這篇文章是《SpringBoot2.x入門(mén)》專輯的第7篇文章,使用的SpringBoot
版本為2.3.1.RELEASE
,JDK
版本為1.8
。
這篇文章會(huì)簡(jiǎn)單介紹jdbc
模塊也就是spring-boot-starter-jdbc
組件的引入、數(shù)據(jù)源的配置以及JdbcTemplate
的簡(jiǎn)單使用。為了讓文中的例子相對(duì)通用,下文選用MySQL8.x
、h2database
(內(nèi)存數(shù)據(jù)庫(kù))作為示例數(shù)據(jù)庫(kù),選用主流的Druid
和HikariCP
作為示例數(shù)據(jù)源。
引入jdbc模塊
引入spring-boot-starter-jdbc
組件,如果在父POM
全局管理spring-boot
依賴版本的前提下,只需要在項(xiàng)目pom
文件的dependencies
元素直接引入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
通過(guò)IDEA
展開(kāi)該依賴的關(guān)系圖如下:
其實(shí)spring-boot-starter-jdbc
模塊本身已經(jīng)引入了spring-jdbc
(間接引入spring-core
、spring-beans
、spring-tx
)、spring-boot-starter
和HikariCP
三個(gè)依賴,如果希望啟動(dòng)Servlet
容器,可以額外引入spring-boot-starter-jdbc
。
spring-boot-starter-jdbc
提供了數(shù)據(jù)源配置、事務(wù)管理、數(shù)據(jù)訪問(wèn)等等功能,而對(duì)于不同類型的數(shù)據(jù)庫(kù),需要提供不同的驅(qū)動(dòng)實(shí)現(xiàn),才能更加簡(jiǎn)單地通過(guò)驅(qū)動(dòng)實(shí)現(xiàn)根據(jù)連接URL
、用戶口令等屬性直接連接數(shù)據(jù)庫(kù)(或者說(shuō)獲取數(shù)據(jù)庫(kù)的連接),因此對(duì)于不同類型的數(shù)據(jù)庫(kù),需要引入不同的驅(qū)動(dòng)包依賴。對(duì)于MySQL
而言,需要引入mysql-connector-java
,而對(duì)于h2database
而言,需要引入h2
(驅(qū)動(dòng)包和數(shù)據(jù)庫(kù)代碼位于同一個(gè)依賴中),兩者中都具備數(shù)據(jù)庫(kù)抽象驅(qū)動(dòng)接口java.sql.Driver
的實(shí)現(xiàn)類:
- 對(duì)于
mysql-connector-java
而言,常用的實(shí)現(xiàn)是com.mysql.cj.jdbc.Driver
(MySQL8.x
版本)。 - 對(duì)于
h2
而言,常用的實(shí)現(xiàn)是org.h2.Driver
。
如果需要連接的數(shù)據(jù)庫(kù)是h2database
,引入h2
對(duì)應(yīng)的數(shù)據(jù)庫(kù)和驅(qū)動(dòng)依賴如下:
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency>
如果需要連接的數(shù)據(jù)庫(kù)是MySQL
,引入MySQL
對(duì)應(yīng)的驅(qū)動(dòng)依賴如下:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
上面的類庫(kù)版本選取了編寫(xiě)本文時(shí)候的最新版本,實(shí)際上要根據(jù)軟件對(duì)應(yīng)的版本選擇合適的驅(qū)動(dòng)版本。
數(shù)據(jù)源配置
spring-boot-starter-jdbc
模塊默認(rèn)使用HikariCP
作為數(shù)據(jù)庫(kù)的連接池。
HikariCP,也就是Hikari Connection Pool,Hikari連接池。HikariCP的作者是日本人,而Hikari是日語(yǔ),意義和light相近,也就是"光"。Simplicity is prerequisite for reliability(簡(jiǎn)單是可靠的先決條件)是HikariCP的設(shè)計(jì)理念,他是一款代碼精悍的高性能連接池框架,被Spring項(xiàng)目選中作為內(nèi)建默認(rèn)連接池,值得信賴。
如果決定使用HikariCP
連接h2
數(shù)據(jù)庫(kù),則配置文件中添加如下的配置項(xiàng)以配置數(shù)據(jù)源HikariDataSource
:
spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:test spring.datasource.username=root spring.datasource.password=123456 # 可選配置,是否啟用h2數(shù)據(jù)庫(kù)的WebUI控制臺(tái) spring.h2.console.enabled=true # 可選配置,訪問(wèn)h2數(shù)據(jù)庫(kù)的WebUI控制臺(tái)的路徑 spring.h2.console.path=/h2-console # 可選配置,是否允許非本機(jī)訪問(wèn)h2數(shù)據(jù)庫(kù)的WebUI控制臺(tái) spring.h2.console.settings.web-allow-others=true
如果決定使用HikariCP
連接MySQL
數(shù)據(jù)庫(kù),則配置文件中添加如下的配置項(xiàng)以配置數(shù)據(jù)源HikariDataSource
:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 注意MySQL8.x需要指定服務(wù)時(shí)區(qū)屬性 spring.datasource.url=jdbc:mysql://localhost:3306/local?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false spring.datasource.username=root spring.datasource.password=root
有時(shí)候可能更偏好于使用其他連接池,例如Alibaba
出品的Durid
,這樣就要禁用默認(rèn)的數(shù)據(jù)源加載,改成Durid
提供的數(shù)據(jù)源。引入Druid
數(shù)據(jù)源需要額外添加依賴:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency>
如果決定使用Druid
連接MySQL
數(shù)據(jù)庫(kù),則配置文件中添加如下的配置項(xiàng)以配置數(shù)據(jù)源DruidDataSource
:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 注意MySQL8.x需要指定服務(wù)時(shí)區(qū)屬性 spring.datasource.url=jdbc:mysql://localhost:3306/local?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false spring.datasource.username=root spring.datasource.password=root # 指定數(shù)據(jù)源類型為Druid提供的數(shù)據(jù)源 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
上面這樣配置DruidDataSource
,所有數(shù)據(jù)源的屬性值都會(huì)選用默認(rèn)值,如果想深度定制數(shù)據(jù)源的屬性,則需要覆蓋由DataSourceConfiguration.Generic
創(chuàng)建的數(shù)據(jù)源,先預(yù)設(shè)所有需要的配置,為了和內(nèi)建的spring.datasource
屬性前綴避嫌,這里自定義一個(gè)屬性前綴druid
,配置文件中添加自定義配置項(xiàng)如下:
druid.url=jdbc:mysql://localhost:3306/local?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false druid.driver-class-name=com.mysql.cj.jdbc.Driver druid.username=root druid.password=root # 初始化大小 druid.initialSize=1 # 最大 druid.maxActive=20 # 空閑 druid.minIdle=5 # 配置獲取連接等待超時(shí)的時(shí)間 druid.maxWait=60000 # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 druid.timeBetweenEvictionRunsMillis=60000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 druid.minEvictableIdleTimeMillis=60000 druid.validationQuery=SELECT 1 FROM DUAL druid.testWhileIdle=true druid.testOnBorrow=false druid.testOnReturn=false # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 druid.poolPreparedStatements=true druid.maxPoolPreparedStatementPerConnectionSize=20 # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,后臺(tái)統(tǒng)計(jì)相關(guān) druid.filters=stat,wall # 打開(kāi)mergeSql功能;慢SQL記錄 druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
這里要確保本地安裝了一個(gè)8.x版本的MySQL服務(wù),并且建立了一個(gè)命名為local的數(shù)據(jù)庫(kù)。
需要在項(xiàng)目中添加一個(gè)數(shù)據(jù)源自動(dòng)配置類,這里命名為DruidAutoConfiguration
,通過(guò)注解@ConfigurationProperties
把druid
前綴的屬性注入到數(shù)據(jù)源實(shí)例中:
@Configuration public class DruidAutoConfiguration { @Bean @ConfigurationProperties(prefix = "druid") public DataSource dataSource() { return new DruidDataSource(); } @Bean public ServletRegistrationBean<StatViewServlet> statViewServlet() { ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); // 添加IP白名單 servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // 添加控制臺(tái)管理用戶 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "123456"); // 是否能夠重置數(shù)據(jù) servletRegistrationBean.addInitParameter("resetEnable", "true"); return servletRegistrationBean; } @Bean public FilterRegistrationBean<WebStatFilter> webStatFilter() { WebStatFilter webStatFilter = new WebStatFilter(); FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(webStatFilter); // 添加過(guò)濾規(guī)則 filterRegistrationBean.addUrlPatterns("/*"); // 忽略過(guò)濾格式 filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"); return filterRegistrationBean; } }
可以通過(guò)訪問(wèn)${requestContext}/druid/login.html
跳轉(zhuǎn)到Druid
的監(jiān)控控制臺(tái),登錄賬號(hào)密碼就是在statViewServlet
中配置的用戶和密碼:
Druid是一款爭(zhēng)議比較多的數(shù)據(jù)源框架,項(xiàng)目的Issue中也有人提出過(guò)框架中加入太多和連接池?zé)o關(guān)的功能,例如SQL監(jiān)控、屬性展示等等,這些功能本該讓專業(yè)的監(jiān)控軟件完成。但毫無(wú)疑問(wèn),這是一款活躍度比較高的優(yōu)秀國(guó)產(chǎn)開(kāi)源框架。
配置schema和data腳本
spring-boot-starter-jdbc
可以通過(guò)一些配置然后委托DataSourceInitializerInvoker
進(jìn)行schema
(一般理解為DDL
)和data
(一般理解為DML
)腳本的加載和執(zhí)行,具體的配置項(xiàng)是:
# 定義schema的加載路徑,可以通過(guò)英文逗號(hào)指定多個(gè)路徑 spring.datasource.schema=classpath:/ddl/schema.sql # 定義data的加載路徑,可以通過(guò)英文逗號(hào)指定多個(gè)路徑 spring.datasource.data=classpath:/dml/data.sql # 可選 # spring.datasource.schema-username= # spring.datasource.schema-password= # 項(xiàng)目數(shù)據(jù)源初始化之后的執(zhí)行模式,可選值EMBEDDED、ALWAYS和NEVER spring.datasource.initialization-mode=always
類路徑的resources
文件夾下添加ddl/schema.sql
:
DROP TABLE IF EXISTS customer; CREATE TABLE customer ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY COMMENT '主鍵', customer_name VARCHAR(32) NOT NULL COMMENT '客戶名稱', create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', edit_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時(shí)間' ) COMMENT '客戶表';
由于spring.datasource.initialization-mode
指定為ALWAYS
,每次數(shù)據(jù)源初始化都會(huì)執(zhí)行spring.datasource.schema
中配置的腳本,會(huì)刪表重建。接著類路徑的resources
文件夾下添加dml/data.sql
:
INSERT INTO customer(customer_name) VALUES ('throwable');
添加一個(gè)CommandLineRunner
實(shí)現(xiàn)驗(yàn)證一下:
@Slf4j @SpringBootApplication public class Ch7Application implements CommandLineRunner { @Autowired private DataSource dataSource; public static void main(String[] args) { SpringApplication.run(Ch7Application.class, args); } @Override public void run(String... args) throws Exception { Connection connection = dataSource.getConnection(); ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM customer WHERE id = 1"); while (resultSet.next()) { log.info("id:{},name:{}", resultSet.getLong("id"), resultSet.getString("customer_name")); } resultSet.close(); connection.close(); } }
啟動(dòng)后執(zhí)行結(jié)果如下:
這里務(wù)必注意一點(diǎn),spring.datasource.schema指定的腳本執(zhí)行成功之后才會(huì)執(zhí)行spring.datasource.data指定的腳本,如果想僅僅執(zhí)行spring.datasource.data指定的腳本,那么需要至少把spring.datasource.schema指向一個(gè)空的文件,確保spring.datasource.schema指定路徑的文件初始化成功。
使用JdbcTemplate
spring-boot-starter-jdbc
中自帶的JdbcTemplate
是對(duì)JDBC
的輕度封裝。這里只簡(jiǎn)單介紹一下它的使用方式,構(gòu)建一個(gè)面向前面提到的customer
表的具備CURD
功能的DAO
。這里先在前文提到的DruidAutoConfiguration
中添加一個(gè)JdbcTemplate
實(shí)例到IOC
容器中:
@Bean public JdbcTemplate jdbcTemplate(DataSource dataSource){ return new JdbcTemplate(dataSource); }
添加一個(gè)Customer
實(shí)體類:
// 實(shí)體類 @Data public class Customer { private Long id; private String customerName; private LocalDateTime createTime; private LocalDateTime editTime; }
接著添加一個(gè)CustoemrDao
類,實(shí)現(xiàn)增刪改查:
// CustoemrDao @RequiredArgsConstructor @Repository public class CustomerDao { private final JdbcTemplate jdbcTemplate; /** * 增 */ public int insertSelective(Customer customer) { StringJoiner p = new StringJoiner(",", "(", ")"); StringJoiner v = new StringJoiner(",", "(", ")"); Optional.ofNullable(customer.getCustomerName()).ifPresent(x -> { p.add("customer_name"); v.add("?"); }); Optional.ofNullable(customer.getCreateTime()).ifPresent(x -> { p.add("create_time"); v.add("?"); }); Optional.ofNullable(customer.getEditTime()).ifPresent(x -> { p.add("edit_time"); v.add("?"); }); String sql = "INSERT INTO customer" + p.toString() + " VALUES " + v.toString(); KeyHolder keyHolder = new GeneratedKeyHolder(); int updateCount = jdbcTemplate.update(con -> { PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); int index = 1; if (null != customer.getCustomerName()) { ps.setString(index++, customer.getCustomerName()); } if (null != customer.getCreateTime()) { ps.setTimestamp(index++, Timestamp.valueOf(customer.getCreateTime())); } if (null != customer.getEditTime()) { ps.setTimestamp(index, Timestamp.valueOf(customer.getEditTime())); } return ps; }, keyHolder); customer.setId(Objects.requireNonNull(keyHolder.getKey()).longValue()); return updateCount; } /** * 刪 */ public int delete(long id) { return jdbcTemplate.update("DELETE FROM customer WHERE id = ?", id); } /** * 查 */ public Customer queryByCustomerName(String customerName) { return jdbcTemplate.query("SELECT * FROM customer WHERE customer_name = ?", ps -> ps.setString(1, customerName), SINGLE); } public List<Customer> queryAll() { return jdbcTemplate.query("SELECT * FROM customer", MULTI); } public int updateByPrimaryKeySelective(Customer customer) { final long id = Objects.requireNonNull(Objects.requireNonNull(customer).getId()); StringBuilder sql = new StringBuilder("UPDATE customer SET "); Optional.ofNullable(customer.getCustomerName()).ifPresent(x -> sql.append("customer_name = ?,")); Optional.ofNullable(customer.getCreateTime()).ifPresent(x -> sql.append("create_time = ?,")); Optional.ofNullable(customer.getEditTime()).ifPresent(x -> sql.append("edit_time = ?,")); StringBuilder q = new StringBuilder(sql.substring(0, sql.lastIndexOf(","))).append(" WHERE id = ?"); return jdbcTemplate.update(q.toString(), ps -> { int index = 1; if (null != customer.getCustomerName()) { ps.setString(index++, customer.getCustomerName()); } if (null != customer.getCreateTime()) { ps.setTimestamp(index++, Timestamp.valueOf(customer.getCreateTime())); } if (null != customer.getEditTime()) { ps.setTimestamp(index++, Timestamp.valueOf(customer.getEditTime())); } ps.setLong(index, id); }); } private static Customer convert(ResultSet rs) throws SQLException { Customer customer = new Customer(); customer.setId(rs.getLong("id")); customer.setCustomerName(rs.getString("customer_name")); customer.setCreateTime(rs.getTimestamp("create_time").toLocalDateTime()); customer.setEditTime(rs.getTimestamp("edit_time").toLocalDateTime()); return customer; } private static ResultSetExtractor<List<Customer>> MULTI = rs -> { List<Customer> result = new ArrayList<>(); while (rs.next()) { result.add(convert(rs)); } return result; }; private static ResultSetExtractor<Customer> SINGLE = rs -> rs.next() ? convert(rs) : null; }
測(cè)試結(jié)果如下:
JdbcTemplate
的優(yōu)勢(shì)是可以應(yīng)用函數(shù)式接口簡(jiǎn)化一些值設(shè)置和值提取的操作,并且獲得接近于原生JDBC
的執(zhí)行效率,但是它的明顯劣勢(shì)就是會(huì)產(chǎn)生大量模板化的代碼,在一定程度上影響開(kāi)發(fā)效率。
小結(jié)
本文簡(jiǎn)單分析spring-boot-starter-jdbc
引入,以及不同數(shù)據(jù)庫(kù)和不同數(shù)據(jù)源的使用方式,最后簡(jiǎn)單介紹了JdbcTemplate
的基本使用。
demo
項(xiàng)目倉(cāng)庫(kù):
Github
:https://github.com/zjcscut/spring-boot-guide/tree/master/ch6-jdbc-module-h2
Github
:https://github.com/zjcscut/spring-boot-guide/tree/master/ch7-jdbc-module-mysql
總結(jié)
到此這篇關(guān)于SpringBoot2.x入門(mén)教程:引入jdbc模塊與JdbcTemplate簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)SpringBoot2.x入門(mén)教程:引入jdbc模塊與JdbcTemplate簡(jiǎn)單使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
- SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測(cè)好用)
- SpringBoot使用JdbcTemplate訪問(wèn)操作數(shù)據(jù)庫(kù)基本用法
- SpringBoot jdbctemplate使用方法解析
- springBoot使用JdbcTemplate代碼實(shí)例
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫(kù)
- springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫(kù)的增刪改查功能
- 如何在SpringBoot項(xiàng)目中使用Oracle11g數(shù)據(jù)庫(kù)
- SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解
相關(guān)文章
SpringBatch從入門(mén)到精通之StepScope作用域和用法詳解
這篇文章主要介紹了SpringBatch從入門(mén)到精通之StepScope作用域和用法詳解,主要包括IOC容器中幾種bean的作用范圍以及可能遇到的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08idea項(xiàng)目的左側(cè)目錄沒(méi)了如何設(shè)置
這篇文章主要介紹了idea項(xiàng)目的左側(cè)目錄沒(méi)了如何設(shè)置的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02解決springboot application.properties server.port配置問(wèn)題
這篇文章主要介紹了解決springboot application.properties server.port配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java并發(fā)多線程編程之CountDownLatch的用法
這篇文章主要介紹了Java并發(fā)多線程編程之CountDownLatch的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06SpringBoot集成消息隊(duì)列的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot集成消息隊(duì)列的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02