Springboot 使用Jedis
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
application.yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: xxx
redis:
database: 0
host: 127.0.0.1
port: 6379
# password: xxx
jedis:
pool:
max-active: 8
max-idle: 8
min-idle: 0
BookController
package com.draymonder.book.mybatis;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @auther draymonder
*/
@RestController
public class BookController {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/test1")
public void test1() {
ValueOperations<String, String> op1 = stringRedisTemplate.opsForValue();
op1.set("name", "三国演义");
String name = op1.get("name");
System.out.println(name);
ValueOperations op2 = redisTemplate.opsForValue();
Book b1 = new Book();
b1.setId(1);
b1.setName("2333");
b1.setAuthor("2333");
op2.set("b1", b1);
Book book = (Book) op2.get("b1");
System.out.println(book);
}
}
遇到的问题
- 无法set
book
, Book
类要实现序列化接口 - Jedis Read time out
redis-cli
登陆客户端,发现ping
长时间无响应- 解决方案: 重启
redis-server