Spring Cache優(yōu)化數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)的項(xiàng)目實(shí)踐
在這篇博客中,我們將學(xué)習(xí)如何使用Spring Cache來(lái)優(yōu)化數(shù)據(jù)庫(kù)訪(fǎng)問(wèn),提高系統(tǒng)性能。我們將創(chuàng)建一個(gè)簡(jiǎn)單的圖書(shū)管理應(yīng)用作為示例,并演示如何通過(guò)緩存減少對(duì)數(shù)據(jù)庫(kù)的頻繁查詢(xún)。
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)目依賴(lài)
我們使用了Spring Boot和Spring Data JPA來(lái)簡(jiǎn)化項(xiàng)目配置。以下是主要的Maven依賴(lài):
<!-- 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í)體類(lèi)
定義一個(gè)簡(jiǎn)單的實(shí)體類(lèi)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ù)訪(fǎng)問(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ù)訪(fǎng)問(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)用程序類(lèi)
在主應(yīng)用程序類(lèi) 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

到此這篇關(guān)于Spring Cache優(yōu)化數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)Spring Cache數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kotlin基礎(chǔ)教程之伴生對(duì)象,getter,setter,內(nèi)部,局部,匿名類(lèi),可變參數(shù)
這篇文章主要介紹了Kotlin基礎(chǔ)教程之伴生對(duì)象,getter,setter,內(nèi)部,局部,匿名類(lèi),可變參數(shù)的相關(guān)資料,需要的朋友可以參考下2017-05-05
解決rocketmq-client查詢(xún)手動(dòng)發(fā)送消息異常問(wèn)題
這篇文章主要介紹了解決rocketmq-client查詢(xún)手動(dòng)發(fā)送消息異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Java與Android使用監(jiān)聽(tīng)者模式示例
這篇文章主要為大家介紹了Java與Android使用監(jiān)聽(tīng)者模式示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
springboot?@PostConstruct無(wú)效的解決
這篇文章主要介紹了springboot?@PostConstruct無(wú)效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Mybatis Plus條件構(gòu)造器ConditionConstructor用法實(shí)例解析
這篇文章主要介紹了Mybatis Plus條件構(gòu)造器ConditionConstructor用法實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java擴(kuò)展庫(kù)RxJava的基本結(jié)構(gòu)與適用場(chǎng)景小結(jié)
RxJava(GitHub: https://github.com/ReactiveX/RxJava)能夠幫助Java進(jìn)行異步與事務(wù)驅(qū)動(dòng)的程序編寫(xiě),這里我們來(lái)作一個(gè)Java擴(kuò)展庫(kù)RxJava的基本結(jié)構(gòu)與適用場(chǎng)景小結(jié),剛接觸RxJava的同學(xué)不妨看一下^^2016-06-06
Java實(shí)現(xiàn)PDF導(dǎo)出功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)PDF導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-09-09
10個(gè)Java解決內(nèi)存溢出OOM的方法詳解
在Java開(kāi)發(fā)過(guò)程中,有效的內(nèi)存管理是保證應(yīng)用程序穩(wěn)定性和性能的關(guān)鍵,不正確的內(nèi)存使用可能導(dǎo)致內(nèi)存泄露甚至是致命的OutOfMemoryError(OOM),下面我們就來(lái)學(xué)習(xí)一下有哪些解決辦法吧2024-01-01
使用lombok注解導(dǎo)致mybatis-plus TypeHandler失效的解決
這篇文章主要介紹了使用lombok注解導(dǎo)致mybatis-plus TypeHandler失效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

