springboot+redis

redis:键值对数据库
导入坐标

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置文件配置类

@Configuration//配置类注解
public class RedisConfig {
	@Bean//配置redis配置
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig config=new JedisPoolConfig();
		config.setMaxIdle(10);//最大空闲数
		config.setMinIdle(5);//最小空闲数
		config.setMaxTotal(20);//最大连接数
		return config;
	}
	@Bean//配置redis连接信息
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
		//spring2.3之后方式
		//添加集群
		Set<String> sentinelHostAndPorts=new HashSet<String>();
		sentinelHostAndPorts.add("192.168.190.128:6379");
		RedisSentinelConfiguration configuration=new RedisSentinelConfiguration("192.168.190.128", sentinelHostAndPorts);
		JedisConnectionFactory factory=new JedisConnectionFactory(configuration,config);
		//旧版本方式
		//factory.setHostName("");
		//factory.setPoolConfig(config);
		return factory;
	}
	@Bean//用于执行redis的方法
	public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
		//添加redis处理机制:由于redis都是以字符串存储,需要进行转换
		RedisTemplate<String, Object> redisTemplate=new RedisTemplate<>();
		redisTemplate.setConnectionFactory(factory);
		//设置键序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//设置值的序列化操作
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		return redisTemplate;
	}
}

二、使用properties配置配置信息

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.190.128
spring.redis.port=6379
redisConfig.java
@Configuration//配置类注解

public class RedisConfig {
	@Bean//配置redis配置
	@ConfigurationProperties("spring.redis.pool")
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig config=new JedisPoolConfig();
		System.out.println("测试2:"+config.getMaxIdle());
		System.out.println("测试2:"+config.getMinIdle());
		System.out.println("测试2:"+config.getMaxTotal());
		return config;
	}
	@Bean//配置redis连接信息
	@ConfigurationProperties(prefix="spring.redis")
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
		System.out.println("测试1:"+config.getMaxIdle());
		System.out.println("测试1:"+config.getMinIdle());
		System.out.println("测试1:"+config.getMaxTotal());
		JedisConnectionFactory factory=new JedisConnectionFactory();
		factory.setPoolConfig(config);
		return factory;
	}
	@Bean//用于执行redis的方法
	public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
		//添加redis处理机制:由于redis都是以字符串存储,需要进行转换
		RedisTemplate<String, Object> redisTemplate=new RedisTemplate<>();
		redisTemplate.setConnectionFactory(factory);
		//设置键序列化
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		//设置值的序列化操作
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		return redisTemplate;
	}
}

三、使用redis存储对象

@Test
	public void testSetUser() {
		User user=new User();
		user.setId(1);
		user.setName("张三");
		//设置对象序列化器,对象需要实现序列化接口
		this.redisUser.setValueSerializer(new JdkSerializationRedisSerializer());
		this.redisUser.opsForValue().set("user", user);
	}
	@Test
	public void testGetUser() {
		this.redisUser.setValueSerializer(new JdkSerializationRedisSerializer());
		User value=(User) this.redisUser.opsForValue().get("user");
		System.out.println(value.toString());
	}

四、使用redis存储JSON

@Test
	public void testSetJSON() {
		User user=new User();
		user.setId(2);
		user.setName("李四");
		//设置对象序列化器,对象需要实现序列化接口
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));//需要向序列化器说明对象
		this.redisTemplate.opsForValue().set("user_json", user);
	}
	@Test
	public void testGetJSON() {
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));//需要向序列化器说明对象
		User value=(User) this.redisTemplate.opsForValue().get("user_json");
		System.out.println(value.toString());
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值