本文我们一起看看Spring Boot中 JdbcClientJdbcTemplate 之间的差异。

以下内容使用的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 ClientJDBC 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");
}

总结

上面我们分别演示了JdbcClientJdbcTemplate从初始化到真正执行增删改查操作的代码样例。总体上来说,JdbcClient的实现更为简洁方便。如果不考虑其他ORM框架的情况下,在未来的Spring Boot版本中,我会更偏向于选择JdbcClient来操作数据库。那么您觉得怎么样呢?留言区一起聊聊~

欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源

对比Spring Boot中的JdbcClient与JdbcTemplate的更多相关文章

  1. Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅

    在上一篇Spring中使用JdbcTemplate访问数据库中介绍了一种基本的数据访问方式,结合构建RESTful API和使用Thymeleaf模板引擎渲染Web视图的内容就已经可以完成App服务端 ...

  2. Spring Boot中使用Swagger2构建RESTful APIs介绍

    1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...

  3. Spring Boot 中 Redis 的使用

    Spring Boot 对常用的数据库支持外,对 Nosql 数据库也进行了封装自动化,如Redis.MongoDB等,本文主要介绍Redis的使用. Redis 介绍 Redis 是目前业界使用最广 ...

  4. 传统的Servlet在spring boot中怎么实现的?

    传统的Servlet在spring boot中怎么实现的? 本文主要内容: 1:springboot一些介绍 2:传统的servlete项目在spring boot项目中怎么实现的?web.xml.u ...

  5. Spring Boot中的Properties

    文章目录 简介 使用注解注册一个Properties文件 使用属性文件 Spring Boot中的属性文件 @ConfigurationProperties yaml文件 Properties环境变量 ...

  6. 在 Spring Boot 中使用 HikariCP 连接池

    上次帮小王解决了如何在 Spring Boot 中使用 JDBC 连接 MySQL 后,我就一直在等,等他问我第三个问题,比如说如何在 Spring Boot 中使用 HikariCP 连接池.但我等 ...

  7. 在 Spring Boot 中使用 Flyway

    一.Flyway 介绍 Flyway 是一个开源的数据库迁移工具,MySQL, SQL Server, Oracle 等二十多种数据库 在 Flyway 中数据库的所有改变均称为迁移(migratio ...

  8. 利用 Spring Boot 中的 @ConfigurationProperties,优雅绑定配置参数

    使用 @Value("${property}") 注释注入配置属性有时会很麻烦,尤其是当你使用多个属性或你的数据是分层的时候. Spring Boot 引入了一个可替换的方案 -- ...

  9. Spring Boot中使用过滤器和拦截器

    过滤器(Filter)和拦截器(Interceptor)是Web项目中常用的两个功能,本文将简单介绍在Spring Boot中使用过滤器和拦截器来计算Controller中方法的执行时长,并且简单对比 ...

  10. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

随机推荐

  1. 模块化打包工具-初识Webpack

    1. 为什么需要模块化打包工具 在上一篇文章中提到的ES Module可以帮助开发者更好地组织代码,完成js文件的模块化,基本解决了模块化的问题,但是实际开发中仅仅完成js文件的模块化是不够的,尤其是 ...

  2. 推荐免费的svn空间(SVN代码托管)

    推荐免费的svn空间(SVN代码托管) 最近研究了国内和国外的免费svn空间,SVN代码托管,SVN在线,代码托管中心,有所心得. 1.http://www.svn999.com/ [推荐]国内的,免 ...

  3. Java多线程编程的优点和缺点

    优点: 加快响应用户的时间:多线程允许并发执行多个任务,可以充分利用多核处理器,从而提高程序的性能和响应速度.比如我们经常用的迅雷下载,都喜欢多开几个线程去下载,谁都不愿意用一个线程去下载,为什么呢? ...

  4. Redis中的缓存雪崩、缓存击穿、缓存穿透问题

    1. 什么是缓存雪崩 当我们提到缓存系统中的问题,缓存雪崩是一个经常被讨论的话题.缓存雪崩是指在某一时刻发生大量的缓存失效,导致瞬间大量的请求直接打到了数据库,可能会导致数据库瞬间压力过大甚至宕机.尤 ...

  5. Apache协议原文及中文翻译

    Apache协议原文及中文翻译 参考链接 原文 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TER ...

  6. 🎊OpenTiny Vue 3.11.0 发布:增加富文本、ColorPicker等4个新组件,迎来了贡献者大爆发!

    你好,我是 Kagol. 非常高兴跟大家宣布,2023年10月24日,OpenTiny Vue 发布了 v3.11.0 . OpenTiny 每次大版本发布,都会给大家带来一些实用的新特性,8.14 ...

  7. RLChina2022公开课-博弈搜索算法

    序列决策 序列决策问题一般用马尔可夫决策模型进行描述 搜索算法的优化

  8. 全面掌握胶囊网络:从基础理论到PyTorch实战

    本文全面深入地探讨了胶囊网络(Capsule Networks)的原理.构建块.数学模型以及在PyTorch中的实现.通过本文,读者不仅能够理解胶囊网络的基础概念和高级数学原理,还能掌握其在实际问题中 ...

  9. JUC并发编程学习笔记(六)Callable(简单)

    Callable(简单) callable接口和runnable接口类似,都是为了执行另外一条线程而设计的,区别是Runnable不会返回结果也不会抛出异常. 1.可以有返回值 2.可以抛出异常 3. ...

  10. Windows10+Python+Yolov8+ONNX图片缺陷识别,并在原图中标记缺陷,有onnx模型则无需配置,无需训练。

    目录 一.训练自己数据集的YOLOv8模型 1.博主电脑配置 2.深度学习GPU环境配置 3.yolov8深度学习环境准备 4.准备数据集 二.Python+Onnx模型进行图像缺陷检测,并在原图中标 ...