文章目录

  • 1.在Spring Boot中集成Redis
  • 2.配置Redis类
    • 关于Redis Template中序列化和反序列化配置的解释
  • 3.创建测试实体类
  • 4.实现实体和数据表的映射关系
  • 5.创建Redis缓存服务器
  • 6.完成增删改查测试API
  • 7.启动测试

1.在Spring Boot中集成Redis

1)添加依赖
pom.xml
添加的依赖包含Redis,Mysql,MyBatis,fastjson,junit

     <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>

2)配置application.properties

application.properties
配置mysql,redis服务器

spring.datasource.url=jdbc:mysql://127.0.0.1/book?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= truespring.datasource.initialization-mode=always
spring.datasource.schema=classpath:db/schema.sql# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0# 连接超时时间(毫秒)
spring.redis.timeout=5000

3)在入口类加上@EnableCaching注解,开启缓存支持

RedisDemoApplication.java

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class RedisDemoApplication {public static void main(String[] args) {SpringApplication.run(RedisDemoApplication.class, args);}}

4)建立下面的项目结构

2.配置Redis类

要想启动Spring缓存支持,需要创建一个CacheManager的Bean。

RedisConfig.java

package com.example.demo.config;import  com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import  org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.lang.reflect.Method;@Configurationpublic class RedisConfig extends  CachingConfigurerSupport{@Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object target, Method method, Object... params) {StringBuilder sb = new StringBuilder();sb.append(target.getClass().getName());sb.append(method.getName());for (Object obj : params) {sb.append(obj.toString());}return sb.toString();}};}@SuppressWarnings("rawtypes")@Bean//缓存管理器public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory);//设置缓存过期时间return cacheManager;}@Beanpublic RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {//配置连接工厂StringRedisTemplate template = new StringRedisTemplate(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis 的value值Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();//指定要序列化的域,field,get和set,以及修饰符范围om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);//om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);//指定序列化输入的类型,类必须是非final类om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);jackson2JsonRedisSerializer.setObjectMapper(om);//序列化配置为String格式template.setValueSerializer(new StringRedisSerializer());//template.setKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}}

关于Redis Template中序列化和反序列化配置的解释

关于数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)

序列化/发序列化 解释
JdkSerializationRedisSerializer POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。
StringRedisSerializer 适用于Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string
JacksonJsonRedisSerializer 提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。
OxmSerializer 提供了将javabean与xml之间的转换能力,目前可用的三方支持包括jaxb,apache-xmlbeans;redis存储的数据将是xml工具。

上面4种策略中:

JdkSerializationRedisSerializerStringRedisSerializer是最基础的策略,在设计时仍然不推荐直接使用后面两种,即JacksonJsonRedisSerializerOxmSerializer,因为无论是json还是xml,他们本身仍然是String。

如果数据需要被第三方工具解析,那么数据应该使用StringRedisSerializer而不是JdkSerializationRedisSerializer

如果数据格式必须为json或者xml,那么在编程级别,在redisTemplate配置中仍然使用StringRedisSerializer,在存储之前或者读取之后,使用“SerializationUtils”工具转换转换成json或者xml

3.创建测试实体类

User.java

package com.example.demo.model;import lombok.Data;import java.io.Serializable;@Data
public class User implements Serializable {private  String id;private  String name;private int age;
}

这里需要注意的是spring boot在启动时会自动创建user表
在项目的resources目录下新建db目录,并添加schema.sql文件,写入创建user表的sql语句
当然还需要配置数据库连接,并加上数据表初始化的配置(配置工作前面的文件中已经包含)
这里是schema.sql

DROP TABLE IF EXISTS user;
--IF object_id('user','U') is not NULL drop table 'user';
CREATE TABLE user(id int(11) NOT NULL AUTO_INCREMENT,name varchar(255) DEFAULT NULL,age int(11) DEFAULT NULL,PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

4.实现实体和数据表的映射关系

UserMapper.java

package com.example.demo.mapper;import com.example.demo.model.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import  org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import  org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;@Repository
@Mapper
public interface UserMapper {@Insert("insert into user(name,age) values(#{name},#{age})")int addUser(@Param("name")String name,@Param("age")String age);@Select("select * from user where id=#{id}")User findById(@Param("id")String id);@Update("update user set name=#{name},age=#{age} where id=#{id}")int updateById(User user);@Delete("delete from user where id=#{id}")void deleteById(@Param("id")String id);
}

5.创建Redis缓存服务器

创建Redis缓存服务器,即缓存在服务器层面
UserService.java

代码解释
@Cacheable:将查询结果缓存到Redis中
key="#p0":指定传入的第一个参数作为Redis的key
@CachePut:指定key,将更新的结果同步到Redis中
@CacheEvict:指定key,删除缓存数据
allEntries:方法调用后将立即清除缓存

package com.example.demo.service;import com.example.demo.model.User;
import com.example.demo.mapper.UserMapper;
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;//Redis缓存服务层
@Service
@CacheConfig(cacheNames = "users")
public class UserService {@AutowiredUserMapper userMapper;@Cacheable(key="#p0")public User selectUser(String id){System.out.println("select");return userMapper.findById(id);}@CachePut(key="#p0")public  void updateById(User user){System.out.println("uddate");userMapper.updateById(user);}//如果allEntries指定为true,则调用CacheEvict方法后将立即清空所有缓存@CacheEvict(key="#p0",allEntries = true)public void deleteById(String id){System.out.println("delete");userMapper.deleteById(id);}
}

6.完成增删改查测试API

RedisController.java

package com.example.demo.controller;
import com.example.demo.service.UserService;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user")
public class RedisController {@AutowiredUserService userService;@RequestMapping("/{id}")public User ForTest(@PathVariable String id){return userService.selectUser(id);}@RequestMapping( "/update/")public String update(User user){userService.updateById(user);return "success";}@RequestMapping( "/delete/{id}")public String delete (@PathVariable String id){userService.deleteById(id);return "delete success";}}

7.启动测试

1)启动Redis服务
由于没有设置环境变量,需要手动进入到Redis安装目录cd /d D:\Redis-x64-3.2.100,然后启动服务

