spinrgboot缓存之缓存抽象(缓存初步体验)
时间:2022-07-23
本文章向大家介绍spinrgboot缓存之缓存抽象(缓存初步体验),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
几个重要接口以及注解:
相关注解中的参数:
参数中的可用spel表达式:
首先搭建好用于测试缓存的环境:
1、新建一个springboot项目,选择web、mysql、mybatis、cache这些模块。
2、建立如下目录及文件
Department.java
package com.gong.springbootcache.bean;
public class Department {
private Integer id;
private String departmentName;
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Department(Integer id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName + "]";
}
}
Employee.java
package com.gong.springbootcache.bean;
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender; //性别 1男 0女
private Integer dId;
public Employee() {
super();
}
public Employee(Integer id, String lastName, String email, Integer gender, Integer dId) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.dId = dId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getdId() {
return dId;
}
public void setdId(Integer dId) {
this.dId = dId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", dId="
+ dId + "]";
}
}
EmployeeMapper.java
package com.gong.springbootcache.mapper;
import com.gong.springbootcache.bean.Employee;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
//@Mapper或者@MapperScan将接口扫描装配到容器中
public interface EmployeeMapper {
@Select("select * from employee where id=#{id}")
public Employee getEmpById(Integer id);
@Update("update employee set lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} where id=#{id}")
public void updateEmp(Employee employee);
@Delete("delete from employee where id=#{id}")
public void deleteEmp(Integer id);
@Insert("insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId}) ")
public void insertEmp(Employee employee);
}
DepartmentMapper.java
package com.gong.springbootcache.mapper;
import com.gong.springbootcache.bean.Department;
import org.apache.ibatis.annotations.*;
//@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(department_name) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set department_name=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
EmployeeController.java
package com.gong.springbootcache.controller;
import com.gong.springbootcache.bean.Employee;
import com.gong.springbootcache.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@ResponseBody
@RequestMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
Employee emp = employeeService.getEmp(id);
return emp;
}
}
数据库文件:spring_cache.sql
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`gender` int(2) DEFAULT NULL,
`d_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/spring_cache
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#开启驼峰命名法
mybatis.configuration.map-underscore-to-camel-case=true
#打印sql日志
logging.level.com.gong.springbootcache.mapper=debug
启动入口:
package com.gong.springbootcache;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@MapperScan("com.gong.springbootcache.mapper")
@SpringBootApplication
@EnableCaching
public class SpringbootCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCacheApplication.class, args);
}
}
此时,我们启动服务器:发送请求(自己先往数据库中可以添点数据)
在控制台可以看到:
接下来我们就可以测试缓存了。
3、进行测试了
我们给EmployeeController.java中改成:
//value:指定缓存的名字,每个缓存组件有一个唯一的名字。缓存组件由CacheManager进行管理。
//key:缓存数据时用到的key,默认使用方法参数的值,1-方法返回值
//#id也可这么表示:#root.args[0](第一个参数)
//keyGenerator:指定生成缓存的组件id,使用key或keyGenerator其中一个即可
//cacheManager,cacheResolver:指定交由哪个缓存管理器,使用其中一个参数即可
//condition:指定符合条件时才进行缓存
//unless:当unless指定的条件为true,方法的返回值就不会被缓存
//sync:是否使用异步模式
@Cacheable(value = "emp")
@ResponseBody
@RequestMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
Employee emp = employeeService.getEmp(id);
return emp;
}
当我们第一次发送请求时,结果:
当我们再次发送该请求时,控制台就不会在sql调试了,因为已经存入到缓存中,不必再发送sql来查询结果。
- null或空值的判断处理
- 前端知识小结
- 除了比特币,区块链技术能做的还有很多
- Progressive Web Apps入门
- IDEA入门级使用教程
- 掌握Docker命令-Docker for Web Developers(4)
- InfoPath中repeating section中赋值操作
- 百布(baibu.la)完成1.65亿B+轮融资
- 八大排序算法总结与java实现
- Angular企业级开发(5)-项目框架搭建
- 如何让nginx显示文件夹目录
- Facebook Graph API(2)--读取数据之picture
- 使用Dockerfile构建镜像-Docker for Web Developers(5)
- 2018年程序员的出路有哪些
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 干货 | 从0到1,搭建一个体系完善的前端React组件库
- LeetCode 01两数之和&02两数相加
- 给GitHub "彩蛋" readme 生成自定义统计信息
- Android |《看完不忘系列》之okhttp
- pt-osc改表过程中的中文乱码问题
- Hive Query生命周期 —— 钩子(Hook)函数篇
- python快速排序
- 扩展 Microsoft.Owin.Security
- Angular 2 版本的 ng-bootstrap 初体验
- 揭开MySQL“锁”的神秘面纱
- 从0到1开发测试平台(九)后端对接口response的封装
- 在 Mac OS X 上安装和配置 Wine
- 性能测试之nginx访问日志分析
- Pytest之断言
- R语言单因素方差分析简单小例子