SpringBoot配置Redis连接详解

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Redis 版,经济版 1GB 1个月
简介: 本文讲述在SpringBoot环境下配置并连接Redis数据库详解

1、导入Redis相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、配置application.yml

server:
  port: 3060
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: *********

3、配置config

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class ConfigRedis {
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        // 序列化key
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        // 序列化hash
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        // 连接redis数据库
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        return redisTemplate;
    }
}

4、UserPojo

package com.redis.start.pojo;

public class UserPojo {
    private String id;
    private String name;
    private String pwd;

    public UserPojo() {
        super();
    }

    @Override
    public String toString() {
        return "UserPojo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }

    public UserPojo(String id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

5、Controller

import com.redis.start.pojo.UserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
@CrossOrigin
@ResponseBody
public class Controller {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/object")
    public String putObject(){
        UserPojo user = new UserPojo("001", "zhangSan","123456");
        // 序列化对象
        this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(UserPojo.class));
        this.redisTemplate.opsForValue().set("cookie@001", user,30, TimeUnit.SECONDS);
        System.out.print("/Object:");
        System.out.println(this.redisTemplate.opsForValue().get("cookie@001").toString());
        return "1";
    }
    @GetMapping("/list")
    public String putList(){
        this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(UserPojo.class));
        this.redisTemplate.opsForList().leftPush("cookie@001", new UserPojo("001", "zhangSan","123456"));
        System.out.print("/List:");
        System.out.println(this.redisTemplate.opsForList().range("cookie@001", 0, -1));
        return "1";
    }
    @GetMapping(value = "/key")
    public String putKey(String key, String value){
        this.redisTemplate.opsForValue().set(key,value,30, TimeUnit.SECONDS);
        System.out.print("/Key:");
        System.out.println(this.redisTemplate.opsForValue().get(key));
        return "1";
    }

    @GetMapping("/map")
    public String putMap(){
        Map<String, String> myMap = new HashMap<>();
        myMap.put("name", "zhangSan");
        myMap.put("age", "20");
        this.redisTemplate.opsForHash().putAll("map", myMap);
        System.out.print("/Map:");
        System.out.println(this.redisTemplate.opsForHash().get("map", "name"));
        return "1";
    }

}

6、MyRedisApplicationStart启动类

package com.redis.start;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyRedisApplicationStart {
    public static void main(String[] args) {
        SpringApplication.run(MyRedisApplicationStart.class,args);
    }
}

7、启动测试

1662015578172_A100B503-61E0-4419-8677-B908B00B3D1B.png

成功


相关文档:

Windows系统下安装Redis详细步骤
从零搭建微服务SpringCloud(二)新建一个SpringCloud项目

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
9天前
|
缓存 NoSQL Java
Redis Spring配置集群
【7月更文挑战第5天】
36 10
|
10天前
|
前端开发 NoSQL 数据库
部署常用的流程,可以用后端,连接宝塔,将IP地址修改好,本地只要连接好了,在本地上前后端跑起来,前端能够跑起来,改好了config.js资料,后端修改好数据库和连接redis,本地上跑成功了,再改
部署常用的流程,可以用后端,连接宝塔,将IP地址修改好,本地只要连接好了,在本地上前后端跑起来,前端能够跑起来,改好了config.js资料,后端修改好数据库和连接redis,本地上跑成功了,再改
|
10天前
|
弹性计算 NoSQL 网络安全
软件开发常见之云数据库Redis连接不上如何解决,修改配置后,需要重启下redis服务,配置才能生效呢,是重启,而不是重载配置,最后导致的问题是点击了的重启,配置修改了之后必须点击重启,而不是修改
软件开发常见之云数据库Redis连接不上如何解决,修改配置后,需要重启下redis服务,配置才能生效呢,是重启,而不是重载配置,最后导致的问题是点击了的重启,配置修改了之后必须点击重启,而不是修改
|
10天前
|
NoSQL Linux Redis
redis的安装和配置
redis的安装和配置
34 0
|
10天前
|
NoSQL JavaScript Redis
若依后端部署---若依部署,Redis在D盘的project的应用工具当中,在连接过程中,先用Xshell连接若依,RDM在应用工具里,同时host的主机也要写好
若依后端部署---若依部署,Redis在D盘的project的应用工具当中,在连接过程中,先用Xshell连接若依,RDM在应用工具里,同时host的主机也要写好
|
10天前
|
SQL NoSQL 关系型数据库
若依修改02,若以提供了多种版本,RuoYi-Cloud和SpringBoot+Vue都是PC端的,如果想要适配手机端,用Uniapp+vue,导入Mysql和启动Redis
若依修改02,若以提供了多种版本,RuoYi-Cloud和SpringBoot+Vue都是PC端的,如果想要适配手机端,用Uniapp+vue,导入Mysql和启动Redis
|
10天前
|
NoSQL Java 关系型数据库
软件开发常用之若依修改之添加数据库,添加redis,利用RDM连接宝塔
软件开发常用之若依修改之添加数据库,添加redis,利用RDM连接宝塔
|
10天前
|
Java Redis 数据安全/隐私保护
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
|
10天前
|
Java Redis 数据安全/隐私保护
Redis13的Java客户端-Jedis快速入门,建立连接的写法,ip地址,设置密码密码,选择库的写法
Redis13的Java客户端-Jedis快速入门,建立连接的写法,ip地址,设置密码密码,选择库的写法
|
13天前
|
NoSQL Redis 数据库
Redis的GUI工具——Another-Redis-Desktop-Manager连接远程数据库Redis
Redis的GUI工具——Another-Redis-Desktop-Manager连接远程数据库Redis
13 0