springboot_redis
2020-12-13 03:53
标签:row tag serial compiler unknown set res actor gen 1.引入redis的启动器 2.指定redis的主机地址 3.自动注入模板 redsiAutoConfiguration: 测试保存对象: 添加配置,使其能以json的数据形式存入redis中: 将redis设为缓存中间件: 1.在pom.xml中引入redis的starter 2.将数据格式转为json形式 要修改redisManager 其余设置与spring-boot启动缓存的方式一样。 springboot_redis 标签:row tag serial compiler unknown set res actor gen 原文地址:https://www.cnblogs.com/ustc-anmin/p/11093688.htmldependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-redisartifactId>
dependency>
@Autowired
StringRedisTemplate stringRedisTemplate; //k v都是字符串的
@Autowired
RedisTemplate
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.data.redis;
import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate
@Test
public void test01(){
stringRedisTemplate.opsForValue().append("k2","_append_k2");
}
@Test
public void test02(){
User user = new User();
user.setId(36);
user.setAddress("上海市黄浦区赵家桥285号");
user.setAge(21);
user.setSex(1);
user.setName("包青天");
redisTemplate.opsForValue().set("usr1",user);//默认以java序列化的形式保存
}
package ustc.anmin.springboot.service;
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;
import ustc.anmin.springboot.bean.User;
import java.net.UnknownHostException;
@Configuration
public class MyRedisConfig {
@Bean
public RedisTemplate
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//设置CacheManager的值序列化方式为json序列化
RedisSerializer
上一篇:JSP:绝对路径引用文件
下一篇:js容易被忽视的问题