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

 更新时间:2018年11月07日 09:46:21   作者:小崔的笔记本  
这篇文章主要给大家介绍了关于Spring Boot基础学习之Mybatis操作中使用Redis做缓存的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起看看吧

前言

这篇博客学习下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中。

 然后通过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对象.

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

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

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

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

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • Java编程在ICPC快速IO实现源码

    Java编程在ICPC快速IO实现源码

    这篇文章主要介绍了Java Fast IO in ICPC实现源码,具有一定参考价值,需要的朋友可以了解下。
    2017-09-09
  • Maven项目web多图片上传及格式验证的实现

    Maven项目web多图片上传及格式验证的实现

    本文主要介绍了Maven项目web多图片上传及格式验证的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Java Lambda List转Map代码实例

    Java Lambda List转Map代码实例

    这篇文章主要介绍了Java Lambda List转Map代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java并发编程之详解ConcurrentHashMap类

    Java并发编程之详解ConcurrentHashMap类

    在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口、ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue、BlockingDeque接口,本文为系列文章第八篇.需要的朋友可以参考下
    2021-06-06
  • SpringBoot集成Swagger2生成接口文档的方法示例

    SpringBoot集成Swagger2生成接口文档的方法示例

    我们提供Restful接口的时候,API文档是尤为的重要,它承载着对接口的定义,描述等,本文主要介绍了SpringBoot集成Swagger2生成接口文档的方法示例,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • RxJava2.x+ReTrofit2.x多线程下载文件的示例代码

    RxJava2.x+ReTrofit2.x多线程下载文件的示例代码

    本篇文章主要介绍了RxJava2.x+ReTrofit2.x多线程下载文件的示例代码,具有一定的参考价值,有兴趣的可以了解一下
    2017-09-09
  • 10道springboot常见面试题

    10道springboot常见面试题

    这篇文章主要为大家详细介绍了10道springboot常见面试题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • java实现读取txt文件中的内容

    java实现读取txt文件中的内容

    本文通过一个具体的例子向大家展示了如何使用java实现读取TXT文件里的内容的方法以及思路,有需要的小伙伴可以参考下
    2016-03-03
  • 理解Java中的静态绑定和动态绑定

    理解Java中的静态绑定和动态绑定

    这篇文章主要帮助大家理解Java中的静态绑定和动态绑定,在Java中存在两种绑定方式,一种为静态绑定,另一种就是动态绑定,亦称为后期绑定,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • springboot+vue实现验证码功能

    springboot+vue实现验证码功能

    这篇文章主要为大家详细介绍了springboot+vue实现验证码功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论