对比SpringBoot中的JdbcClient与JdbcTemplate

哥们看看码农 2024-01-13 14:48:13
本文我们一起看看Spring Boot中 JdbcClient 和 JdbcTemplate 之间的差异。 以下内容使用的Java和Spring Boot版本为: Java 21Spring Boot 3.2.1假设我们有一个ICustomerService接口: public interface ICustomerService { List getAllCustomer(); Optional 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 rowMapper = (rs, row) -> new Customer(rs.getInt("id"), rs.getString("name"), rs.getString("lastname"), rs.getDate("birthday"));public List getAllCustomer() { return jdbcTemplate.query("select id, name, lastname, birthday from customer", rowMapper);}// jdbcClient实现public List getAllCustomer(){ return jdbcClient.sql("select id, name, lastname, birthday from customer").query(Customer.class).list();}getCustomerById查询返回单条数据的实现对比: // jdbcTemplate实现public Optional 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 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来操作数据库。那么您觉得怎么样呢?留言区一起聊聊~ 来源:https://www.didispace.com/article/spring-boot/spring-boot-jdbcclient-jdbctemplate.html
0 阅读:43

哥们看看码农

简介:感谢大家的关注