小呆呆的生活

  • 首页
  • 分类
    • Linux
    • MySQL
    • SpringBoot
    • SpringCloud
  • 工具
  • 留言
  • 登录
  • 注册
  • 友情链接
    • 咸鱼的窝
    • DIY熙的家
    • Farmer的自习室
    • Dark的小黑屋
  • 关于
人的一生注定会遇到两个人
一个惊艳了时光,一个温柔了岁月
  1. 首页
  2. SpringBoot框架
  3. 正文

Spring Boot整合Redis

2020年 4月 18日 766次阅读 0人点赞 0条评论

Redis简介:

Redis 是一个开源(BSD许可)的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件,并提供多种语言的API。

Redis优点:

  1. 存取速度快:Redis速度非常快,每秒可执行大约110000次的设值操作,或者执行81000次的读取操作。
  2. 支持丰富的数据类型:Redis支持开发人员常用的大多数数据类型,例如列表、集合、排序集和散列等。
  3. 操作具有原子性:所有Redis操作都是原子操作,这确保如果两个客户端并发访问,Redis服务器能接收更新后的值。
  4. 提供多种功能:Redis提供了多种功能特性,可用作非关系型数据库、缓存中间件、消息中间件等。

下载:

  • Linux:https://redis.io/download 或 https://download.redis.io/releases/
  • Windows:https://github.com/MicrosoftArchive/redis/tags
  • 下载安装教程可参考:https://blog.csdn.net/leisure_life/article/details/82078233

整合步骤:

  1. 在pom文件中添加Spring Data Redis依赖启动器

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  2. 编写实体类

    @RedisHash("persons")  
    public class Person {
        @Id        
        private String id;
        @Indexed  
        private String firstname;
        @Indexed
        private String lastname;
        private Address address;
        private List<Family> familyList;
     }
    public class Address {
        @Indexed
        private String city;
        @Indexed
        private String country;
    }
    
  3. 编写Repository接口

    public interface PersonRepository extends CrudRepository<Person, String> {
        List<Person> findByAddress_City(String city);
    }
    
  4. 在全局配置文件application.properties中添加Redis数据库连接配置

    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=
    
  5. 编写单元测试进行接口方法测试以及整合测试

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisTests {
        @Autowired
        private PersonRepository repository;
        @Test
        public void selectPerson() {
          List<Person> list = repository.findByAddress_City("北京");
          System.out.println(list);
        }
    }
    
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Redis Spring SpringBoot
最后更新:2021年 7月 13日

小呆呆

知足常乐,就会拥有幸福

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

小呆呆

知足常乐,就会拥有幸福

最新 热点 随机
最新 热点 随机
数据库索引简析 Java多线程的使用场景以及线程的创建方式 Spring事务的使用示例和传播行为以及失效场景 Spring Boot使用JUnit和Mockito进行Service层单元测试 Spring Cloud Zuul和Gateway的简单示例(搭建方式) Spring Cloud的网关Zuul和Gateway
Spring Boot整合Redis Centos7安装MySQL教程 Spring Cloud Feign的理解以及搭建方式(Feign调用简单示例) 领取阿里云免费SSL证书 PicGo+GitHub+CDN加速+Typora搭建图床 Typora如何破解
标签聚合
干货 面试 SpringCloud MySQL 后端 SpringBoot Java Spring
最近评论
我是可是尼古拉斯·爱新觉·罗·G·钰豪啊 发布于 3 年前(04月08日) 我来注水了胜哥 :hehe:
鸟人金 发布于 3 年前(03月03日) v
鸟人金 发布于 3 年前(03月03日) 胜哥yyds
鸟人金 发布于 3 年前(03月03日) 我滴偶像!!!!!!!!!!!!!!!
水军2号 发布于 3 年前(03月03日) 胜哥tql
归档
  • 2024 年 2 月
  • 2024 年 1 月
  • 2023 年 12 月
  • 2023 年 11 月
  • 2023 年 10 月
  • 2023 年 8 月
  • 2023 年 6 月
  • 2022 年 11 月
  • 2022 年 8 月
  • 2022 年 6 月
  • 2022 年 4 月
  • 2022 年 3 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 9 月
  • 2021 年 8 月
  • 2021 年 6 月
  • 2021 年 4 月
  • 2020 年 10 月
  • 2020 年 9 月
  • 2020 年 8 月
  • 2020 年 7 月
  • 2020 年 6 月
  • 2020 年 5 月
  • 2020 年 4 月
  • 2020 年 3 月

COPYRIGHT © 2023 小呆呆的生活. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

粤ICP备2020104583号

粤公网安备44011802000463号