软件编程
位置:首页>> 软件编程>> java编程>> Spring Boot基础学习之Mybatis操作中使用Redis做缓存详解

Spring Boot基础学习之Mybatis操作中使用Redis做缓存详解

作者:小崔的笔记本  发布时间:2023-01-19 16:35:55 

标签:mybatis,redis,缓存

前言

这篇博客学习下Mybatis操作中使用Redis做缓存。这里其实主要学习几个注解:@CachePut、@Cacheable、@CacheEvict、@CacheConfig。

下面话不多说了,来一起看看详细的介绍吧

一、基础知识

@Cacheable

@Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存

参数解释example
value缓存的名称,在 spring 配置文件中定义,必须指定至少一个例如:
     @Cacheable(value=”mycache”)
     @Cacheable(value={”cache1”,”cache2”}
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合@Cacheable(value=”testcache”,key=”#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

 @CachePut

@CachePut 的作用 主要针对方法配置,能够根据方法的返回值对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用,在其他地方写的是根据方法的请求参数对其结果进行缓存,实际是按方法返回值进行缓存的,这里我就遇到了一个坑,我开始的时候是在Mybatis的Mapper层进行缓存的,如下面的代码。但是缓存到Redis的是Null值,今天看了一博友的博客,交流了一下,才知道它缓存的是方法的返回值,如果把下面update的返回值该为int,在redis中保存的是int类型,报的错误是int无法转换成User对象。


@CachePut(value="user",key = "#p0.id")
@Update({"UPDATE user SET name=#{name},age=#{age} WHERE id =#{id}"})
void update(User user);

参数解释example
value缓存的名称,在 spring 配置文件中定义,必须指定至少一个@CachePut(value=”my cache”)
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合@CachePut(value=”testcache”,key=”#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@CachePut(value=”testcache”,condition=”#userName.length()>2”)

 @CachEvict

 @CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空

参数解释example
value缓存的名称,在 spring 配置文件中定义,必须指定至少一个@CacheEvict(value=”my cache”)
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合@CacheEvict(value=”testcache”,key=”#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
allEntries是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存@CachEvict(value=”testcache”,allEntries=true)
beforeInvocation是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存@CachEvict(value=”testcache”,beforeInvocation=true)

@CacheConfig

所有的@Cacheable()里面都有一个value=“xxx”的属性,这显然如果方法多了,写起来也是挺累的,如果可以一次性声明完 那就省事了,有了@CacheConfig这个配置,@CacheConfig is a class-level annotation that allows to share the cache names,如果你在你的方法写别的名字,那么依然以方法的名字为准。

二、实例

还是在上一博客demo的基础上进行修改,原本是在Mybatis的Mapper层上增加cache注解,但由于update返回值为void,所以这里又增加了一services层,mapper层算是DAO层。这里使用了@CacheConfig注解指定类级别的value属性,如果在方法上定义就以方法为主,就近原则。


package com.example.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.example.model.User;
import com.example.write.mapper.WriteUserMapper;

@Service
@CacheConfig(cacheNames="user")
public class UserServices {

@Autowired
private WriteUserMapper writeUserMapper;

public List<User> getAll()
{
 return writeUserMapper.getAll();
}

@Cacheable(key = "#p0")
public User getOne(String id)
{
 return writeUserMapper.getOne(id);
}

public void insert(User user)
{
 writeUserMapper.insert(user);
}

@CachePut(value="user",key = "#p0.id")
public User update(User user)
{
 writeUserMapper.update(user);
 return user;
}

@CacheEvict(value="user",key ="#p0",allEntries=true)
public void delete(String id)
{
 writeUserMapper.delete(id);
}

}

UserController


package com.example.demo;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.example.model.User;
import com.example.model.UserSexEnum;
import com.example.read.mapper.ReadUserMapper;
import com.example.services.UserServices;
import com.example.write.mapper.WriteUserMapper;

import io.lettuce.core.dynamic.annotation.Param;

@Controller
@RequestMapping("/user")
public class UserController {

@Autowired
private WriteUserMapper userMapperWrite;

@Autowired
private ReadUserMapper userMapperRead;

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Autowired
private RedisTemplate<String, Serializable> redisCacheTemplate;

@Autowired
private UserServices userServices;

@RequestMapping(value = "/alluser.do",method = RequestMethod.GET)
public String getallusers(Model model) {
 List<User> users=userServices.getAll();
 model.addAttribute("users", users);
//  stringRedisTemplate.opsForValue().set("keytest", "cuiyw");
//  final String keytest = stringRedisTemplate.opsForValue().get("keytest");
//  model.addAttribute("keytest", keytest);
//  String key = "1857XXXX040";
//  redisCacheTemplate.opsForValue().set(key, new User(key, "cuiyw", 18, UserSexEnum.MAN));
//  // TODO 对应 String(字符串)
//  final User user = (User) redisCacheTemplate.opsForValue().get(key);
//  model.addAttribute("user", user);
 return "userlist";
}
@RequestMapping(value = "/insert.do",method = RequestMethod.GET)
public String adduser(Model model) {
 User user=new User();
 user.setName("cuiyw");
 user.setAge(27);
 userServices.insert(user);
//  List<User> users=userMapperWrite.getAll();
//  model.addAttribute("users", users);
 return "forward:/user/alluser.do";

}
@RequestMapping(value = "/getuserbyid.do/{id}",method = RequestMethod.GET)
public ModelAndView GetUserById(@PathVariable("id") String id) {
 System.out.println(id);
 User user=userServices.getOne(id);
 System.out.println(user.toString());
 ModelAndView modelAndView = new ModelAndView("userlist");

modelAndView.addObject("user", user);
 return modelAndView;

}
@RequestMapping(value = "/deleteuserbyid.do/{id}",method = RequestMethod.GET)
public String DeleteUserById(@PathVariable("id") String id) {
 userServices.delete(id);
 return "forward:/user/alluser.do";

}
@RequestMapping(value = "/updateuserbyid.do/{id}",method = RequestMethod.GET)
public String UpdateUserByid(@PathVariable("id") String id) {
 User user=userServices.getOne(id);
 System.out.println(user.toString());
 user.setAge(28);
 System.out.println(user.toString());
 userServices.update(user);
  System.out.println(user.toString());
 return "forward:/user/alluser.do";

}
}

这里先输入http://localhost:8080/user/getuserbyid.do/17通过getOne()方法在redis中缓存一个user。通过redis-cli可以看到user::17已在redis中。

Spring Boot基础学习之Mybatis操作中使用Redis做缓存详解

 然后通过update()方法输入http://localhost:8080/user/updateuserbyid.do/17修改user,此时年龄改为了28,数据库的值也会变了。然后多次使用http://localhost:8080/user/updateuserbyid.do/17这个url刷新浏览器,此时是不会报错的,如果是在mapper中使用@Cacheput时由于保存的是null就会导致报错。

最后通过delete()方法输入http://localhost:8080/user/deleteuserbyid.do/17删除redis和数据库中的user对象.

Spring Boot基础学习之Mybatis操作中使用Redis做缓存详解

至此,基本把这4个注解大致了解了一下,这里还有一个地方需要补充,就是如果按照上面运行还是不行的,它依然找不到UserServices,在UserController中找不到这个类,还需要在main方法上面@ComponentScan注解加上扫描com.example.services。


@ComponentScan(basePackages={"com.example.config","com.example.demo","com.example.services"})

最后来一碗鸡汤,记录下今天看抖音听到的一句话,还挺有道理。

为什么大多数人宁愿吃生活的苦,而不愿意吃学习的苦?因为学习的苦需要自己主动去吃,而生活的苦你躺着它就来了。

来源:https://www.cnblogs.com/5ishare/p/9439381.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com