redis-server.exe redis.windows.conf

在cmd窗口打开的情况下,运行项目

2)多次访问

http://localhost:8080/user/2

第一次时控制台会出现select信息,代表对数据库进行了查询操作。后面再访问则不会出现提示,表示没有对数据库执行操作,而是使用Redis中的缓存数据。

SpringBoot使用Redis和MyBatis完成缓存数据的增删改查相关推荐

  1. SpringBoot+MyBatis包含MyBatis Plus实现数据的增删改查(详细过程,适合新手小白)

    首先引入相应的依赖(创建SpringBoot项目这里就不再多说了) 修改pom.xml <dependency><groupId>org.projectlombok</g ...

  2. Redis概述_使用命令对redis的数据进行增删改查_Jedis连接redis进行数据操作_redis进行数据缓存案例

    学习目标 redis 概念 下载安装 命令操作 1. 数据结构 持久化操作 使用Java客户端操作redis Redis 前言(从百度上抄的, 看看了解一下, 懒得排版了) 1. 概念: redis是 ...

  3. SpringBoot+Mysql+MDUI实现数据的增删改查和列表操作及单,多文件上传实例

    SpringBoot+Mysql+MDUI实现数据的增删改查和列表操作及单,多文件上传实例 开源地址:https://gitee.com/jfkjrym/demo.git evan原创内容!evan原 ...

  4. mybatis从零基础到增删改查数据库

    本文是mybatis框架一个初步的入门总结,最全的最好的资料应该参考这个:http://mybatis.github.io/mybatis-3/zh/index.html 本文在Eclipse下搭建一 ...

  5. MyBatis的Mapper 代理的增删改查操作(三)

    沉迷于黑与白世界中的人,无论怎么挣扎,都逃不过被同化的命运.前世看见了什么,那么今世便是什么. 上一章简单介绍了MyBatis的命名空间方式的增删改查操作(二),如果没有看过,请观看上一章. 一. M ...

  6. mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数

    mybatis复习02,简单的增删改查 创建数据表 user_info 在项目中创建数据表对应的实体类 UserInfo.java 在pom.xml文件中引入依赖 创建核心配置文件mybatis-co ...

  7. Mybatis实现简单的数据库增删改查操作

    简介: MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Ma ...

  8. JavaWeb小项目(二)- 完成品牌数据的增删改查(JSP/MVC/三层架构综合应用)

    JavaWeb小项目(二)- 完成品牌数据的增删改查(JSP/MVC/三层架构综合应用) 文章目录 JavaWeb小项目(二)- 完成品牌数据的增删改查(JSP/MVC/三层架构综合应用) 环境搭建 ...

  9. Java操作Mongodb数据(增删改查聚合查询)

    文章目录 一.Java操作MongoDB 二.使用步骤 1.基础配置 2.实体类 3.MongoDB表数据 3.增删改查聚合查询 总结 一.Java操作MongoDB 上一篇文章介绍了,如何在本地使用 ...

最新文章

  1. 2007年上半年 网络工程师 上下午试卷【附带答案】
  2. 延展公司与兰石重装签订战略合作协议
  3. 打开了悬浮窗权限但是没有_给你的手机添加“樱花雨”特效,打开手机樱花就满屏飘落,漂亮!...
  4. 转载:【opencv入门教程之六】创建Trackbar图片对比度、亮度值调整
  5. 使用SAP云平台portal服务将SAP UI5应用部署到portal上
  6. Druid 配置及内置监控,Web页面查看监控内容 【我改】
  7. Keep将赴港上市?回应:不予置评
  8. 发布水晶报表时需要在服务器进行如下操作
  9. 23. 线程如何退出结束
  10. TwinCAT 3 首次运行报错4115
  11. python xlwt_python 中 xlwt 操作excel
  12. ai怎么做盒子效果图_ai怎么制作包装盒? Ai贴图工具制作包装盒的实例教程
  13. MAC chrome 谷歌浏览器全屏显示——隐藏菜单栏
  14. CTF Broadcast
  15. ld cannot find -lbz2
  16. 如何使用 哑节点(dummy node),高效解决问题
  17. vol.138 三周年特辑 · 那些无处安放的敏感词们
  18. 设计师:设计师知识储备之硬装部分/软装部分简介、家装材料知识(吊顶材料/门窗材料/五金材料/墙面材料/地面材料/胶粘材料/油漆材料/水电材料/瓦工部分)之详细攻略
  19. CanMV K210 AI开发板全网首发,项目代码完全开源,千套开发板限时优惠!
  20. PHP —— 一份前端开发工程师够用的PHP知识点(持续更新)

热门文章

  1. 超越IEtab、网银支付助手,无需再次登陆的Firefox的IE插件
  2. Predicate 在根据多个条件过滤数据的例子
  3. 《程序出错后,程序员给测试人员的20条高频回复》
  4. 卷积核一定可以提升网络性能吗?-分类0,2
  5. 【控制】《多智能体系统的动力学分析与设计》徐光辉老师-第7章-不确定分数阶系统的多一致
  6. 1.6 课程资源-深度学习-Stanford吴恩达教授
  7. STM32 基础系列教程 7 - 单脉冲PWM
  8. 猜想串scan的order
  9. 【PC工具】更新文字转语音、文字文本朗读工具,语音朗读软件,TTS语音合成...
  10. C++类的定义和声明