日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
怎么在SpringBoot中設置使用緩存

本篇文章為大家展示了怎么在Spring Boot中設置使用緩存,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

公司主營業(yè)務:網站設計制作、網站設計、移動網站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出郟縣免費做網站回饋大家。

幾個緩存注解的作用:

@Cacheable:將方法的返回結果根據key指定的鍵保存在緩存中,以后要獲取相同的數(shù)據直接從緩存中共獲取

  • cacheNames/value:指定Cache組件名稱

  • key:指定緩存時使用的key,默認使用方法參數(shù)值,可以使用#a0、#p0、#參數(shù)名等,支持SpEL表達式,root可省略

  • keyGenerator:指定key的生成器的組件id,如自定義的KeyGenerator

  • cacheManager:指定緩存管理器

  • cacheResolver:指定緩存解析器

  • condition:指定在哪種條件下緩存,如condition = “#id>=1”在參數(shù)>=1時緩存

  • unless:指定該條件為真時不緩存

  • sync:指定是否使用異步模式

@CachePut:不管緩存中是否有需要的數(shù)據,都會執(zhí)行該注解標注的方法,并將結果更新到緩存,屬性見上

@CacheEvit:執(zhí)行方法后,清除key指定的緩存

  • allEntries:默認為false,值為true,刪除所有緩存

  • beforeInvocation:默認為false,值為true,在方法調用之前清除緩存

@CacheConfig:定義一些通用或公共的規(guī)則,如cacheNames、keyGenerator等

可使用的SpEL表達式:

怎么在Spring Boot中設置使用緩存

使用緩存的步驟:

(1)創(chuàng)建一個Spring Boot應用,勾選Cache、Web、MySQL、Mybatis模塊,在主程序類上添加注解,開啟基于注解的緩存

@MapperScan(basePackages = "com.youngpain.cache.mapper")
@SpringBootApplication
@EnableCaching

(2)創(chuàng)建JavaBean,和數(shù)據庫中的表對應,并配置數(shù)據源

spring:
 datasource:
  url: jdbc:mysql://localhost:3306/mybatis_database
  username: root
  password: 1741248769
  driver-class-name: com.mysql.jdbc.Driver
 redis:
  host: 39.108.114.57
#開啟駝峰命名法
mybatis:
 configuration:
  map-underscore-to-camel-case: true
logging:
 level:
  com.youngpain.cache.mapper: debug

(3)創(chuàng)建mapper接口進行增刪改查操作

/**
 * 部門表的增刪改查操作
 */
public interface DepartmentMapper {
  @Insert("insert into department(id,depart_name,depart_build) values(#{id},#{depart_name},#{depart_build})")
  void insertDepartment(Department department);
  @Delete("delete from department where id=#{id}")
  void deleteDepartment(Integer id);
  @Update("update department set depart_name=#{departName},depart_build=#{departBuild} where id=#{id}")
  void updateDepartment(Department department);
  @Select("select * from department where id=#{id}")
  Department getDepartmentById(Integer id);
}

(4)創(chuàng)建service

@Service
@CacheConfig(cacheNames = {"departs"})
public class DepartmentService {
  @Autowired
  DepartmentMapper departmentMapper;
  @Cacheable(key = "#a0.id")
  public void insertDepartment(Department department) {
    departmentMapper.insertDepartment(department);
  }
  @CacheEvict(key = "#p0")
  public void deleteDepartment(Integer id) {
    departmentMapper.deleteDepartment(id);
  }
  @CachePut(key = "#a0.id")
  public Department updateDepartment(Department department) {
    departmentMapper.updateDepartment(department);
    return department;
  }
  @Cacheable(key = "#id", condition = "#p0>=1")
  public Department getDepartmentById(Integer id) {
    return departmentMapper.getDepartmentById(id);
  }
}

(5)創(chuàng)建controller

@Controller
public class DepartmentController {
  @Autowired
  DepartmentService departmentService;
  @GetMapping("/index")
  public String index() {
    return "index";
  }
  @GetMapping("/deleteDepart/{id}")
  public String deleteDepart(@PathVariable("id") Integer id, Model model) {
    model.addAttribute("condition", "delete");
    Department delete = departmentService.getDepartmentById(id);
    model.addAttribute("department", delete);
    departmentService.deleteDepartment(id);
    return "success";
  }
  @PostMapping("/updateDepart")
  public String updateDepart(Department department, Model model) {
    model.addAttribute("condition", "update");
    Department update = departmentService.updateDepartment(department);
    model.addAttribute("department", update);
    return "success";
  }
  @GetMapping("/getDepart/{id}")
  public String getDepartmentById(@PathVariable("id") Integer id, Model model) {
    model.addAttribute("condition", "delete");
    Department get = departmentService.getDepartmentById(id);
    model.addAttribute("department", get);
    return "success";
  }
}

(6)測試結果:

@Cacheable:第一次查詢數(shù)據,控制臺發(fā)出sql語句,之后再查詢直接從緩存中獲取
@CachePut:調用方法修改某個數(shù)據后,再次查詢該數(shù)據是從緩存中獲取的更新后的數(shù)據
@CacheEvict:調用該方法后,再次查詢某個數(shù)據需要重新發(fā)出sql語句查詢

springboot是什么

springboot一種全新的編程規(guī)范,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。

上述內容就是怎么在Spring Boot中設置使用緩存,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


名稱欄目:怎么在SpringBoot中設置使用緩存
文章分享:http://www.dlmjj.cn/article/gophgi.html