SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法
前提
這篇文章是《SpringBoot2.x入門》專輯的第7篇文章,使用的SpringBoot版本為2.3.1.RELEASE,JDK版本為1.8。
這篇文章會(huì)簡單介紹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依賴版本的前提下,只需要在項(xiàng)目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三個(gè)依賴,如果希望啟動(dòng)Servlet容器,可以額外引入spring-boot-starter-jdbc。
spring-boot-starter-jdbc提供了數(shù)據(jù)源配置、事務(wù)管理、數(shù)據(jù)訪問等等功能,而對于不同類型的數(shù)據(jù)庫,需要提供不同的驅(qū)動(dòng)實(shí)現(xiàn),才能更加簡單地通過驅(qū)動(dòng)實(shí)現(xiàn)根據(jù)連接URL、用戶口令等屬性直接連接數(shù)據(jù)庫(或者說獲取數(shù)據(jù)庫的連接),因此對于不同類型的數(shù)據(jù)庫,需要引入不同的驅(qū)動(dòng)包依賴。對于MySQL而言,需要引入mysql-connector-java,而對于h2database而言,需要引入h2(驅(qū)動(dòng)包和數(shù)據(jù)庫代碼位于同一個(gè)依賴中),兩者中都具備數(shù)據(jù)庫抽象驅(qū)動(dòng)接口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ū)動(dòng)依賴如下:
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency>
如果需要連接的數(shù)據(jù)庫是MySQL,引入MySQL對應(yīng)的驅(qū)動(dòng)依賴如下:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
上面的類庫版本選取了編寫本文時(shí)候的最新版本,實(shí)際上要根據(jù)軟件對應(yīng)的版本選擇合適的驅(qū)動(dòng)版本。
數(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è)計(jì)理念,他是一款代碼精悍的高性能連接池框架,被Spring項(xiàng)目選中作為內(nèi)建默認(rèn)連接池,值得信賴。
如果決定使用HikariCP連接h2數(shù)據(jù)庫,則配置文件中添加如下的配置項(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ù)庫的WebUI控制臺(tái) spring.h2.console.enabled=true # 可選配置,訪問h2數(shù)據(jù)庫的WebUI控制臺(tái)的路徑 spring.h2.console.path=/h2-console # 可選配置,是否允許非本機(jī)訪問h2數(shù)據(jù)庫的WebUI控制臺(tái) spring.h2.console.settings.web-allow-others=true

如果決定使用HikariCP連接MySQL數(shù)據(jù)庫,則配置文件中添加如下的配置項(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ù)庫,則配置文件中添加如下的配置項(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)行一次檢測,檢測需要關(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 # 打開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 # 打開mergeSql功能;慢SQL記錄 druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
這里要確保本地安裝了一個(gè)8.x版本的MySQL服務(wù),并且建立了一個(gè)命名為local的數(shù)據(jù)庫。
需要在項(xiàng)目中添加一個(gè)數(shù)據(jù)源自動(dòng)配置類,這里命名為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");
// 添加控制臺(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ī)則
filterRegistrationBean.addUrlPatterns("/*");
// 忽略過濾格式
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
return filterRegistrationBean;
}
}
可以通過訪問${requestContext}/druid/login.html跳轉(zhuǎn)到Druid的監(jiān)控控制臺(tái),登錄賬號(hào)密碼就是在statViewServlet中配置的用戶和密碼:

Druid是一款爭議比較多的數(shù)據(jù)源框架,項(xiàng)目的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í)行,具體的配置項(xiàng)是:
# 定義schema的加載路徑,可以通過英文逗號(hào)指定多個(gè)路徑 spring.datasource.schema=classpath:/ddl/schema.sql # 定義data的加載路徑,可以通過英文逗號(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是對JDBC的輕度封裝。這里只簡單介紹一下它的使用方式,構(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;
}
測試結(jié)果如下:

JdbcTemplate的優(yōu)勢是可以應(yīng)用函數(shù)式接口簡化一些值設(shè)置和值提取的操作,并且獲得接近于原生JDBC的執(zhí)行效率,但是它的明顯劣勢就是會(huì)產(chǎn)生大量模板化的代碼,在一定程度上影響開發(fā)效率。
小結(jié)
本文簡單分析spring-boot-starter-jdbc引入,以及不同數(shù)據(jù)庫和不同數(shù)據(jù)源的使用方式,最后簡單介紹了JdbcTemplate的基本使用。
demo項(xiàng)目倉庫:
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項(xiàng)目中使用Oracle11g數(shù)據(jù)庫
- SpringBoot中使用JdbcTemplate訪問Oracle數(shù)據(jù)庫的案例詳解
相關(guān)文章
SpringCloud Gateway加載斷言predicates與過濾器filters的源碼分析
這篇文章主要介紹了SpringCloud Gateway加載斷言predicates與過濾器filters的詳細(xì)過程,本文通過源碼給大家解析的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
ArrayList和LinkedList區(qū)別及使用場景代碼解析
這篇文章主要介紹了ArrayList和LinkedList區(qū)別及使用場景代碼解析,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
關(guān)于MyBatis plus條件構(gòu)造器的逐條詳解
什么是條件構(gòu)造器呢?簡單來說,條件構(gòu)造器就是用來生成我們查數(shù)據(jù)庫的sql。它可以簡化sql代碼的編寫,靈活、方便且易于維護(hù)2021-09-09
springcloud安裝rabbitmq并配置延遲隊(duì)列插件的過程詳解
本期主要講解如何利用docker快速安裝rabbitmq并且配置延遲隊(duì)列插件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
如何保證RabbitMQ全鏈路數(shù)據(jù)100%不丟失問題
這篇文章主要介紹了如何保證RabbitMQ全鏈路數(shù)據(jù)100%不丟失問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼
這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請求中某個(gè)接口需要升級(jí),正常做法是升級(jí)我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03

