Spring Boot中Redis的安装和解析与实战(附源码 超详细)

创作不易 觉得有帮助请点赞关注收藏~~~

一、Redis简介

Redis是一个开源的使用ANSIC语言编写,支持网络,可基于内存亦可持久化的日志型,key-value数据库,并提供多种语言的API。它支持多种数据类型。

但是在Redis在内存中存储数据,因此,存放在Redis中的数据不应该大于内存容量,否则回导致操作系统性能降低

二、安装Redis

1:下载Redis

https://github.com/MSOpenTech/redis/tags

2:启动Redis服务

使用redis-server-windows.conf命令行启动Redis服务 

 进入Redis解压目录 执行设置服务命令

redis-server--service-install redis.windows-service.conf--loglevel verbose 设置成windows下的服务

 常用的Redis服务命令如下

卸载服务 redis-server --service-uninstall
开启服务 redis-server --service-start
停止服务 redis-server --service-stop

3:操作测试Redis

启动服务后 可以使用如下命令创建一个数据库服务

redis-cli.exe-h 127.0.0.1-p 6379
set key value保存数据
get key 获取数据

三、Spring Boot整合Redis

Spring对Redis的支持是通过Spring Data Redis来实现的,提供了RedisTemplate和StringRedisTemplate两个模板来进行数据操作。 

序列化

当数据从存储到Redis时,键值都是通过Spring提供的Serializer序列化到数据的。

在Spring Boot应用中 只要引入spring-boot-starter-data-redis依赖就可以使用默认配置的Redis进行数据操作

实战在Spring Boot应用中使用StringRedisTemplate和RedisTmeplate模板访问操作Redis数据库

1:创建Spring Boot Web应用时记得引入上文所说依赖

2:application.properties不用配置

3:创建实体类 

创建名为com.ch.ch6_9.entity的包 并创建Student类 代码如下

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:创建数据访问层

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 redisTemplate;
	/**
	 * 使用@Resource注解指定stringRedisTemplate,可注入基于字符串的简单属性操作方法
	 * ValueOperations valueOpsStr = stringRedisTemplate.opsForValue();
	 */
	@Resource(name="stringRedisTemplate")
	ValueOperations valueOpsStr;
	/**
	 * 使用@Resource注解指定redisTemplate,可注入基于对象的简单属性操作方法
	 * ValueOperations valueOpsObject = redisTemplate.opsForValue();
	 */
	@Resource(name="redisTemplate")
	ValueOperations 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 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:创建控制器层

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  stus = new ArrayList();
		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 getMultiStus(String key) {
		return (List)studentRepository.getObject(key);
	}
}

6:修改配置类

因为Redis默认使用序列化数据,这对使用Redis Client查看数据很不直观,所以我们自己配置并且定义Serializer

package com.ch.ch6_9;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
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;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@EnableCaching
@SpringBootApplication
public class Ch69Application {
	public static void main(String[] args) {
		SpringApplication.run(Ch69Application.class, args);
	}
	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
		RedisTemplate rTemplate = new RedisTemplate();
		rTemplate.setConnectionFactory(redisConnectionFactory);
		@SuppressWarnings({ "unchecked", "rawtypes" })
		Jackson2JsonRedisSerializer  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;
	}
}
 
  

你可能感兴趣的:(Spring,Boot,redis,spring,boot,java,eclipse,spring)