Spring Boot 集成 Redis

一、Spring Boot 集成 Redis

配置文件pom.xml:


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

        <!-- redis保存Session-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

配置文件application.properties:


spring.redis.database=1

##指定Session的存储方式
spring.session.store-type=Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=Redis!123
# 最大连接数
spring.redis.jedis.pool.max-active = 8
# 链接超时时间(毫秒)
spring.redis.timeout=5000

二、Redis在Spring Boot中的测试代码

RedisStringController代码如下:

package com.example.shopgoods.controller.redisTest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @Author: zp
 * @Date: 2019/4/28 10:39
 * @Description:
 */
@RestController
@RequestMapping("/strRedis")
public class RedisStringController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @PostMapping("/setGet")
    public String env(@RequestParam(value = "para") String para) {
        redisTemplate.opsForValue().set("testEnv", para);
        BoundListOperations boundListOperations = redisTemplate.boundListOps("boundList");
        boundListOperations.leftPush("a");
        System.out.println(boundListOperations.size());


        String str = stringRedisTemplate.opsForValue().get("testEnv");


        redisTemplate.opsForValue().set("key0", "hello");
        redisTemplate.opsForValue().set("key1", User.getSampleUser());
        String value = (String) redisTemplate.opsForValue().get("key0");
        User user = (User) redisTemplate.opsForValue().get("key1");
        System.out.println(value);
        System.out.println(user);
        return str;
    }


}

实体类User代码如下:

package com.example.shopgoods.controller.redisTest;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author: zp
 * @Date: 2019/4/28 14:56
 * @Description:
 */
@Data
public class User implements Serializable {
    private int id;
    private String name;

    public static User getSampleUser() {
        User user = new User();
        user.setId(123);
        user.setName("abc");
        return user;
    }
}

疑问:

1.redis Manager分多个db是什么意思?
如图:从redis可视化工具RedisDesktopManager中可以看见有db0,db1,……;
然而,我通过redis命令只可以获取到db0里的数据,却获取不到db1里的数据;
求大神指点迷津!!!
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值