前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot整合Redis

Spring Boot整合Redis

作者头像
别团等shy哥发育
发布2023-02-25 16:58:56
6420
发布2023-02-25 16:58:56
举报
文章被收录于专栏:全栈开发那些事

Spring Boot整合Redis

一、整合Redis

1、Spring Data Redise

 Spring对Redis的支持是通过Spring Data Redis来实现的。Spring Data Redis提供了RedisTemplate和StringRedisTemplate两个模板来进行数据操作,其中StringRedisTemplate只针对键值都是字符串类型的数据进行操作。  RedisTemplate和StringRedisTemplate模板提供的主要数据访问方法如下表:

方法

说明

posForValue()

操作只有简单属性的数据

opsForList()

操作含有List的数据

opsForSet()

操作含有Set的数据

opsForZSet()

操作含有Zset(有序的Set)的数据

opsForHash()

操作含有Hash的数据

2、Serializer

 当数据存储到Redis时,键和值都是通过Spring提供的Serializer序列化到数据的。RedisTemplate默认使用JdkSerializationRedisSerializer序列化,StringRedisTemplate默认使用StringRedisSerializer序列化。

3、Spring Boot的支持

Spring Boot对Redis的支持位于org.springframework.boot.autoconfigure.data.redis包下。 在RedisAutoConfiguration配置类中,默认配置了RedisTemplate和StringRedisTemplate,可以直接使用Redis存储数据。 在RedisProperties类中,可以使用以Spring.redis为前缀的属性在application.properties中配置Redis,主要属性默认配置如下:

代码语言:javascript
复制
spring.redis.database=0	#数据库名db0
spring.redis.host=localhost	#服务器地址
spring.redis.port=6379	#连接端口号
spring.redis.max-idle=8	#连接池的最大连接数
spring.redis.min-idle=0	#连接池的最小连接数
spring.redis.max-active=8	#在给定时间连接池可以分配的最大连接数
spring.redis.max-wait=-1	#当池被耗尽时,抛出异常之前连接分配应阻塞的最大时间量
							#(以毫秒为单位)使用负值表示无限期地阻止

二、使用StringRedisTemplate和RedisTemplate

1、创建基于spring-boot-starter-data-redis依赖的Spring Boot Web应用ch6_9

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、配置application.properties文件

在该Spring Boot应用ch6_9中,使用Redis的默认数据库连接。所以,不需要在application.properties文件中配置数据库连接信息。

3、创建实体类

创建名为com.ch.ch6_9.entity的包,并在改包中创建名为Student的实体类。该类必须实现序列化接口,这是因为使用Jackson做序列化需要一个空构造。 Student的代码如下:

代码语言:javascript
复制
package com.ch.ch6_9.entity;
import java.io.Serializable;
public class Student implements Serializable{
	private static final long serialVersionUID = 1L;
	private String sno;
	private String sname;
	private Integer sage;
	public Student() {
		super();
	}
	public Student(String sno, String sname, Integer sage) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.sage = sage;
	}
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Integer getSage() {
		return sage;
	}
	public void setSage(Integer sage) {
		this.sage = sage;
	}
}

4、创建数据访问层

创建名为com.ch.ch6_9.repository的包,并在该包中创建名为StudentRepository的类,该类使用@Repository注解标注为数据访问层 StudentRepository的代码如下:

代码语言:javascript
复制
package com.ch.ch6_9.repository;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import com.ch.ch6_9.entity.Student;
@Repository
public class StudentRepository {
	@SuppressWarnings("unused")
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@SuppressWarnings("unused")
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;
	/**
	 * 使用@Resource注解指定stringRedisTemplate,可注入基于字符串的简单属性操作方法
	 * ValueOperations<String, String> valueOpsStr = stringRedisTemplate.opsForValue();
	 */
	@Resource(name="stringRedisTemplate")
	ValueOperations<String, String> valueOpsStr;
	/**
	 * 使用@Resource注解指定redisTemplate,可注入基于对象的简单属性操作方法
	 * ValueOperations<Object, Object> valueOpsObject = redisTemplate.opsForValue();
	 */
	@Resource(name="redisTemplate")
	ValueOperations<Object, Object> valueOpsObject;
	/**
	 * 保存字符串到redis
	 */
	public void saveString(String key, String value) {
		valueOpsStr.set(key, value);
	}
	/**
	 * 保存对象到redis
	 */
	public void saveStudent(Student stu) {
		valueOpsObject.set(stu.getSno(), stu);
	}
	/**
	 * 保存List数据到redis
	 */
	public void saveMultiStudents(Object key, List<Student> stus) {
		valueOpsObject.set(key, stus);
	}
	/**
	 * 从redis中获得字符串数据
	 */
	public String getString(String key) {
		return valueOpsStr.get(key);
	}
	/**
	 * 从redis中获得对象数据
	 */
	public Object getObject(Object key) {
		return valueOpsObject.get(key);
	}
}

