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

Spring Cache優(yōu)化數(shù)據(jù)庫訪問的項目實踐

 更新時間:2024年01月07日 11:55:51   作者:冷風(fēng)扇666  
本文主要介紹了Spring Cache優(yōu)化數(shù)據(jù)庫訪問的項目實踐,將創(chuàng)建一個簡單的圖書管理應(yīng)用作為示例,并演示如何通過緩存減少對數(shù)據(jù)庫的頻繁查詢,感興趣的可以了解一下

在這篇博客中,我們將學(xué)習(xí)如何使用Spring Cache來優(yōu)化數(shù)據(jù)庫訪問,提高系統(tǒng)性能。我們將創(chuàng)建一個簡單的圖書管理應(yīng)用作為示例,并演示如何通過緩存減少對數(shù)據(jù)庫的頻繁查詢。

1. 項目結(jié)構(gòu)

首先,我們看一下項目的基本結(jié)構(gòu):

lfsun-study-cacheable
|-- src
|   |-- main
|       |-- java
|           |-- com.lfsun.cacheable
|               |-- controller
|                   |-- BookController.java
|               |-- dao
|                   |-- BookRepository.java
|               |-- entity
|                   |-- Book.java
|               |-- service
|                   |-- BookService.java
|                   |-- impl
|                       |-- BookServiceImpl.java
|               |-- LfsunStudyCacheableApplication.java
|       |-- resources
|           |-- application.properties
|-- pom.xml

2. 項目依賴

我們使用了Spring Boot和Spring Data JPA來簡化項目配置。以下是主要的Maven依賴:

<!-- Spring Boot Starter -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- Spring Boot Starter Test -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<!-- Spring Boot Starter Web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot Starter Data JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Lombok for easy POJOs -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<!-- MySQL Connector -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>

3. 數(shù)據(jù)庫配置

配置MySQL數(shù)據(jù)庫連接信息和Hibernate方言:

spring.datasource.url=jdbc:mysql://localhost:3306/lfsun_study
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

4. 實體類

定義一個簡單的實體類Book,用于表示圖書信息:

@Entity
@Data
@Table(name = "cacheable_book")
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
}

5. 數(shù)據(jù)訪問層

創(chuàng)建一個JPA Repository接口 BookRepository,用于數(shù)據(jù)庫交互:

public interface BookRepository extends JpaRepository<Book, Long> {
    Book findByTitle(String title);
    void deleteByTitle(String title);
}

6. 服務(wù)層

實現(xiàn)一個 BookService 接口,并創(chuàng)建具體的服務(wù)實現(xiàn) BookServiceImpl。在服務(wù)實現(xiàn)中,使用Spring Cache注解來優(yōu)化數(shù)據(jù)庫訪問:

@Service
@AllArgsConstructor
public class BookServiceImpl implements BookService {

    private final BookRepository bookRepository;

    @Override
    @Cacheable(value = "books", key = "#title")
    public Book getByTitle(String title) {
        System.out.println("Getting book from database for title: " + title);
        return bookRepository.findByTitle(title);
    }

    @Override
    @CachePut(value = "books", key = "#result.title")
    public Book save(Book book) {
        System.out.println("Saving book to database: " + book.getTitle());
        return bookRepository.save(book);
    }

    @Override
    @Transactional
    @CacheEvict(value = "books", key = "#title")
    public void deleteByTitle(String title) {
        System.out.println("Deleting book from database for title: " + title);
        bookRepository.deleteByTitle(title);
    }
}

7. 控制器層

創(chuàng)建REST控制器 BookController 處理圖書的獲取、保存和刪除操作:

@RestController
@RequestMapping("/books")
@AllArgsConstructor
public class BookController {

    private final BookService bookService;

    @GetMapping("/{title}")
    public Book getBookByTitle(@PathVariable String title) {
        return bookService.getByTitle(title);
    }

    @PostMapping
    public Book saveBook(@RequestBody Book book) {
        return bookService.save(book);
    }

    @PostMapping("/delete/{title}")
    public ResponseEntity<String> deleteBookByTitle(@PathVariable String title) {
        bookService.deleteByTitle(title);
        return new ResponseEntity<>("Book deleted successfully", HttpStatus.OK);
    }
}

8. 主應(yīng)用程序類

在主應(yīng)用程序類 LfsunStudyCacheableApplication 上啟用Spring緩存:

@SpringBootApplication
@EnableCaching
public class LfsunStudyCacheableApplication {

    public static void main(String[] args) {
        SpringApplication.run(LfsunStudyCacheableApplication.class, args);
    }
}

測試

1. 新增圖書

POST http://localhost:8888/books
Content-Type: application/json

{
  "title": "JavaStudy777",
  "author": "冷風(fēng)扇",
  "isbn": "1234567890"
}

2. 獲取圖書信息

# GET請求
GET http://localhost:8888/books/JavaStudy777

3. 刪除圖書

# POST請求
POST http://localhost:8888/books/delete/JavaStudy777

image.png

到此這篇關(guān)于Spring Cache優(yōu)化數(shù)據(jù)庫訪問的項目實踐的文章就介紹到這了,更多相關(guān)Spring Cache數(shù)據(jù)庫訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java JConsole遠(yuǎn)程連接配置案例詳解

    Java JConsole遠(yuǎn)程連接配置案例詳解

    這篇文章主要介紹了Java JConsole遠(yuǎn)程連接配置案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • zookeeper集群搭建超詳細(xì)過程

    zookeeper集群搭建超詳細(xì)過程

    這篇文章主要介紹了zookeeper集群搭建超詳細(xì)過程,本文對zookeeper集群測試通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • mybatis模糊查詢之bind標(biāo)簽和concat函數(shù)用法詳解

    mybatis模糊查詢之bind標(biāo)簽和concat函數(shù)用法詳解

    大家都知道bind 標(biāo)簽可以使用 OGNL 表達(dá)式創(chuàng)建一個變量井將其綁定到上下文中,接下來通過本文給大家介紹了mybatis模糊查詢——bind標(biāo)簽和concat函數(shù)用法,需要的朋友可以參考下
    2022-08-08
  • 如何使用cmd命令行窗口運行java文件

    如何使用cmd命令行窗口運行java文件

    多年以來一直使用的是IDE來寫java項目,導(dǎo)致很多的最基礎(chǔ)的東西都漸漸模糊了,最近遇到一個問題就是如果命令行來運行一個java項目,這里總結(jié)下,這篇文章主要給大家介紹了關(guān)于如何使用cmd命令行窗口運行java文件的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • 一文詳解Java中多進(jìn)程與多線程處理

    一文詳解Java中多進(jìn)程與多線程處理

    在Java編程中,多進(jìn)程和多線程是兩種常見的并發(fā)編程技術(shù),用于提高程序的執(zhí)行效率和響應(yīng)速度,本文將為大家簡單介紹一下多進(jìn)程與多線程處理的相關(guān)知識,希望對大家有所幫助
    2025-01-01
  • SpringBoot整合LDAP的流程分析

    SpringBoot整合LDAP的流程分析

    這篇文章主要介紹了SpringBoot整合LDAP的流程分析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05
  • SpringBoot中各種Controller的寫法

    SpringBoot中各種Controller的寫法

    這篇文章主要介紹了SpringBoot中各種Controller的寫法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Mybatis-Plus使用ID_WORKER生成主鍵id重復(fù)的解決方法

    Mybatis-Plus使用ID_WORKER生成主鍵id重復(fù)的解決方法

    本文主要介紹了Mybatis-Plus使用ID_WORKER生成主鍵id重復(fù)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java實現(xiàn)一行一行讀取文本的多種方法詳解

    Java實現(xiàn)一行一行讀取文本的多種方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)一行一行讀取文本的多種方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以了解下
    2025-10-10
  • 解決RedisTemplate調(diào)用increment報錯問題

    解決RedisTemplate調(diào)用increment報錯問題

    這篇文章主要介紹了解決RedisTemplate調(diào)用increment報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11

最新評論

古交市| 平原县| 永吉县| 秭归县| 苍溪县| 香格里拉县| 运城市| 靖宇县| 盘锦市| 中阳县| 西盟| 阜新市| 台安县| 唐海县| 左贡县| 绥滨县| 泽库县| 晋宁县| 禹城市| 台安县| 德昌县| 即墨市| 盘山县| 都匀市| 京山县| 台中县| 广南县| 永丰县| 定襄县| 蓝山县| 景东| 莎车县| 绵阳市| 杭锦旗| 宜州市| 沙田区| 南溪县| 昌江| 浮梁县| 名山县| 新源县|