Spring Boot整合Spring Data Redis-存取Java对象

4.0.0com.learnspring-boot-redis0.0.1-SNAPSHOTorg.springframework.bootspring-boot-starter-parent1.5.12.RELEASEUTF-8UTF-81.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-data-redisorg.springframework.bootspring-boot-starter-test

package com.learn.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
public class RedisConfig {/*** 1.创建JedisPoolConfig对象。在该对象中完成一些链接池配置* @ConfigurationProperties:会将前缀相同的内容创建一个实体。*/@Bean@ConfigurationProperties(prefix="spring.redis.pool")
//	@ConfigurationProperties(prefix="aaa.bbb")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("10.40.8.152");//端口factory.setPort(6379);*/return factory;}/*** 3.创建RedisTemplate:用于执行Redis操作的方法*/@Beanpublic RedisTemplate redisTemplate(JedisConnectionFactory factory){RedisTemplate template = new RedisTemplate<>();//关联template.setConnectionFactory(factory);//为key设置序列化器template.setKeySerializer(new StringRedisSerializer());//为value设置序列化器template.setValueSerializer(new StringRedisSerializer());return template;}
}
package com.learn.pojo;import java.io.Serializable;public class Users implements Serializable {
//	public class Users {private Integer id;private String name;private Integer 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;}@Overridepublic String toString() {return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";}}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RedisApp {public static void main(String[] args) {SpringApplication.run(RedisApp.class, args);}
}
package com.learn.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.JdkSerializationRedisSerializer;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.learn.RedisApp;
import com.learn.pojo.Users;/*** Spring Data Redis测试*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=RedisApp.class)
public class RedisJdkSerializationTest {@Autowiredprivate RedisTemplate redisTemplate;/*** 添加Users对象*/@Testpublic void testSetUesrs(){Users users = new Users();users.setAge(20);users.setName("张三丰");users.setId(1);//重新设置序列化器this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
//		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Users.class));this.redisTemplate.opsForValue().set("users", users);}/*** 取Users对象*/@Testpublic void testGetUsers(){//重新设置序列化器this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
//		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Users.class));Users users = (Users)this.redisTemplate.opsForValue().get("users");System.out.println(users);}}

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部