5、创建控制器层

由于实例比较简单,我们直接在控制器层调用数据访问层。创建名为com.ch.ch6_9.controller的包,并在改包中创建控制器类TestRedisController

代码语言:javascript
复制
package com.ch.ch6_9.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ch.ch6_9.entity.Student;
import com.ch.ch6_9.repository.StudentRepository;
@RestController
public class TestRedisController {
	@Autowired
	private StudentRepository studentRepository;
	@RequestMapping("/save")
	public void save() {
		studentRepository.saveString("uname", "陈恒");
		Student s1 = new Student("111","陈恒1",77);
		studentRepository.saveStudent(s1);
		Student s2 = new Student("222","陈恒2",88);
		Student s3 = new Student("333","陈恒3",99);
		List<Student>  stus = new ArrayList<Student>();
		stus.add(s2);
		stus.add(s3);
		studentRepository.saveMultiStudents("mutilStus",stus);
	}
	@RequestMapping("/getUname")
	@Cacheable(value = "myuname")
	public String getUname(String key) {
		System.out.println("测试缓存");
		return studentRepository.getString(key);
	}
	@RequestMapping("/getStudent")
	public Student getStudent(String key) {
		return (Student)studentRepository.getObject(key);
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/getMultiStus")
	public List<Student> getMultiStus(String key) {
		return (List<Student>)studentRepository.getObject(key);
	}
}

6、修改配置类Ch69Application

我们知道REdisTemplate默认使用JdkSerializationRedisSerializer序列化数据,这对使用Redis Client查看数据很不直观,因为JdkSerializationRedisSerializer使用二进制形式存储数据。所以,在此我们将自己配置RedisTemplate,并定义Serializer。 修改后的配置类Ch69Application的代码如下:

代码语言:javascript
复制
package com.ch.ch6_9;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@SpringBootApplication
public class Ch69Application {

    public static void main(String[] args) {
        SpringApplication.run(Ch69Application.class, args);
    }
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object, Object> rTemplate = new RedisTemplate<Object, Object>();
        rTemplate.setConnectionFactory(redisConnectionFactory);
        @SuppressWarnings({ "unchecked", "rawtypes" })
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        //设置值的序列化采用Jackson2JsonRedisSerializer
        rTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //设置键的序列化采用StringRedisSerializer
        rTemplate.setKeySerializer(new StringRedisSerializer());
        return rTemplate;
    }

}

7、运行测试

首先,运行CH69Application主类。然后通过http://localhost:8080/save测试存储数据。成功运行后,通过Redis Client查看数据,如下图所示:

在这里插入图片描述
在这里插入图片描述

通过http://localhost:8080/getUname?key=uname查询key为uname的字符串值,如图:

在这里插入图片描述
在这里插入图片描述

通过http://localhost:8080/getStudent?key=111查询key为111的Student对象值,如图所示:

在这里插入图片描述
在这里插入图片描述

通过http://localhost:8080/getMultiStus?key=mutilStus查询key为mutilStus的list集合,如图所示:

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring Boot整合Redis
  • 一、整合Redis
    • 1、Spring Data Redise
      • 2、Serializer
        • 3、Spring Boot的支持
        • 二、使用StringRedisTemplate和RedisTemplate
          • 1、创建基于spring-boot-starter-data-redis依赖的Spring Boot Web应用ch6_9
            • 2、配置application.properties文件
              • 3、创建实体类
                • 4、创建数据访问层
                  • 5、创建控制器层
                    • 6、修改配置类Ch69Application
                      • 7、运行测试
                      相关产品与服务
                      文件存储
                      文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档