Springboot 使用Jedis

Springboot 使用Jedis#

依赖#

 

Copy

<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#

 

Copy

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#

 

Copy

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); } }

遇到的问题#

  1. 无法set bookBook类要实现序列化接口
  2. Jedis Read time out
    • redis-cli 登陆客户端,发现ping 长时间无响应
    • 解决方案: 重启redis-server
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值