对比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支持更丰富的数据结 ...
随机推荐
- Oracle11g安装教程(带安装包)
找了半天没在官网上找到Oracle11g的安装包下载,又找了半天,终于在网上的一个教程里找到安装包的网盘链接.现在在这记一下防止以后重新找麻烦. 网盘链接 百度云盘链接:[https://pan.ba ...
- go 流程控制之switch 语句介绍
go 流程控制之switch 语句介绍 目录 go 流程控制之switch 语句介绍 一.switch语句介绍 1.1 认识 switch 语句 1.2 基本语法 二.Go语言switch语句中cas ...
- oracle RAC redhat
RAC比较严格,如果操作系统不纯净,容易失败: 装备第一台VM:chkconfig sendmail offchkconfig iptables offchkconfig ip6talbes offs ...
- 如何通过代码混淆绕过苹果机审,解决APP被拒问题
目录 iOS代码混淆 功能分析 实现流程 类名修改 方法名修改 生成垃圾代码 替换png等静态资源MD5 info.plist文件添加垃圾字段 功能分析 实现流程 类名修改 方法名修改 生成垃圾代码 ...
- 模块化打包工具-Webpack插件与其他功能
1.Webpack插件机制 上一篇提到的webpack的loader可以用来加载资源,包括各种css,图片文件资源,实现打包文件的功能,而webpack的插件则起到了加强webpack的作用,可以完成 ...
- 快速展示原型之Minimal API开发
Minimal API官网地址: https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/minimal-apis/security?vi ...
- c# 引入同一个版本dll(比如包含opencv不同模块的dll,但是版本却是一致的)
1.使用dnSpy.exe工具,打开相关dll 在左侧"管理器"中选中并点击右键 2.在弹出菜单中点击编辑程序集 3.更改名称和版本号 4.工具栏-文件-全部保存 5.在VS中引入 ...
- http1.x,http2.0,https分别介绍以及他们的区别
一.HTTP/1.x Http1.x 缺陷:线程阻塞,在同一时间,同一域名的请求有一定数量限制,超过限制数目的请求会被阻塞 http1.0 缺陷:浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要 ...
- spring---反射(java.lang.reflect)
反射简介 反射是Java的高级特性之一,但是在实际的开发中,使用Java反射的案例却非常的少,但是反射确实在底层框架中被频繁的使用. 比如:JDBC中的加载数据库驱动程序,Spring框架中加载bea ...
- Prometheus+Grafana实现服务性能监控:windows主机监控、Spring Boot监控、Spring Cloud Alibaba Seata监控
1.Prometheus介绍 Prometheus使用Go语言开发,中文名称叫:普罗 米修斯.Prometheus是一个开源系统最初在SoundCloud构建的监控和警报工具包.自 2012 年成立以 ...