欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析

 更新時(shí)間:2024年01月17日 08:46:02   作者:程序猿DD  
這篇文章主要介紹了Spring Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析,一起看看Spring Boot 中 JdbcClient 和 JdbcTemplate 之間的差異

引言

以下內(nèi)容使用的Java和Spring Boot版本為:

  • Java 21
  • Spring Boot 3.2.1

假設(shè)我們有一個(gè)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操作。

下面就來(lái)一起看看,分別使用 JDBC Client 和 JDBC Template 的實(shí)現(xiàn)。

初始化對(duì)比

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)對(duì)比

查詢的實(shí)現(xiàn)對(duì)比

getAllCustomer查詢返回集合數(shù)據(jù)的實(shí)現(xiàn)對(duì)比:

// 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)對(duì)比:

// 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)對(duì)比

// 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)對(duì)比

// 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)對(duì)比

// 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í)行增刪改查操作的代碼樣例。總體上來(lái)說,JdbcClient的實(shí)現(xiàn)更為簡(jiǎn)潔方便。如果不考慮其他ORM框架的情況下,在未來(lái)的Spring Boot版本中,我會(huì)更偏向于選擇JdbcClient來(lái)操作數(shù)據(jù)庫(kù)。

以上就是Spring Boot中的JdbcClient與JdbcTemplate使用對(duì)比分析的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot JdbcClient對(duì)比JdbcTemplate的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決Error occurred during initialization of VM Java虛擬機(jī)初始化失敗問題

    解決Error occurred during initialization o

    這篇文章主要介紹了解決Error occurred during initialization of VM Java虛擬機(jī)初始化失敗問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • JavaEE中volatile、wait和notify詳解

    JavaEE中volatile、wait和notify詳解

    這篇文章主要給大家介紹了關(guān)于JavaEE中volatile、wait和notify的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-02-02
  • Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺(tái)項(xiàng)目 附源碼

    Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺(tái)項(xiàng)目 附源碼

    這篇文章主要介紹了Spring Boot + Thymeleaf + Activiti 快速開發(fā)平臺(tái)項(xiàng)目附源碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Eclipse插件開發(fā)實(shí)現(xiàn)控制臺(tái)輸出信息的方法

    Eclipse插件開發(fā)實(shí)現(xiàn)控制臺(tái)輸出信息的方法

    今天小編就為大家分享一篇關(guān)于Eclipse插件開發(fā)實(shí)現(xiàn)控制臺(tái)輸出信息的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 解讀maven項(xiàng)目中Tomcat10與JSTL的問題匯總(Debug親身經(jīng)歷)

    解讀maven項(xiàng)目中Tomcat10與JSTL的問題匯總(Debug親身經(jīng)歷)

    這篇文章主要介紹了解讀maven項(xiàng)目中Tomcat10與JSTL的問題匯總(Debug親身經(jīng)歷),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java 回調(diào)函數(shù)深入理解

    Java 回調(diào)函數(shù)深入理解

    這篇文章主要介紹了 Java 回調(diào)函數(shù)深入理解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java--裝箱和拆箱詳解

    Java--裝箱和拆箱詳解

    本篇文章主要介紹了詳解Java 自動(dòng)裝箱與拆箱的實(shí)現(xiàn)原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2021-07-07
  • java中CompletableFuture異步執(zhí)行方法

    java中CompletableFuture異步執(zhí)行方法

    本文主要介紹了java中CompletableFuture異步執(zhí)行方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Mybatis的動(dòng)態(tài)拼接條件方式

    Mybatis的動(dòng)態(tài)拼接條件方式

    這篇文章主要介紹了Mybatis的動(dòng)態(tài)拼接條件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 詳解Spring Boot實(shí)戰(zhàn)之Filter實(shí)現(xiàn)使用JWT進(jìn)行接口認(rèn)證

    詳解Spring Boot實(shí)戰(zhàn)之Filter實(shí)現(xiàn)使用JWT進(jìn)行接口認(rèn)證

    本篇文章主要介紹了詳解Spring Boot實(shí)戰(zhàn)之Filter實(shí)現(xiàn)使用JWT進(jìn)行接口認(rèn)證,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07

最新評(píng)論