一、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里的数据;
求大神指点迷津!!!