SpringBoot进阶(七)SpringBoot整合SpringData Redis

前言

      本章讲解SpringBoot整合SpringData Redis的相关知识

方法

1.概念

在SpringData章节,我讲解了有关SpringData Redis的相关知识,之前整合的是Spring。那么我们使用了SpringBoot之后呢,要该怎么整合SpringData Redis呢?这就是接下来要说的内容。

需要事先安装好Redis,该步骤不在赘述。

2.整合流程

1)新建项目

SpringBoot进阶(七)SpringBoot整合SpringData Redis_第1张图片

2)修改pom文件,添加必要的jar包坐标


	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.7.RELEASE
	
	cn.edu.ccut
	springboot-redis
	0.0.1-SNAPSHOT

	
		
		1.8
		
		3.1.0
	

	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
		
			org.springframework.boot
			spring-boot-starter-data-redis
		
		
		
		    redis.clients
		    jedis
		
		
		
			org.springframework.boot
			spring-boot-devtools
		
		
		
			org.springframework.boot
			spring-boot-starter-test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					cn.edu.ccut.App
				
			
		
	

3)修改application.properties文件

#Redis
spring.redis.hostName=192.168.1.103
spring.redis.port=6379

4)新增实体类,实现序列化接口

package cn.edu.ccut.bo;

import java.io.Serializable;

public class Users implements Serializable {
	private Integer id;
	private String username;
	private String password;

    public Users() {
    }

    public Users(Integer id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}

	public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

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

5)创建SpringData Redis配置类(重点

package cn.edu.ccut.conf;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * SpringData Redis配置类
 * @author jwang
 *
 */
@Configuration
public class RedisConfig {
	
	//配置RedisStandaloneConfiguration
	@Bean
	@ConfigurationProperties(prefix="spring.redis")//该注解表示读取application.properties中指定开头键值对封装返回的对象
	public RedisStandaloneConfiguration getStandaloneConfiguration(){
		RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
		return standaloneConfig;
	}
	
	//配置Jedis连接工厂
	@Bean
	public JedisConnectionFactory getJedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig){
		JedisConnectionFactory connectionFactory = new JedisConnectionFactory(standaloneConfig);
		return connectionFactory;
	}
	
	//配置Redis模板类
	@Bean
	public RedisTemplate getRedisTemplate(JedisConnectionFactory connectionFactory){
		RedisTemplate redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(connectionFactory);
		//设置redis序列化器
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
		return redisTemplate;
	}
}

该步骤重点在于生成模板类对象redisTemplate!

6)编写测试类测试

package cn.edu.ccut.test;

import java.util.List;

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;

import cn.edu.ccut.App;
import cn.edu.ccut.bo.Users;

@RunWith(SpringRunner.class)
@SpringBootTest(classes={App.class})
public class RedisTest {
	
	@Autowired
	private RedisTemplate redisTemplate;

	@Test
	public void testRedis(){
		redisTemplate.opsForValue().set("username", "zhangsan");
		System.out.println("================username:"+redisTemplate.opsForValue().get("username"));
	}
	
	@Test
	public void testRedis01(){
		//本次添加对象采用hash存储,key为users,代表表名
		//其存储的键值对采用key=id,value=对象的方法
		//本次采用Json格式存储
		Users user01 = new Users(1,"jwang","jwang");
		Users user02 = new Users(2,"zhangsan","zhangsan");
		Users user03 = new Users(3,"lisi","lisi");
		redisTemplate.opsForHash().put("users", user01.getId().toString(), user01);
		redisTemplate.opsForHash().put("users", user02.getId().toString(), user02);
		redisTemplate.opsForHash().put("users", user03.getId().toString(), user03);
	}
	
	@Test
	public void testRedis02(){
		List users = redisTemplate.opsForHash().values("users");
		for (Object user : users) {
			System.out.println(user);
		}
	}
} 
  

测试结果如下:

SpringBoot进阶(七)SpringBoot整合SpringData Redis_第2张图片

观察Redis数据情况:

SpringBoot进阶(七)SpringBoot整合SpringData Redis_第3张图片

SpringBoot进阶(七)SpringBoot整合SpringData Redis_第4张图片

你可能感兴趣的:(SpringBoot)