最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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);
}

其中,涵蓋了我們常見(jiàn)的數(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í)行增刪改查操作的代碼樣例??傮w上來(lái)說(shuō),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)文章

  • JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺(tái)為例)

    JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺(tái)為例)

    這篇文章主要介紹了JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺(tái)為例),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • SpringBoot集成Redis之RedisTemplate實(shí)踐

    SpringBoot集成Redis之RedisTemplate實(shí)踐

    文章介紹了在使用SpringBoot與Redis進(jìn)行交互時(shí),遇到的中文亂碼問(wèn)題,并提供兩種解決方法:1) 使用StringRedisTemplate;2) 自定義Redis序列化器,同時(shí),還討論了在集群模式下,若主機(jī)宕機(jī),客戶端如何動(dòng)態(tài)感知集群狀態(tài)
    2026-04-04
  • SpringBoot服務(wù)拆包打包的詳細(xì)實(shí)現(xiàn)過(guò)程

    SpringBoot服務(wù)拆包打包的詳細(xì)實(shí)現(xiàn)過(guò)程

    文章詳細(xì)介紹了如何使用Maven插件對(duì)SpringBoot服務(wù)進(jìn)行拆包打包,包括使用maven-shade-plugin和maven-jar-plugin配置,以及如何通過(guò)assembly.xml實(shí)現(xiàn)自定義打包結(jié)構(gòu),需要的朋友可以參考下
    2026-02-02
  • IDEA 2023 spring項(xiàng)目配置Maven全過(guò)程

    IDEA 2023 spring項(xiàng)目配置Maven全過(guò)程

    在IDEA中配置Maven需依次打開設(shè)置,搜索并配置Maven,成功時(shí)顯示BUILD SUCCESS,此方法實(shí)用,供開發(fā)者參考,歡迎支持腳本之家
    2025-09-09
  • 注冊(cè)中心配置了spring?security后客戶端啟動(dòng)報(bào)錯(cuò)

    注冊(cè)中心配置了spring?security后客戶端啟動(dòng)報(bào)錯(cuò)

    這篇文章主要為大家介紹了注冊(cè)中心配置了spring?security后客戶端啟動(dòng)報(bào)錯(cuò)問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Java解決約瑟夫問(wèn)題代碼實(shí)例

    Java解決約瑟夫問(wèn)題代碼實(shí)例

    這篇文章主要介紹了Java解決約瑟夫(環(huán))問(wèn)題的代碼實(shí)例,決約瑟問(wèn)題貌似經(jīng)常出現(xiàn)在面試題中,需要的朋友可以參考下
    2014-03-03
  • EasyExcel讀寫、模板填充、Web上傳下載入門到實(shí)戰(zhàn)全指南

    EasyExcel讀寫、模板填充、Web上傳下載入門到實(shí)戰(zhàn)全指南

    EasyExcel類是一套基于Java的開源Excel解析工具類,相較于傳統(tǒng)的框架如Apache poi、jxl等更加快速、簡(jiǎn)潔,還可以解決大文件內(nèi)存溢出問(wèn)題,這篇文章主要介紹了EasyExcel讀寫、模板填充、Web上傳下載入門到實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考下
    2026-01-01
  • Java布隆過(guò)濾器的原理和實(shí)現(xiàn)分析

    Java布隆過(guò)濾器的原理和實(shí)現(xiàn)分析

    數(shù)組、鏈表、樹等數(shù)據(jù)結(jié)構(gòu)會(huì)存儲(chǔ)元素的內(nèi)容,一旦數(shù)據(jù)量過(guò)大,消耗的內(nèi)存也會(huì)呈現(xiàn)線性增長(zhǎng)所以布隆過(guò)濾器是為了解決數(shù)據(jù)量大的一種數(shù)據(jù)結(jié)構(gòu)。本文就來(lái)和大家詳細(xì)說(shuō)說(shuō)布隆過(guò)濾器的原理和實(shí)現(xiàn),感興趣的可以了解一下
    2022-10-10
  • Spring @ConditionalOnMissingBean 注解的主要作用解析

    Spring @ConditionalOnMissingBean 注解的主要作用解析

    @ConditionalOnMissingBean是Spring Boot中用于條件化配置的注解,確保只有在指定Bean不存在時(shí)才創(chuàng)建,它在自動(dòng)配置中廣泛應(yīng)用,提供默認(rèn)配置并允許用戶自定義Bean,本文給大家介紹Spring @ConditionalOnMissingBean注解的作用,感興趣的朋友一起看看吧
    2026-01-01
  • 服務(wù)器實(shí)現(xiàn)Java遠(yuǎn)程訪問(wèn)Linux服務(wù)器方式(JSch)

    服務(wù)器實(shí)現(xiàn)Java遠(yuǎn)程訪問(wèn)Linux服務(wù)器方式(JSch)

    文章介紹了如何使用Java遠(yuǎn)程訪問(wèn)Linux服務(wù)器,主要包括建立SSH連接、使用JSch庫(kù)執(zhí)行命令、解析返回值以及關(guān)閉連接的步驟
    2024-11-11

最新評(píng)論

安吉县| 鄂州市| 沁阳市| 苍梧县| 抚宁县| 常德市| 莒南县| 松潘县| 都兰县| 旬邑县| 高安市| 桂林市| 新疆| 留坝县| 乌鲁木齐市| 正宁县| 启东市| 文成县| 温泉县| 格尔木市| 玉屏| 浙江省| 龙山县| 松江区| 乌审旗| 房产| 垫江县| 尖扎县| 光泽县| 韶山市| 柳河县| 荆门市| 河津市| 吴桥县| 旺苍县| 安国市| 宜宾市| 琼海市| 嘉禾县| 阿克陶县| 二连浩特市|