对比Spring Boot中的JdbcClient与JdbcTemplate
本文我们一起看看Spring Boot中 JdbcClient 和 JdbcTemplate 之间的差异。
以下内容使用的Java和Spring Boot版本为:
- Java 21
- Spring Boot 3.2.1
假设我们有一个ICustomerService接口:
public interface ICustomerService {
List<Customer> getAllCustomer();
Optional<Customer> getCustomerById(int id);
void insert(Customer customer);
void update(int id, Customer customer);
void delete(int id);
}
其中,涵盖了我们常见的数据CRUD操作。
下面就来一起看看,分别使用 JDBC Client 和 JDBC Template 的实现。
初始化对比
JdbcTemplate的初始化:
private final JdbcTemplate jdbcTemplate;
public CustomerJdbcTemplateService(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
JdbcClient的初始化;
private final JdbcClient jdbcClient;
public CustomerJdbcClientService(JdbcClient jdbcClient){
this.jdbcClient = jdbcClient;
}
增删改查的实现对比
如果您学习过程中如遇困难?可以加入我们超高质量的Spring技术交流群,参与交流与讨论,更好的学习与进步!更多Spring Boot教程可以点击直达!,欢迎收藏与转发支持!
查询的实现对比
getAllCustomer查询返回集合数据的实现对比:
// jdbcTemplate实现
private final RowMapper<Customer> rowMapper = (rs, row)
-> new Customer(rs.getInt("id"), rs.getString("name"), rs.getString("lastname"), rs.getDate("birthday"));
public List<Customer> getAllCustomer() {
return jdbcTemplate.query("select id, name, lastname, birthday from customer", rowMapper);
}
// jdbcClient实现
public List<Customer> getAllCustomer(){
return jdbcClient.sql("select id, name, lastname, birthday from customer").query(Customer.class).list();
}
getCustomerById查询返回单条数据的实现对比:
// jdbcTemplate实现
public Optional<Customer> getCustomerById(int id) {
Customer customer = null;
try {
customer = jdbcTemplate.queryForObject("select id, name, lastname, birthday from customer where id = ?", rowMapper, id );
} catch (DataAccessException ex){
LOG.error("Data not found. Id parameter: " + id, ex);
}
return Optional.ofNullable(customer);
}
// jdbcClient实现
public Optional<Customer> getCustomerById(int id){
return jdbcClient.sql("select id, name, lastname, birthday from customer where id= :id")
.param("id", id)
.query(Customer.class)
.optional();
}
insert插入数据的实现对比
// jdbcTemplate实现
public void insert(Customer customer) {
int inserted = jdbcTemplate.update("insert into customer (id, name, lastname, birthday) values (?,?,?,?)",
customer.id(), customer.name(), customer.lastname(),customer.birthday());
Assert.state(inserted == 1 , "An exception error occurred while inserting customer");
}
// jdbcClient实现
public void insert(Customer customer){
int inserted = jdbcClient.sql("insert into customer (id, name, lastname, birthday) values (?,?,?,?)")
.params(List.of(customer.id(), customer.name(), customer.lastname(), customer.birthday()))
.update();
Assert.state(inserted == 1 , "An exception error occurred while inserting customer");
}
update更新数据的实现对比
// jdbcTemplate实现
public void update(int id, Customer customer) {
int updated = jdbcTemplate.update("update customer set name = ?, lastname = ?, birthday = ? where id = ? ",
customer.name(), customer.lastname(),customer.birthday(), id);
Assert.state(updated == 1 , "An exception error occurred while updating customer");
}
// jdbcClient实现
public void update(int id, Customer customer){
int updated = jdbcClient.sql("update customer set name = ?, lastname = ?, birthday = ? where id = ?")
.params(List.of(customer.name(), customer.lastname(), customer.birthday(), id))
.update();
Assert.state(updated == 1, "An exception error occurred while updating customer");
}
delete删除数据的实现对比
// jdbcTemplate实现
public void delete(int id) {
int deleted = jdbcTemplate.update("delete from customer where id = ?", id);
Assert.state(deleted == 1 , "An exception error occurred while deleting customer");
}
// jdbcClient实现
public void delete(int id) {
int deleted = jdbcClient.sql("delete from customer where id = :id").param("id",id).update();
Assert.state(deleted == 1, "An exception error occurred while updating customer");
}
总结
上面我们分别演示了JdbcClient 和 JdbcTemplate从初始化到真正执行增删改查操作的代码样例。总体上来说,JdbcClient的实现更为简洁方便。如果不考虑其他ORM框架的情况下,在未来的Spring Boot版本中,我会更偏向于选择JdbcClient来操作数据库。那么您觉得怎么样呢?留言区一起聊聊~
欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源
对比Spring Boot中的JdbcClient与JdbcTemplate的更多相关文章
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...
- Spring Boot中使用Swagger2构建RESTful APIs介绍
1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...
- Spring Boot 中 Redis 的使用
Spring Boot 对常用的数据库支持外,对 Nosql 数据库也进行了封装自动化,如Redis.MongoDB等,本文主要介绍Redis的使用. Redis 介绍 Redis 是目前业界使用最广 ...
- 传统的Servlet在spring boot中怎么实现的?
传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...
- Spring Boot中的Properties
文章目录 简介 使用注解注册一个Properties文件 使用属性文件 Spring Boot中的属性文件 @ConfigurationProperties yaml文件 Properties环境变量 ...
- 在 Spring Boot 中使用 HikariCP 连接池
上次帮小王解决了如何在 Spring Boot 中使用 JDBC 连接 MySQL 后,我就一直在等,等他问我第三个问题,比如说如何在 Spring Boot 中使用 HikariCP 连接池.但我等 ...
- 在 Spring Boot 中使用 Flyway
一.Flyway 介绍 Flyway 是一个开源的数据库迁移工具,MySQL, SQL Server, Oracle 等二十多种数据库 在 Flyway 中数据库的所有改变均称为迁移(migratio ...
- 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数
使用 @Value("${property}") 注释注入配置属性有时会很麻烦,尤其是当你使用多个属性或你的数据是分层的时候. Spring Boot 引入了一个可替换的方案 -- ...
- Spring Boot中使用过滤器和拦截器
过滤器(Filter)和拦截器(Interceptor)是Web项目中常用的两个功能,本文将简单介绍在Spring Boot中使用过滤器和拦截器来计算Controller中方法的执行时长,并且简单对比 ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
随机推荐
- MySQL系列之MyCat——架构图、准备、安装、配置文件、垂直分表、MyCAT核心特性——分片(水平拆分)、范围分片、枚举分片、Mycat全局表、E-R分片
文章目录 1. MyCAT基础架构图 2. MyCAT基础架构准备 2.1 环境准备: 2.2 删除历史环境: 2.3 创建相关目录初始化数据 2.4 准备配置文件和启动脚本 2.5 修改权限,启动多 ...
- OPPO主题组件开发 - 组件内容自适应
OPPO桌面有 3*5.3*6.4*5.4*6.5*5.5*6 等布局,随着布局不同,组件大小也会发生改变:不同型号手机分辨率不同,组件大小也不一致.这就要求组件内容做到自适应. 说明 OPPO主题组 ...
- Hugging Face 分词器新增聊天模板属性
一个幽灵,格式不正确的幽灵,在聊天模型中游荡! 太长不看版 现存的聊天模型使用的训练数据格式各各不同,我们需要用这些格式将对话转换为单个字符串并传给分词器.如果我们在微调或推理时使用的格式与模型训练时 ...
- Vue之style的用法
Vue中style的用法总结如下: v-bind:style 简写:style 1.基本用法 <!DOCTYPE html> <html lang="en"&g ...
- 机器学习实战5-KMeans聚类算法
概述 聚类 VS 分类 有监督学习 VS 无监督学习 sklearn中的聚类算法 KMeans KMeans参数&接口 n_clusters n_clusters就是KMeans中的K就是告诉 ...
- 阿里云创建BUCKET脚本
创建BUCKET脚本 安装模块 pip install pymysql pip install aliyun-python-sdk-core pip install aliyun-python-sdk ...
- PostgreSQL 提升子连接与 ORACLE 子查询非嵌套
查询优化器对子查询一般采用嵌套执行的方式,即父查询中的每一行,都要执行一次子查询,这样子查询会执行很多次,效率非常低. 例如 exists.not exists 逐行取出经行匹配处理,项目中使用子查询 ...
- 使用ResponseSelector实现校园招聘FAQ机器人
本文主要介绍使用ResponseSelector实现校园招聘FAQ机器人,回答面试流程和面试结果查询的FAQ问题.FAQ机器人功能分为业务无关的功能和业务相关的功能2类. 一.data/nlu.y ...
- 「保姆级」网络爬虫教程(二):教你下载文库中的PDF文档!
如何点赞再看,养成习惯.微信公众号搜索「Job Yan」关注这个爱发技术干货的 Coder.本文 GitHub https://github.com/JobYan/PythonPearls 已收录,还 ...
- shell 语法介绍
大家好,我是蓝胖子,在日常开发中或多或少都会接触到shell脚本,可以说会shell脚本是一位后端开发的基本功,今天我将会花上一篇文章总结下常见的shell的语法,学完本篇,相信简单的shell脚本就 ...