1.首先应该引入 依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
    @Autowired
StringRedisTemplate stringRedisTemplate; //操作字符串的
@Autowired
RedisTemplate redisTemplate; //k -v 操作对象的
   stringRedisTemplate.opsForValue().append("msg","hello");
String msg = stringRedisTemplate.opsForValue().get("msg");
System.out.println(msg);
stringRedisTemplate.opsForHash();
stringRedisTemplate.opsForList().leftPush("myList","");
stringRedisTemplate.opsForZSet();
stringRedisTemplate.opsForSet();

自定义序列化器

package cn.edu.aynu.config;

import cn.edu.aynu.bean.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.rmi.UnknownHostException; @Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate<Object, Employee> empredisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Employee> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
} }
package cn.edu.aynu;

import cn.edu.aynu.bean.Employee;
import cn.edu.aynu.mapper.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootCacheApplicationTests {
@Autowired
EmployeeMapper employeeMapper;
@Autowired
StringRedisTemplate stringRedisTemplate; //操作字符串的
@Autowired
RedisTemplate redisTemplate; //k -v 操作对象的
@Autowired
RedisTemplate<Object, Employee> empredisTemplate;
@Test
public void contextLoads() {
/*redis常见的5大数据类型*/
/*List(列表) String(字符串) zset(有序集合) set(集合) Hash(散列)*/
stringRedisTemplate.opsForValue().append("msg","hello");
String msg = stringRedisTemplate.opsForValue().get("msg");
System.out.println(msg);
/* stringRedisTemplate.opsForHash();
stringRedisTemplate.opsForList().leftPush("myList","1");
stringRedisTemplate.opsForZSet();
stringRedisTemplate.opsForSet();*/
}
@Test
public void Test02(){
Employee empById = employeeMapper.getEmpById();
//默认如果保存对象,使用jdk序列化机制,序列化后的数据保存到redis中
empredisTemplate.opsForValue().set("emp_01",empById);
/*redisTemplate.opsForValue().set("emp_id",empById);*/
System.out.println(empById);
} }
序列化的结果
{
"id": ,
"lastName": "zs",
"email": "zs@qq.com",
"gender": null,
"department": null,
"birth": null
}

springboot 如何操作redis的更多相关文章

  1. springboot jpa操作redis

    SpringBoot使用Redis缓存   (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boo ...

  2. springboot之使用redistemplate优雅地操作redis

    概述 本文内容主要 关于spring-redis 关于redis的key设计 redis的基本数据结构 介绍redis与springboot的整合 sringboot中的redistemplate的使 ...

  3. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  4. SpringBoot入门 (七) Redis访问操作

    本文记录学习在SpringBoot中使用Redis. 一 什么是Redis Redis 是一个速度非常快的非关系数据库(Non-Relational Database),它可以存储键(Key)与 多种 ...

  5. springboot中,使用redisTemplate操作redis

    知识点: springboot中整合redis springboot中redisTemplate的使用 redis存数据时,key出现乱码问题 一:springboot中整合redis (1)pom. ...

  6. 【快学springboot】14.操作redis之list

    前言 之前讲解了springboot(StringRedisTemplate)操作redis的string数据结构,这篇文章将会讲解list数据结构 list数据结构具有的操作 下图列出了redis ...

  7. 【快学springboot】13.操作redis之String数据结构

    前言 在之前的文章中,讲解了使用redis解决集群环境session共享的问题[快学springboot]11.整合redis实现session共享,这里已经引入了redis相关的依赖,并且通过spr ...

  8. SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存

    系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...

  9. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

随机推荐

  1. Linux操作系统有什么吸引力,在程序员中这么受欢迎?

    Linux操作系统有什么吸引力,在程序员中这么受欢迎? 在过去的几十年里,操作系统已经取得了很大的进步.曾经由微软Windows或苹果MacOS主导的操作系统,如今已不再是常态.千禧年之后,随着人们对 ...

  2. [CQOI2018]九连环

    嘟嘟嘟 对于这种找规律的题,我向来是不会的. 通过大佬们的各种打表找规律.神奇dp等方法,我们得到了答案就是\(\lfloor \frac{2 ^ {n + 1}}{3} \rfloor\). 高精是 ...

  3. L2-010 排座位 (并查集)

    这里唯一需要注意的是,各个输出的条件在题目中有点描述模糊. 是朋友关系,(不管是不是间接朋友关系) 既不是朋友也不是敌人(这里不用管是不是间接朋友) 是敌人关系,同时是间接朋友关系 是单纯的敌人关系, ...

  4. 上下文管理协议with open as

    我们知道在操作文件对象的时候可以这么写 with open('a.txt') as f: '代码块' 上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明_ ...

  5. 区分range() , np.arange() , np.linspace()

    content: range() np.arange() np.linspace() 一.range(start, stop, step) 1.range() 为 python 自带函数 2.生成一个 ...

  6. Matconvnet安装

    本文主要介绍Linux下Matconvnet的安装注意事项. 最近帮老师校验一份超分的代码,用到了matconvnet深度学习工具包.代码里面使用的是Matconvnet-1.0-beta20版本, ...

  7. Linux进程管理 (篇外)内核线程简要介绍

    关键词:kthread.irq.ksoftirqd.kworker.workqueues 在使用ps查看线程的时候,会有不少[...]名称的线程,这些有别于其它线程,都是内核线程. 其中多数内核线程从 ...

  8. 基于vs2015的rdlc报表运行环境部署

    先说明一下,rdlc报表是由visual studio来支持的,不是FM. 本次项目采用的是vs2015开发的,当中使用了ReportViewer报表. 两种方式可以支持开发rdlc报表环境: 1)在 ...

  9. 读写分离子系统 - C# SQL分发子系统 - Entity Framework支持

    A2D Framework增加了EF支持,加上原先支持ADO.NET: 支持EF方式 支持ADO.NET方式 这次来讲如何让Entity Framework变成nb的读写分离 1. 先设计EF模型, ...

  10. java连接Mysql8

    相较于之前版本会有部分改动 pom依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql- ...