SpringBoot使用缓存Cache以及集成Redis(Docker安装)

1、SpringBoot与缓存

SpringBoot使用Spring-Cache来操作缓存,常用的有@Cacheable、@CacheEvict、@CachePut、@EnableCache

1.1、引入cache相关的依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1.2、在主程序类前加@EnableCaching开启缓存
@EnableCaching
@SpringBootApplication
public class Springboot02RedisApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot02RedisApplication.class, args);
	}

}
1.3、连接MySQL数据库进行前期工作准备
#连接mysql数据库
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
1.4、创建实体类
  • Employee 类
public class Employee implements Serializable {

	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 + "]";
	}
}
1.5、建立相应的mapper文件查询数据库
@Mapper
public interface EmployeeMapper {

	//通过id查询雇员的信息
	@Select("SELECT * FROM employee WHERE id = #{id}")
	public Employee getById(Integer id);
	
	//更新员工的信息
	@Update("UPDATE employee SET lastname = #{lastName}," +
			"email = #{email}," +
			"gender = #{gender}," +
			"d_id = #{dId} WHERE id = #{id}")
	public void updateEmployee(Employee employee);
}
1.6、创建相应的service类并将方法返回的结果存储到缓存
@Service
public class EmployeeService {

	@Autowired
	EmployeeMapper employeeMapper;

	@Cacheable(cacheNames = "emp") //将方法返回的结果存储到名为emp的缓存中
	public Employee getById(Integer id){
		System.out.println("查询了" + id + "员工");
		Employee employee = employeeMapper.getById(id);
		return employee;
	}
	//方法被调用,结果也被缓存
	@CachePut(cacheNames = "emp")
	public Employee updateEmp(Employee employee){
		employeeMapper.updateEmployee(employee);
		return employee;
	}
}
1.7、创建对应的controller类
@RestController
public class EmployeeController {

	@Autowired
	EmployeeService employeeService;
	
	//@PathVariable("id")注解从路径中取值
	
	@GetMapping("/search/{id}")
	public Employee getById(@PathVariable("id") Integer id){
		Employee employee = employeeService.getById(id);
		return employee;
	}
	
	@GetMapping("/emp")
	public Employee updateEmployee(Employee employee){
		Employee emp = employeeService.updateEmp(employee);
		return emp;
	}
}
1.8、查看缓存是否生效
  • 通过日志可以看出,进行第一遍查询时出现了"查询了1员工",以后的查询控制台不再打印,即缓存已经生效。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值