对比Spring Boot中的JdbcClient与JdbcTemplate

打印 上一主题 下一主题

主题 862|帖子 862|积分 2586

本文我们一起看看Spring Boot中 JdbcClient 和 JdbcTemplate 之间的差异。
以下内容使用的Java和Spring Boot版本为:

  • Java 21
  • Spring Boot 3.2.1
假设我们有一个ICustomerService接口:
  1. public interface ICustomerService {
  2.    
  3.     List<Customer> getAllCustomer();
  4.     Optional<Customer> getCustomerById(int id);
  5.     void insert(Customer customer);
  6.     void update(int id, Customer customer);
  7.    
  8.     void delete(int id);
  9. }
复制代码
其中,涵盖了我们常见的数据CRUD操作。
下面就来一起看看,分别使用 JDBC Client 和 JDBC Template 的实现。
初始化对比

JdbcTemplate的初始化:
  1. private final JdbcTemplate jdbcTemplate;
  2. public CustomerJdbcTemplateService(JdbcTemplate jdbcTemplate){
  3.   this.jdbcTemplate = jdbcTemplate;
  4. }
复制代码
JdbcClient的初始化;
  1. private final JdbcClient jdbcClient;
  2. public CustomerJdbcClientService(JdbcClient jdbcClient){
  3.   this.jdbcClient = jdbcClient;
  4. }
复制代码
增删改查的实现对比

如果您学习过程中如遇困难?可以加入我们超高质量的Spring技术交流群,参与交流与讨论,更好的学习与进步!更多Spring Boot教程可以点击直达!,欢迎收藏与转发支持!
查询的实现对比

getAllCustomer查询返回集合数据的实现对比:
  1. // jdbcTemplate实现
  2. private final RowMapper<Customer> rowMapper = (rs, row)
  3.      -> new Customer(rs.getInt("id"), rs.getString("name"), rs.getString("lastname"), rs.getDate("birthday"));
  4. public List<Customer> getAllCustomer() {
  5.   return jdbcTemplate.query("select id, name, lastname, birthday from customer", rowMapper);
  6. }
  7. // jdbcClient实现
  8. public List<Customer> getAllCustomer(){
  9.   return jdbcClient.sql("select id, name, lastname, birthday from customer").query(Customer.class).list();
  10. }
复制代码
getCustomerById查询返回单条数据的实现对比:
  1. // jdbcTemplate实现
  2. public Optional<Customer> getCustomerById(int id) {
  3.   Customer customer = null;
  4.   try {
  5.     customer = jdbcTemplate.queryForObject("select id, name, lastname, birthday from customer where id = ?", rowMapper,  id );
  6.   } catch (DataAccessException ex){
  7.     LOG.error("Data not found. Id parameter: " + id, ex);
  8.   }
  9.   return Optional.ofNullable(customer);
  10. }
  11. // jdbcClient实现
  12. public Optional<Customer> getCustomerById(int id){
  13.   return jdbcClient.sql("select id, name, lastname, birthday from customer where id= :id")
  14.                    .param("id", id)
  15.                    .query(Customer.class)
  16.                    .optional();
  17. }
复制代码
insert插入数据的实现对比
  1. // jdbcTemplate实现
  2. public void insert(Customer customer) {
  3.   int inserted = jdbcTemplate.update("insert into customer (id, name, lastname, birthday) values (?,?,?,?)",
  4.                  customer.id(), customer.name(), customer.lastname(),customer.birthday());
  5.   Assert.state(inserted == 1 , "An exception error occurred while inserting customer");
  6. }
  7. // jdbcClient实现
  8. public void insert(Customer customer){
  9.   int inserted = jdbcClient.sql("insert into customer (id, name, lastname, birthday) values (?,?,?,?)")
  10.                 .params(List.of(customer.id(), customer.name(), customer.lastname(), customer.birthday()))
  11.                 .update();
  12.   Assert.state(inserted == 1 , "An exception error occurred while inserting customer");
  13. }
复制代码
update更新数据的实现对比
  1. // jdbcTemplate实现
  2. public void update(int id, Customer customer) {
  3.   int updated = jdbcTemplate.update("update customer set name = ?, lastname = ?, birthday = ? where id = ? ",
  4.                 customer.name(), customer.lastname(),customer.birthday(), id);
  5.   Assert.state(updated == 1 , "An exception error occurred while updating customer");
  6. }
  7. // jdbcClient实现
  8. public void update(int id, Customer customer){
  9.   int updated = jdbcClient.sql("update customer set name = ?, lastname = ?, birthday = ? where id = ?")
  10.                 .params(List.of(customer.name(), customer.lastname(), customer.birthday(), id))
  11.                 .update();
  12.   Assert.state(updated == 1, "An exception error occurred while updating customer");
  13. }
复制代码
delete删除数据的实现对比
  1. // jdbcTemplate实现
  2. public void delete(int id) {
  3.   int deleted = jdbcTemplate.update("delete from customer where id = ?", id);
  4.   Assert.state(deleted == 1 , "An exception error occurred while deleting customer");
  5. }
  6. // jdbcClient实现
  7. public void delete(int id) {
  8.   int deleted = jdbcClient.sql("delete from customer where id = :id").param("id",id).update();
  9.   Assert.state(deleted == 1, "An exception error occurred while updating customer");
  10. }
复制代码
总结

上面我们分别演示了JdbcClient 和 JdbcTemplate从初始化到真正执行增删改查操作的代码样例。总体上来说,JdbcClient的实现更为简洁方便。如果不考虑其他ORM框架的情况下,在未来的Spring Boot版本中,我会更偏向于选择JdbcClient来操作数据库。那么您觉得怎么样呢?留言区一起聊聊~
欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

涛声依旧在

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表