Spring Boot中的JdbcClient与JdbcTemplate使用对比分析

 更新时间:2024年01月17日 08:46:02   作者:程序猿DD  
这篇文章主要介绍了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;
}

增删改查的实现对比

查询的实现对比

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来操作数据库。

以上就是Spring Boot中的JdbcClient与JdbcTemplate使用对比分析的详细内容,更多关于Spring Boot JdbcClient对比JdbcTemplate的资料请关注脚本之家其它相关文章!

相关文章

  • 解决Error occurred during initialization of VM Java虚拟机初始化失败问题

    解决Error occurred during initialization o

    这篇文章主要介绍了解决Error occurred during initialization of VM Java虚拟机初始化失败问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • JavaEE中volatile、wait和notify详解

    JavaEE中volatile、wait和notify详解

    这篇文章主要给大家介绍了关于JavaEE中volatile、wait和notify的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2023-02-02
  • Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码

    Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码

    这篇文章主要介绍了Spring Boot + Thymeleaf + Activiti 快速开发平台项目附源码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Eclipse插件开发实现控制台输出信息的方法

    Eclipse插件开发实现控制台输出信息的方法

    今天小编就为大家分享一篇关于Eclipse插件开发实现控制台输出信息的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • 解读maven项目中Tomcat10与JSTL的问题汇总(Debug亲身经历)

    解读maven项目中Tomcat10与JSTL的问题汇总(Debug亲身经历)

    这篇文章主要介绍了解读maven项目中Tomcat10与JSTL的问题汇总(Debug亲身经历),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java 回调函数深入理解

    Java 回调函数深入理解

    这篇文章主要介绍了 Java 回调函数深入理解的相关资料,需要的朋友可以参考下
    2017-03-03
  • Java--装箱和拆箱详解

    Java--装箱和拆箱详解

    本篇文章主要介绍了详解Java 自动装箱与拆箱的实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-07-07
  • java中CompletableFuture异步执行方法

    java中CompletableFuture异步执行方法

    本文主要介绍了java中CompletableFuture异步执行方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Mybatis的动态拼接条件方式

    Mybatis的动态拼接条件方式

    这篇文章主要介绍了Mybatis的动态拼接条件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • 详解Spring Boot实战之Filter实现使用JWT进行接口认证

    详解Spring Boot实战之Filter实现使用JWT进行接口认证

    本篇文章主要介绍了详解Spring Boot实战之Filter实现使用JWT进行接口认证,具有一定的参考价值,有兴趣的可以了解一下
    2017-07-07

最新评论