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的资料请关注脚本之家其它相关文章!
相关文章
JDK13.0.1安装与环境变量的配置教程图文详解(Win10平台为例)
这篇文章主要介绍了JDK13.0.1安装与环境变量的配置教程图文详解(Win10平台为例),本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2020-01-01
SpringBoot集成Redis之RedisTemplate实践
文章介绍了在使用SpringBoot与Redis进行交互时,遇到的中文乱码问题,并提供两种解决方法:1) 使用StringRedisTemplate;2) 自定义Redis序列化器,同时,还讨论了在集群模式下,若主机宕机,客户端如何动态感知集群状态2026-04-04
注册中心配置了spring security后客户端启动报错
这篇文章主要为大家介绍了注册中心配置了spring security后客户端启动报错问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07
EasyExcel读写、模板填充、Web上传下载入门到实战全指南
EasyExcel类是一套基于Java的开源Excel解析工具类,相较于传统的框架如Apache poi、jxl等更加快速、简洁,还可以解决大文件内存溢出问题,这篇文章主要介绍了EasyExcel读写、模板填充、Web上传下载入门到实战的相关资料,需要的朋友可以参考下2026-01-01
Spring @ConditionalOnMissingBean 注解的主要作用解析
@ConditionalOnMissingBean是Spring Boot中用于条件化配置的注解,确保只有在指定Bean不存在时才创建,它在自动配置中广泛应用,提供默认配置并允许用户自定义Bean,本文给大家介绍Spring @ConditionalOnMissingBean注解的作用,感兴趣的朋友一起看看吧2026-01-01


最新评论