最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
SpringBoot集成Redis思路代码示例解析
时间:2021-10-16 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下SpringBoot集成Redis思路代码示例解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
SpringBoot集成Redis
1、概述
Redis是什么?
Redis(Remote Dictionary Server ),即远程字典服务。
是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis能该干什么?
内存存储、持久化,内存是断电即失的,所以需要持久化(RDB、AOF)高效率、用于高速缓冲发布订阅系统地图信息分析计时器、计数器(eg:浏览量)… …
特性
多样的数据类型
持久化
集群
事务
…
2、测试Redis
SpringBoot操作数据,Spring-Data、 jbdc、redis… …
SpringData与SpringBoot齐名的项目!
说明:在SpringBoot2.x之后,原来使用的jedis被替换为lettuce
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,需使用jedis pool连接池!像BIO模式
lettuce:采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据,更像NIO模式
< alt="" src="http://.111com.net/attachment/art/203939/09fb7387d4.jpg" data-ke-src="http://.111com.net/attachment/art/203939/09fb7387d4.jpg" style="height:auto;" />
新建一个项目
< alt="" src="https://.jbzj.com/file_images/article/202110/2021101516300525.jpg" data-ke-src="https://.jbzj.com/file_images/article/202110/2021101516300525.jpg" />
< alt="" src="https://.jbzj.com/file_images/article/202110/2021101516300526.jpg" data-ke-src="https://.jbzj.com/file_images/article/202110/2021101516300526.jpg" style="height:auto;" />
注意:
< alt="" src="https://.jbzj.com/file_images/article/202110/2021101516300527.jpg" data-ke-src="https://.jbzj.com/file_images/article/202110/2021101516300527.jpg" style="height:auto;" />
查看底层
< alt="" src="https://.jbzj.com/file_images/article/202110/2021101516300528.jpg" data-ke-src="https://.jbzj.com/file_images/article/202110/2021101516300528.jpg" style="height:auto;" />
源码分析:
@Bean @ConditionalOnMissingBean( //如果未注入组件条件,我们自己可以定义一个redisTemplate来替换这个默认的 name = {"redisTemplate"} ) public RedisTemplate
1、导入依赖
2、配置连接
# SpringBoot 所有的配置类 都有一个自动配置类 RedisAutoConfiguration # 自动配置类都会绑定一个 properties 配置文件 RedisProperties #配置 redis spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis
3、测试!
package com.kk; 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 Redis01SpringbootApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void contextLoads() { /* redisTemplate opsForValue 操作字符串的 类似String opsForList 操作List 类似List opsForSet opsForHash opsForZSet opsForGeo opsForHyperLogLog 除了基本的操作 ,我们常用的方法都可以直接通过redisTemplate 比如事务和基本的CRUD */ //获取redis的连接对象 // RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); // connection.flushDb(); // connection.flushAll(); redisTemplate.opsForValue().set("kk1","kk2"); System.out.println(redisTemplate.opsForValue().get("kk1")); } }
< alt="" src="https://.jbzj.com/file_images/article/202110/2021101516300529.jpg" data-ke-src="https://.jbzj.com/file_images/article/202110/2021101516300529.jpg" style="height:auto;" />
3、自定义redisTemplate
首先先建一个实体类,测试
User类
package com.kk.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; import java.io.Serializable; @Component @Data @AllArgsConstructor @NoArgsConstructor //在企业中,我们所有的pojo都会序列化 public class User implements Serializable { private String name; private int age; }
测试:
@Test public void test() throws JsonProcessingException { //真实的开发一般都使用json来传递对象 User user = new User("kk", 17); String jsonUser = new ObjectMapper().writeValueAsString(user);//这样就变成了一个json对象了 redisTemplate.opsForValue().set("user",jsonUser); System.out.println(redisTemplate.opsForValue().get("user")); }
r = new ObjectMapper().writeValueAsString(user);//这样就变成了一个json对象了 redisTemplate.opsForValue().set(“user”,jsonUser); System.out.println(redisTemplate.opsForValue().get(“user”)); }
==注意:如果不在User类中实现序列化,它会报错==
-
上一个: java多态注意项代码示例
-
下一个: uniapp和vue的区别代码示例解析
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24