SpringBoot與Spring中數據緩存Cache超詳細講解
一、Spring緩存支持
Spring框架定義了org.springframework.cache CacheManager和org.springframework.cache.Cache接口來統一不同的緩存技術
CacheManager常用方法如下
1、@Cacheable
該注解可以標記在一個方法上,也可以標記在一個類上,當標記在一個方法上時表示該方法是支持緩存的,當標記在一個類上時則表示該類所有的方法都是支持緩存的。對于一個支持緩存的方法,在方法執(zhí)行前,Spring先檢查緩存中是否存在方法返回的數據,如果存在則直接返回緩存數據,如果不存在,則調用方法并將方法返回值存入緩存
2、@CacheEvict
該注解用來標注在需要清楚緩存元素的方法或類上,當標記在一個類上時,表示其中所有方法的執(zhí)行都會觸發(fā)緩存的清除操作
3、@CachePut
該注解也可以聲明一個方法支持緩存功能
4、Caching
該注解可以在一個方法或類上同時指定多個Spring Cache相關的注解
5、CacheConfig
該注解作用在類上可以設置當前緩存的一些公共設置
二、Spring Boot緩存支持
1:創(chuàng)建基于spring-voot-starter-cache 和spring-boot-starter-data-jpa依賴的Spring BootWeb應用
2:配置application.properties文件 代碼如下
server.servlet.context-path=/ch6_10
###
##數據源信息配置
###
#數據庫地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#數據庫用戶名
spring.datasource.username=root
#數據庫密碼
spring.datasource.password=root
#數據庫驅動
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
####
#JPA持久化配置
####
#指定數據庫類型
spring.jpa.database=MYSQL
#指定是否在日志中顯示SQL語句
spring.jpa.show-sql=true
#指定自動創(chuàng)建、更新數據庫表等配置,update表示如果數據庫中存在持久化類對應的表就不創(chuàng)建,不存在就創(chuàng)建對應的表
spring.jpa.hibernate.ddl-auto=update
#讓控制器輸出的JSON字符串格式更美觀
spring.jackson.serialization.indent-output=true
3:修改pom.xml文件 添加mysql依賴
<?xml version="1.0" encoding="UTF-8"?> -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> -<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ch</groupId> <artifactId>ch6_10</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ch6_10</name> <description>Demo project for Spring Boot</description> -<properties> <java.version>11</java.version> </properties> -<dependencies> -<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> -<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> -<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加MySQL依賴 --> -<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> <!-- MySQL8.x時,請使用8.x的連接器 --> </dependency> -<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> -<build> -<plugins> -<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4:創(chuàng)建持久化實體類
代碼如下
package com.ch.ch6_10.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @Entity @Table(name = "student_table") @JsonIgnoreProperties(value = {"hibernateLazyInitializer"}) public class Student implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id;//主鍵 private String sno; private String sname; private String ssex; public Student() { super(); } public Student(int id, String sno, String sname, String ssex) { super(); this.id = id; this.sno = sno; this.sname = sname; this.ssex = ssex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } }
5:創(chuàng)建數據訪問接口
package com.ch.ch6_10.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.ch.ch6_10.entity.Student; public interface StudentRepository extends JpaRepository<Student, Integer>{ }
6:創(chuàng)建業(yè)務層 包括一個接口和一個實現類
接口代碼如下
package com.ch.ch6_10.service; import com.ch.ch6_10.entity.Student; public interface StudentService { public Student saveStudent(Student student); public void deleteCache(Student student); public Student selectOneStudent(Integer id); }
實現類代碼如下
package com.ch.ch6_10.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.ch.ch6_10.entity.Student; import com.ch.ch6_10.repository.StudentRepository; @Service public class StudentServiceImpl implements StudentService{ @Autowired private StudentRepository studentRepository; @Override @CachePut(value = "student", key="#student.id") public Student saveStudent(Student student) { Student s = studentRepository.save(student); System.out.println("為key=" + student.getId() + "數據做了緩存"); return s; } @Override @CacheEvict(value = "student", key="#student.id") public void deleteCache(Student student) { System.out.println("刪除了key=" + student.getId() + "的數據緩存"); } @Override @Cacheable(value = "student") public Student selectOneStudent(Integer id) { Student s = studentRepository.getOne(id); System.out.println("為key=" + id + "數據做了緩存"); return s; } }
7:創(chuàng)建控制器層
package com.ch.ch6_10.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ch.ch6_10.entity.Student; import com.ch.ch6_10.service.StudentService; @RestController public class TestCacheController { @Autowired private StudentService studentService; @RequestMapping("/savePut") public Student save(Student student) { return studentService.saveStudent(student); } @RequestMapping("/selectAble") public Student select(Integer id) { return studentService.selectOneStudent(id); } @RequestMapping("/deleteEvict") public String deleteCache(Student student) { studentService.deleteCache(student); return "ok"; } }
8:在主類中開啟緩存支持
package com.ch.ch6_10; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @EnableCaching @SpringBootApplication public class Ch610Application { public static void main(String[] args) { SpringApplication.run(Ch610Application.class, args); } }
到此這篇關于SpringBoot與Spring中數據緩存Cache超詳細講解的文章就介紹到這了,更多相關SpringBoot數據緩存Cache內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于jd-gui啟動報This?program?requires?Java?1.8+的錯誤問題及解決方法
最近,在Mac使用上JD-GUI啟動時總是報錯,接下來通過本文給大家介紹關于jd-gui啟動報this?program?requires?Java?1.8+的錯誤問題及解決方法,需要的朋友可以參考下2022-05-05Java各種排序算法匯總(冒泡,選擇,歸并,希爾及堆排序等)
這篇文章主要介紹了Java各種排序算法,以大量實例形式匯總分析了Java常用的各種排序算法,包括冒泡排序、快速排序、堆排序、插入排序、希爾排序、選擇排序、歸并排序等,需要的朋友可以參考下2015-11-11