第 15 讲 SpringBoot集成Redis

第十五讲 SpringBoot集成Redis

1.Window下安装Redis

1.1 下载Redis单机版安装包

下载网址:https://github.com/MicrosoftArchive/redis/releases

a

1.2 启动Redis

1.2.1 解压压缩包

a

1.2.2 启动redis

redis-server.exe redis.windows.conf

1.2.3 配置窗口关闭,redis依然在运行状态

redis-server --service-install redis.windows.conf

然后使用下面命令就可以启动了:

redis-server --service-start

这样即使Redis服务端的窗口关闭了,也不会影响Redis服务了。

2.SpringBoot集成Redis

2.1 添加依赖

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

2.2 application.yml中配置Redis

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 3000
    pool:
      max-active: 200
      max-wait: 1000
      max-idle: 200
      min-idle: 50

2.3 RedisConfig类

package com.springboot.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Description:
 * @Author: zrblog
 * @CreateTime: 2018-10-08 10:04
 * @Version:v1.0
 */
@Configuration
public class RedisConfig {

    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

实体User类


package com.springboot.redis.domain;

import java.io.Serializable;

/**
 * @Description:
 * @Author: zrblog
 * @CreateTime: 2018-10-08 10:08
 * @Version:v1.0
 */
public class User implements Serializable {

    private Integer id;
    private String name;

    public User() {
    }

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

2.4 测试Redis

package com.springboot.redis;

import com.springboot.redis.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Test
    public void contextLoads() {
    }

    @Test
    public void redisTest() {

        //存儲字符串
        redisTemplate.opsForValue().set("a", 1);

        Object a = redisTemplate.opsForValue().get("a");
        System.out.println("a:" + a);

        //存取user对象
        User user = new User(1, "李四");

        redisTemplate.opsForValue().set("user",user);

        Object result = redisTemplate.opsForValue().get("user");

        System.out.println(result.toString());
    }
}

测试结果:

a

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值