栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot---------Redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot---------Redis

基本使用

首先添加依赖:


            org.springframework.boot
            spring-boot-starter-data-redis
                    

默认会连接到localhost:6379 所以项目中不需要进行任何的配置就可以使用自动配置已经配置好的 3个类型 RedisConnectionFactory, StringRedisTemplate, RedisTemplate

配置:

  @Bean
    public RedisTemplate redisTemplate2(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

    //这里配置一个序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(genericJackson2JsonRedisSerializer);
        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        return template;
    }

server类:

 @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public void doSth(){
        redisTemplate.opsForValue().set("boot","test");
        stringRedisTemplate.opsForValue().set("bootStr","test2");
    }

启动类:

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

    @Autowired
    private RedisDemo redisDemo;
    @Override
    public void run(String... args) throws Exception {
        redisDemo.doSth();
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777801.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号