SpringBoot(五) springboot+EhCache缓存

本文介绍了如何在SpringBoot项目中使用EhCache进行缓存操作。通过配置ehcache.xml,启用@EnableCaching注解,以及在UserMapper.java中使用@CacheConfig和@Cacheable()注解,实现了数据的缓存。测试缓存功能时,删除数据库中的数据后,再次请求仍能获取到,证明缓存生效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、目录接口

二、实现代码

1、EhCache.java

package com.aeolusway;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching  //使缓存可用
@MapperScan("com.aeolusway.mapper")
public class EhCache {

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

2、UserController.java

package com.aeolusway.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aeolusway.mapper.UserMapper;
import com.aeolusway.model.User;

@RestController
public class UserController {
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private CacheManager cacheManager;
	@RequestMapping("/find")
	public User findUser(int id){
	
		return	userMapper.findUser(id);
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值