SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法
前提
這篇文章是《SpringBoot2.x入門》專輯的第7篇文章,使用的SpringBoot
版本為2.3.1.RELEASE
,JDK
版本為1.8
。
這篇文章會簡單介紹jdbc
模塊也就是spring-boot-starter-jdbc
組件的引入、數(shù)據(jù)源的配置以及JdbcTemplate
的簡單使用。為了讓文中的例子相對通用,下文選用MySQL8.x
、h2database
(內(nèi)存數(shù)據(jù)庫)作為示例數(shù)據(jù)庫,選用主流的Druid
和HikariCP
作為示例數(shù)據(jù)源。
引入jdbc模塊
引入spring-boot-starter-jdbc
組件,如果在父POM
全局管理spring-boot
依賴版本的前提下,只需要在項目pom
文件的dependencies
元素直接引入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
通過IDEA
展開該依賴的關(guān)系圖如下:
其實(shí)spring-boot-starter-jdbc
模塊本身已經(jīng)引入了spring-jdbc
(間接引入spring-core
、spring-beans
、spring-tx
)、spring-boot-starter
和HikariCP
三個依賴,如果希望啟動Servlet
容器,可以額外引入spring-boot-starter-jdbc
。
spring-boot-starter-jdbc
提供了數(shù)據(jù)源配置、事務(wù)管理、數(shù)據(jù)訪問等等功能,而對于不同類型的數(shù)據(jù)庫,需要提供不同的驅(qū)動實(shí)現(xiàn),才能更加簡單地通過驅(qū)動實(shí)現(xiàn)根據(jù)連接URL
、用戶口令等屬性直接連接數(shù)據(jù)庫(或者說獲取數(shù)據(jù)庫的連接),因此對于不同類型的數(shù)據(jù)庫,需要引入不同的驅(qū)動包依賴。對于MySQL
而言,需要引入mysql-connector-java
,而對于h2database
而言,需要引入h2
(驅(qū)動包和數(shù)據(jù)庫代碼位于同一個依賴中),兩者中都具備數(shù)據(jù)庫抽象驅(qū)動接口java.sql.Driver
的實(shí)現(xiàn)類:
- 對于
mysql-connector-java
而言,常用的實(shí)現(xiàn)是com.mysql.cj.jdbc.Driver
(MySQL8.x
版本)。 - 對于
h2
而言,常用的實(shí)現(xiàn)是org.h2.Driver
。
如果需要連接的數(shù)據(jù)庫是h2database
,引入h2
對應(yīng)的數(shù)據(jù)庫和驅(qū)動依賴如下:
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency>
如果需要連接的數(shù)據(jù)庫是MySQL
,引入MySQL
對應(yīng)的驅(qū)動依賴如下:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
上面的類庫版本選取了編寫本文時候的最新版本,實(shí)際上要根據(jù)軟件對應(yīng)的版本選擇合適的驅(qū)動版本。
數(shù)據(jù)源配置
spring-boot-starter-jdbc
模塊默認(rèn)使用HikariCP
作為數(shù)據(jù)庫的連接池。
HikariCP,也就是Hikari Connection Pool,Hikari連接池。HikariCP的作者是日本人,而Hikari是日語,意義和light相近,也就是"光"。Simplicity is prerequisite for reliability(簡單是可靠的先決條件)是HikariCP的設(shè)計理念,他是一款代碼精悍的高性能連接池框架,被Spring項目選中作為內(nèi)建默認(rèn)連接池,值得信賴。
如果決定使用HikariCP
連接h2
數(shù)據(jù)庫,則配置文件中添加如下的配置項以配置數(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ù)庫的WebUI控制臺 spring.h2.console.enabled=true # 可選配置,訪問h2數(shù)據(jù)庫的WebUI控制臺的路徑 spring.h2.console.path=/h2-console # 可選配置,是否允許非本機(jī)訪問h2數(shù)據(jù)庫的WebUI控制臺 spring.h2.console.settings.web-allow-others=true
如果決定使用HikariCP
連接MySQL
數(shù)據(jù)庫,則配置文件中添加如下的配置項以配置數(shù)據(jù)源HikariDataSource
:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 注意MySQL8.x需要指定服務(wù)時區(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
有時候可能更偏好于使用其他連接池,例如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ù)庫,則配置文件中添加如下的配置項以配置數(shù)據(jù)源DruidDataSource
:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 注意MySQL8.x需要指定服務(wù)時區(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ù)源的屬性值都會選用默認(rèn)值,如果想深度定制數(shù)據(jù)源的屬性,則需要覆蓋由DataSourceConfiguration.Generic
創(chuàng)建的數(shù)據(jù)源,先預(yù)設(shè)所有需要的配置,為了和內(nèi)建的spring.datasource
屬性前綴避嫌,這里自定義一個屬性前綴druid
,配置文件中添加自定義配置項如下:
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 # 配置獲取連接等待超時的時間 druid.maxWait=60000 # 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 druid.timeBetweenEvictionRunsMillis=60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 druid.minEvictableIdleTimeMillis=60000 druid.validationQuery=SELECT 1 FROM DUAL druid.testWhileIdle=true druid.testOnBorrow=false druid.testOnReturn=false # 打開PSCache,并且指定每個連接上PSCache的大小 druid.poolPreparedStatements=true druid.maxPoolPreparedStatementPerConnectionSize=20 # 配置監(jiān)控統(tǒng)計攔截的filters,后臺統(tǒng)計相關(guān) druid.filters=stat,wall # 打開mergeSql功能;慢SQL記錄 druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
這里要確保本地安裝了一個8.x版本的MySQL服務(wù),并且建立了一個命名為local的數(shù)據(jù)庫。
需要在項目中添加一個數(shù)據(jù)源自動配置類,這里命名為DruidAutoConfiguration
,通過注解@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"); // 添加控制臺管理用戶 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ī)則 filterRegistrationBean.addUrlPatterns("/*"); // 忽略過濾格式 filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"); return filterRegistrationBean; } }
可以通過訪問${requestContext}/druid/login.html
跳轉(zhuǎn)到Druid
的監(jiān)控控制臺,登錄賬號密碼就是在statViewServlet
中配置的用戶和密碼:
Druid是一款爭議比較多的數(shù)據(jù)源框架,項目的Issue中也有人提出過框架中加入太多和連接池?zé)o關(guān)的功能,例如SQL監(jiān)控、屬性展示等等,這些功能本該讓專業(yè)的監(jiān)控軟件完成。但毫無疑問,這是一款活躍度比較高的優(yōu)秀國產(chǎn)開源框架。
配置schema和data腳本
spring-boot-starter-jdbc
可以通過一些配置然后委托DataSourceInitializerInvoker
進(jìn)行schema
(一般理解為DDL
)和data
(一般理解為DML
)腳本的加載和執(zhí)行,具體的配置項是:
# 定義schema的加載路徑,可以通過英文逗號指定多個路徑 spring.datasource.schema=classpath:/ddl/schema.sql # 定義data的加載路徑,可以通過英文逗號指定多個路徑 spring.datasource.data=classpath:/dml/data.sql # 可選 # spring.datasource.schema-username= # spring.datasource.schema-password= # 項目數(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)建時間', edit_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間' ) COMMENT '客戶表';
由于spring.datasource.initialization-mode
指定為ALWAYS
,每次數(shù)據(jù)源初始化都會執(zhí)行spring.datasource.schema
中配置的腳本,會刪表重建。接著類路徑的resources
文件夾下添加dml/data.sql
:
INSERT INTO customer(customer_name) VALUES ('throwable');
添加一個CommandLineRunner
實(shí)現(xià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(); } }
啟動后執(zhí)行結(jié)果如下:
這里務(wù)必注意一點(diǎn),spring.datasource.schema指定的腳本執(zhí)行成功之后才會執(zhí)行spring.datasource.data指定的腳本,如果想僅僅執(zhí)行spring.datasource.data指定的腳本,那么需要至少把spring.datasource.schema指向一個空的文件,確保spring.datasource.schema指定路徑的文件初始化成功。
使用JdbcTemplate
spring-boot-starter-jdbc
中自帶的JdbcTemplate
是對JDBC
的輕度封裝。這里只簡單介紹一下它的使用方式,構(gòu)建一個面向前面提到的customer
表的具備CURD
功能的DAO
。這里先在前文提到的DruidAutoConfiguration
中添加一個JdbcTemplate
實(shí)例到IOC
容器中:
@Bean public JdbcTemplate jdbcTemplate(DataSource dataSource){ return new JdbcTemplate(dataSource); }
添加一個Customer
實(shí)體類:
// 實(shí)體類 @Data public class Customer { private Long id; private String customerName; private LocalDateTime createTime; private LocalDateTime editTime; }
接著添加一個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; }
測試結(jié)果如下:
JdbcTemplate
的優(yōu)勢是可以應(yīng)用函數(shù)式接口簡化一些值設(shè)置和值提取的操作,并且獲得接近于原生JDBC
的執(zhí)行效率,但是它的明顯劣勢就是會產(chǎn)生大量模板化的代碼,在一定程度上影響開發(fā)效率。
小結(jié)
本文簡單分析spring-boot-starter-jdbc
引入,以及不同數(shù)據(jù)庫和不同數(shù)據(jù)源的使用方式,最后簡單介紹了JdbcTemplate
的基本使用。
demo
項目倉庫:
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入門教程:引入jdbc模塊與JdbcTemplate簡單使用的文章就介紹到這了,更多相關(guān)SpringBoot2.x入門教程:引入jdbc模塊與JdbcTemplate簡單使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
- SpringBoot2使用JTA組件實(shí)現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測好用)
- SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法
- SpringBoot jdbctemplate使用方法解析
- springBoot使用JdbcTemplate代碼實(shí)例
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
- springboot使用JdbcTemplate完成對數(shù)據(jù)庫的增刪改查功能
- 如何在SpringBoot項目中使用Oracle11g數(shù)據(jù)庫
- SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
相關(guān)文章
SpringBatch從入門到精通之StepScope作用域和用法詳解
這篇文章主要介紹了SpringBatch從入門到精通之StepScope作用域和用法詳解,主要包括IOC容器中幾種bean的作用范圍以及可能遇到的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot?MongoCustomConversions自定義轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08解決springboot application.properties server.port配置問題
這篇文章主要介紹了解決springboot application.properties server.port配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java并發(fā)多線程編程之CountDownLatch的用法
這篇文章主要介紹了Java并發(fā)多線程編程之CountDownLatch的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06