码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot 使用Jedis

时间:2019-11-10 15:21:57      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:cto   bin   core   amp   work   start   mapping   ppi   jdbc   

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

遇到的问题

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

Springboot 使用Jedis

标签:cto   bin   core   amp   work   start   mapping   ppi   jdbc   

原文地址:https://www.cnblogs.com/Draymonder/p/11830096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!