一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot設置并使用緩存的步驟

Spring Boot設置并使用緩存的步驟

2019-06-21 14:47Codenjoyes Java教程

今天小編就為大家分享一篇關于Spring Boot設置并使用緩存的步驟,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

幾個緩存注解的作用:

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

  • cacheNames/value:指定Cache組件名稱
  • key:指定緩存時使用的key,默認使用方法參數值,可以使用#a0、#p0、#參數名等,支持SpEL表達式,root可省略
  • keyGenerator:指定key的生成器的組件id,如自定義的KeyGenerator
  • cacheManager:指定緩存管理器
  • cacheResolver:指定緩存解析器
  • condition:指定在哪種條件下緩存,如condition = “#id>=1”在參數>=1時緩存
  • unless:指定該條件為真時不緩存
  • sync:指定是否使用異步模式

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

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

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

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

可使用的SpEL表達式:

Spring Boot設置并使用緩存的步驟

使用緩存的步驟:

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

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

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

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)創建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)創建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)創建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:第一次查詢數據,控制臺發出sql語句,之后再查詢直接從緩存中獲取
@CachePut:調用方法修改某個數據后,再次查詢該數據是從緩存中獲取的更新后的數據
@CacheEvict:調用該方法后,再次查詢某個數據需要重新發出sql語句查詢

ps:之前只是用markdown記筆記,今天第一次用markdown寫文章,寫起來好舒服啊QAQ

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲日日操 | 国产一区二区三区福利 | 日韩免费在线视频 | 天天久久综合网站 | 2019天天干夜夜操 | 日本免费久久久久久久网站 | 国产成+人+综合+亚洲欧美丁香花 | 色综合色狠狠天天综合色hd | 美女毛片老太婆bbb80岁 | 婷婷综合亚洲 | 韩国三级年轻小的胰子完整 | 青青草国产精品久久久久 | 国产一区二区免费在线 | 玩50岁四川熟女大白屁股直播 | 欧美成人中文字幕在线看 | 四虎海外影院 | 太深了 太粗h1v1 | 91在线精品国产丝袜超清 | 好 舒服 好 粗 好硬 好爽 | 99精品热线在线观看免费视频 | 亚洲视频在线观看不卡 | 麻豆网| 久久偷拍国2017的 | 91精品手机国产在线观 | 性姿势女人嗷嗷叫图片 | 欧美日韩成人在线视频 | 国产伦精品一区二区三区免 | 免费看美女被靠到爽 | 香蕉国产人午夜视频在线观看 | 嫩草蜜桃 | 国产成人在线影院 | 香蕉久久一区二区三区 | fuqer日本| 日本三级在线观看免费 | 丰满大屁股美女一级毛片 | brazzersvideo欧美最新 | 男女18一级大黄毛片免 | 99视频在线观看视频一区 | 久久99精品久久久久久园产越南 | chinese真实incest| 日产2021免费一二三四区 |