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

Spring Cache優(yōu)化數(shù)據(jù)庫(kù)訪問(wèn)的項(xiàng)目實(shí)踐

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

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

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

首先,我們看一下項(xiàng)目的基本結(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. 項(xiàng)目依賴

我們使用了Spring Boot和Spring Data JPA來(lái)簡(jiǎn)化項(xiàng)目配置。以下是主要的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ù)庫(kù)配置

配置MySQL數(shù)據(jù)庫(kù)連接信息和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. 實(shí)體類

定義一個(gè)簡(jiǎn)單的實(shí)體類Book,用于表示圖書(shū)信息:

@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ù)訪問(wèn)層

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

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

6. 服務(wù)層

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

@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 處理圖書(shū)的獲取、保存和刪除操作:

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

測(cè)試

1. 新增圖書(shū)

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

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

2. 獲取圖書(shū)信息

# GET請(qǐng)求
GET http://localhost:8888/books/JavaStudy777

3. 刪除圖書(shū)

# POST請(qǐng)求
POST http://localhost:8888/books/delete/JavaStudy777

image.png

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

相關(guān)文章

最新評(píng)論