springboot 整合 redis

安装redis

安装镜像
docker pull redis:6.0

运行容器
docker run -itd --name myredis -p 6379:6379 redis:6.0
或
docker run -itd --privileged=true --restart=always --name myredis -p 6379:6379 redis:6.0

创建项目

  • 编辑器: idea

  • Create New Project -> Spring Initializr ->next-> 添加项目信息

  • 添加依赖 spring data redis

配置properties

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.99.100
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=20
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000

Bean层

  • pojo entity bean都是实体类,但是现在用entity多一点

  • 创建Bean文件夹并在下面创建User实体类

package indi.xzw.redis_basic.bean;

import java.util.Date;

public class User {
    private String name;
    private int age;
    private Date time;

    public User(String name, int age, Date time) {
        this.name = name;
        this.age = age;
        this.time = time;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", time=" + time +
                '}';
    }
}

Dao层

  • 创建dao目录,在该目录下新建UserRedis
package indi.xzw.redis_basic.dao;

import indi.xzw.redis_basic.bean.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserRedis {
    @Autowired
    private RedisTemplate<Object,User> redisTemplate;
    public void add(String key,User user){
        redisTemplate.opsForValue().set(key,user);
    }
    public User get(String key){
        return redisTemplate.opsForValue().get(key);
    }
}

Config 层

  • 创建config 目录,在该目录下新建 RedisConfig 实体类

  • 由于在进行插入数据,或者查询数据的时候,如果使用默认的配置,会出现在Readis中存储乱码的情况。所以在使用Redis的时候要根据具体的情况,使用自定义配置的方式进行配置,实现获取数据的正常显示。

package indi.xzw.redis_basic.config;

import indi.xzw.redis_basic.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object, User> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        //自定义实现对于RedisTemplate的配置
        RedisTemplate<Object, User> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //设置一JSON格式存储或者获取对象
        Jackson2JsonRedisSerializer<User> ser = new Jackson2JsonRedisSerializer<>(User.class);
        //为templeate设置默认的序列化方式
        template.setDefaultSerializer(ser);
        return template;
    }
}

添加jackson依赖

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.10.0</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.10.0</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.10.0</version>
</dependency>

测试

在test 里面新建目录dao,并在dao里面创建UserRedisTest测试类

package indi.xzw.redis_basic.dao;

import indi.xzw.redis_basic.bean.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.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRedisTest {
    @Autowired
    UserRedis userRedis;

    @Test
    public void setup(){
        User user = new User();
        user.setName("xiazhongwei");
        user.setAge(27);
        user.setTime(new Date());
        userRedis.add(this.getClass().getName(),user);
        System.out.println(userRedis.get(this.getClass().getName()));
    }
}

18

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×