javascript
对比Spring Boot中的JdbcClient与JdbcTemplate
本文我們一起看看Spring Boot中 JdbcClient 和 JdbcTemplate 之間的差異。
以下內(nèi)容使用的Java和Spring Boot版本為:
- Java 21
- Spring Boot 3.2.1
假設(shè)我們有一個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);
}
其中,涵蓋了我們常見的數(shù)據(jù)CRUD操作。
下面就來一起看看,分別使用 JDBC Client 和 JDBC Template 的實(shí)現(xiàn)。
初始化對比
JdbcTemplate的初始化:
private final JdbcTemplate jdbcTemplate;
public CustomerJdbcTemplateService(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
JdbcClient的初始化;
private final JdbcClient jdbcClient;
public CustomerJdbcClientService(JdbcClient jdbcClient){
this.jdbcClient = jdbcClient;
}
增刪改查的實(shí)現(xiàn)對比
如果您學(xué)習(xí)過程中如遇困難?可以加入我們超高質(zhì)量的Spring技術(shù)交流群,參與交流與討論,更好的學(xué)習(xí)與進(jìn)步!更多Spring Boot教程可以點(diǎn)擊直達(dá)!,歡迎收藏與轉(zhuǎn)發(fā)支持!
查詢的實(shí)現(xiàn)對比
getAllCustomer查詢返回集合數(shù)據(jù)的實(shí)現(xiàn)對比:
// jdbcTemplate實(shí)現(xiàn)
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實(shí)現(xiàn)
public List<Customer> getAllCustomer(){
return jdbcClient.sql("select id, name, lastname, birthday from customer").query(Customer.class).list();
}
getCustomerById查詢返回單條數(shù)據(jù)的實(shí)現(xiàn)對比:
// jdbcTemplate實(shí)現(xiàn)
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實(shí)現(xiàn)
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插入數(shù)據(jù)的實(shí)現(xiàn)對比
// jdbcTemplate實(shí)現(xiàn)
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實(shí)現(xiàn)
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更新數(shù)據(jù)的實(shí)現(xiàn)對比
// jdbcTemplate實(shí)現(xiàn)
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實(shí)現(xiàn)
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刪除數(shù)據(jù)的實(shí)現(xiàn)對比
// jdbcTemplate實(shí)現(xiàn)
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實(shí)現(xiàn)
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");
}
總結(jié)
上面我們分別演示了JdbcClient 和 JdbcTemplate從初始化到真正執(zhí)行增刪改查操作的代碼樣例。總體上來說,JdbcClient的實(shí)現(xiàn)更為簡潔方便。如果不考慮其他ORM框架的情況下,在未來的Spring Boot版本中,我會更偏向于選擇JdbcClient來操作數(shù)據(jù)庫。那么您覺得怎么樣呢?留言區(qū)一起聊聊~
歡迎關(guān)注我的公眾號:程序猿DD。第一時間了解前沿行業(yè)消息、分享深度技術(shù)干貨、獲取優(yōu)質(zhì)學(xué)習(xí)資源
總結(jié)
以上是生活随笔為你收集整理的对比Spring Boot中的JdbcClient与JdbcTemplate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu安装mysql8,debia
- 下一篇: 尊嘟假嘟?三行代码提升接口性能600倍