Redis(五)SpringBoot整合

1、环境搭建及简单入门

导入依赖:pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.experimental</groupId>
        <artifactId>spring-native</artifactId>
        <version>${spring-native.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

配置文件:application.properties spring.redis.port=6379 spring.redis.host=127.0.0.1

测试代码

package com.zch.redisspringboot;
​
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
​
@SpringBootTest
class RedisSpringbootApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
​
    @Test
    void contextLoads() {
​
        // redisTemplate 造作不同的数据类型
        // opsForValue  操作字符串,类似于String
        // opsForList
        // opsForSet
        // opsForHash
        // opsForZSet
        // opsForGeo
​
        // 获取redis的连接对象 操作数据库
//        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
//        connection.flushAll();
//        connection.flushDb();
        redisTemplate.opsForValue().set("money","1000");
        System.out.println(redisTemplate.opsForValue().get("money"));
    }
​
}
 

2、自定义的redisTemplate

新建实体类User:

@Component
public class User {   
        private String username; 
        private String password;    
        //此处省略setter getter tostring 有参无参构造方法
     }

测试方法:

 @Test   
  public void test01(){      
            User user = new User("张三","1234");   
            template.opsForValue().set("user", user);     
            System.out.println(template.opsForValue().get("user"));   
        }

序列化 如果实体类不进行序列化会如下错:

在实体类上实现序列化,再次运行

@Component
public class User implements Serializable{   
        private String username;  
        private String password;
     }

发现虽然程序成功运行,但是redis的客户端key却是转义字符,这是因为redis默认的序列化方式是jdk,因此我们需要自定义序列化方式

自定义序列化

简单配置

@Configuration
public class RedisConfiguration {   
        @Bean(name = "redisTemplate")   
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { 
        RedisTemplate<String , Object> template = new RedisTemplate<>();    
        Jackson2JsonRedisSerializer<Object> json = new Jackson2JsonRedisSerializer<Object>(Object.class); 
        template.setDefaultSerializer(json);       
        template.setConnectionFactory(redisConnectionFactory);       
        return template;   
     }
 }

  

详细配置

 package com.zch.config;
​
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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 org.springframework.data.redis.serializer.StringRedisSerializer;
​
/**
 * Author: zhaoHui
 * Date: 2022/01/09
 * Time: 15:26
 * Description:
 */
@Configuration
public class RedisConfig {
    // 固定模板,可以直接使用
    @Bean(name = "template")
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //使用json序列化数据
        Jackson2JsonRedisSerializer<Object> json = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        json.setObjectMapper(mapper);
        //String的序列化
        StringRedisSerializer str = new StringRedisSerializer();
        template.setKeySerializer(str);
        template.setHashKeySerializer(str);
        template.setValueSerializer(json);
        template.setHashValueSerializer(json);
        template.afterPropertiesSet();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值