028_SpringBoot整合Redis

1. 使用maven构建SpringBoot的名叫spring-boot-redis项目

2. Spring Data Redis的启动器

3. pom.xml 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.bjbs</groupId>
	<artifactId>spring-boot-redis</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.13.RELEASE</version>
	</parent>

	<!-- 修改jdk版本 -->
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- springBoot的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Spring Data Redis的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!-- Test的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
	</dependencies>
</project>

4. 在src/main/resources下, 新建application.properties

spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-total=20

spring.redis.hostName=192.168.25.138
spring.redis.port=6379
spring.redis.password=lyw123456

5. 新建RedisConfig.java

package com.bjbs.config;

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.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 完成对Redis的整合的一些配置
 */
@Configuration // @Configuration注解SpringBoot启动的时候会初始化该类
public class RedisConfig {
	/**
	 * 1. 创建JedisPoolConfig对象, 在该对象中完成一些链接池配置。
	 * @ConfigurationProperties: 会将配置文件中, 前缀(spring.redis.pool)相同的内容创建一个实体, 然后将剩余部分(max-idle)作为属性添加到实体中。
	 */
	@Bean
	@ConfigurationProperties(prefix = "spring.redis.pool")
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig config = new JedisPoolConfig();
		/*
		 * //最大空闲数 config.setMaxIdle(10); 
		 * //最小空闲数 config.setMinIdle(5); 
		 * //最大链接数 config.setMaxTotal(20);
		 */
		System.out.println("默认值: " + config.getMaxIdle());
		System.out.println("默认值: " + config.getMinIdle());
		System.out.println("默认值: " + config.getMaxTotal());
		// 返回对象后, 才能完成属性注入
		return config;
	}

	/**
	 * 2.创建JedisConnectionFactory: 配置redis链接信息
	 */
	@Bean
	@ConfigurationProperties(prefix = "spring.redis")
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
		System.out.println("配置完毕: " + config.getMaxIdle());
		System.out.println("配置完毕: " + config.getMinIdle());
		System.out.println("配置完毕: " + config.getMaxTotal());

		JedisConnectionFactory factory = new JedisConnectionFactory();
		// 关联链接池的配置对象
		factory.setPoolConfig(config);
		/* //配置链接Redis的信息
		 * //主机地址factory.setHostName("192.168.70.128"); 
		 * //端口 factory.setPort(6379);
		 * //密码factory.setPassword("lyw123456");
		 */
		return factory;
	}

	/**
	 * 3.创建RedisTemplate: 用于执行Redis操作的方法
	 */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory) {
		RedisTemplate<String, Object> template = new RedisTemplate<>();
		// 关联
		template.setConnectionFactory(factory);
		// 为key设置序列化器
		template.setKeySerializer(new StringRedisSerializer());
		// 为value设置序列化器
		template.setValueSerializer(new StringRedisSerializer());
		return template;
	}
}

6. 新建User.java

package com.bjbs.pojo;

import java.io.Serializable;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private Integer id;
	private String name;
	private Integer age;

	public User() {

	}

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

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

	public Integer getAge() {
		return age;
	}

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

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

7. 新建App.java

package com.bjbs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

8. 新建RedisTest.java

package com.bjbs.test;

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.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.bjbs.App;
import com.bjbs.pojo.User;

/**
 * SpringBoot整合Redis测试类
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class RedisTest {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	/**
	 * 添加一个字符串
	 */
	@Test
	public void setStr() {
		this.redisTemplate.opsForValue().set("spring-boot-redis", "SpringBoot整合Redis存储了一个字符串");
	}

	/**
	 * 获取一个字符串
	 */
	@Test
	public void getStr() {
		String value = (String) this.redisTemplate.opsForValue().get("spring-boot-redis");
		System.out.println(value);
	}
	
	/**
	 * 删除一个值
	 */
	@Test
	public void delValue(){
		redisTemplate.delete("spring-boot-redis");
	}

	/**
	 * 添加User对象, 使用JDK的序列化器
	 */
	@Test
	public void setUesr() {
		User user = new User();
		user.setAge(20);
		user.setName("张三丰");
		user.setId(1);
		// 重新设置序列化器
		this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		this.redisTemplate.opsForValue().set("user", user);
	}

	/**
	 * 取User对象, 使用JDK的序列化器
	 */
	@Test
	public void getUser() {
		// 重新设置序列化器
		this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		User users = (User) this.redisTemplate.opsForValue().get("user");
		System.out.println(users);
	}

	/**
	 * 基于JSON格式存User对象
	 */
	@Test
	public void setUseJSON() {
		User user = new User();
		user.setAge(20);
		user.setName("李四丰");
		user.setId(1);
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
		this.redisTemplate.opsForValue().set("user_json", user);
	}

	/**
	 * 基于JSON格式取User对象
	 */
	@Test
	public void getUseJSON() {
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
		User user = (User) this.redisTemplate.opsForValue().get("user_json");
		System.out.println(user);
	}
}

9. 启动服务器上的Redis, 选中setStr方法, 右键——>Run As——>JUnit Test

10. 选中getStr方法, 右键——>Run As——>JUnit Test 

11. 选中setUser方法, 右键——>Run As——>JUnit Test 

12. 选中getUser方法, 右键——>Run As——>JUnit Test 

13. 选中setUserJSON方法, 右键——>Run As——>JUnit Test 

14. 选中getUserJSON方法, 右键——>Run As——>JUnit Test 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值