Spring Boot 8:Spring Boot整合Redis
文章目录
- 一、搭建Redis环境
- 1、下载Redis安装压缩包
- 2、将Redis安装包解压到任意位置
- 3、配置环境变量
- 4、启动Redis服务器
- 二、安装Redis可视化管理工具
- Redis可视化工具连接Redis服务
- 三、创建Spring Boot 项目
- 四、创建实体类
- 1、地址实体类
- 2、家庭实体类
- 3、创建人实体类
- 五、创建自定义Repository接口
- 六、在全局配置文件配置Redis属性
- 七、编写测试方法
- 1、 testAddPerson()
- 2、testFindAll()
- 3、testFindById()
- 八、测试自定义个性化查询方法
- 1、在PersonRepository接口定义方法
- 2、testFindPersonByLastName()
- 3、testFindByLastName()
- 4、testFindPersonByLastNameAndFirstName()
- 5、testFindByAddress_City()
- 6、testFindByFamilyList_Name()
一、搭建Redis环境
1、下载Redis安装压缩包
Redis官网与在线教程
- 中文网站:http://www.redis.cn/
- 在线教程:https://www.redis.net.cn/tutorial/3502.html
2、将Redis安装包解压到任意位置
- 解压到D:\Program Files\redis目录
3、配置环境变量
编辑系统变量path
4、启动Redis服务器
启动命令行窗口(cmd),键入redis-server命令
- 报错,寻找解决方法
redis-server.exe redis.windows.conf
windows下redis启动时遇到错误:
D:\redis>redis-server.exe [13732] 29 Nov 11:35:57.446 # Warning: no
config file specified, using the default config. In order to specify a
config file use redis-server.exe /path/to/redis.conf意思是没有默认的conf文件
然后把命令改成:D:\redis>redis-server.exe redis.windows.conf
即可启动。
有时候还会遇到有关端口6379的错误。
只要把redis.windows.conf里面的#bind 127.0.0.1的“#”去掉即可以。
注:是# bind 127.0.0.1 ,不是# bind 127.0.0.1 ::1
D:\redis> 指redis解压到的位置
二、安装Redis可视化管理工具
Redis可视化工具连接Redis服务
在命令行窗口,启动Redis服务
连接服务器
- 打开Redis可视化工具的控制台
三、创建Spring Boot 项目
- 添加依赖
- 设置项目名称与保存位置
四、创建实体类
1、地址实体类
package net.zy.lesson08.bean;import org.springframework.data.redis.core.index.Indexed;//地址实体类 2021.5.17
public class Address {@Indexedprivate String country;@Indexedprivate String city;//有参构造方法public Address(String country, String city) {this.country = country;this.city = city;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}@Overridepublic String toString() {return "Address{" +"country='" + country + '\'' +", city='" + city + '\'' +'}';}
}
2、家庭实体类
package net.zy.lesson08.bean;import org.springframework.data.redis.core.index.Indexed;//家庭实体类 2021.5.17
public class Family {@Indexedprivate String type; //成员类型@Indexedprivate String name;//有参构造方法public Family(String type, String name) {this.type = type;this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Family{" +"type='" + type + '\'' +", name='" + name + '\'' +'}';}
}
3、创建人实体类
package net.zy.lesson08.bean;import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;import java.util.List;//人实体类 2021.5.17
@RedisHash("persons")
public class Person {@Id //主键private String id;@Indexedprivate String firstName; //名@Indexedprivate String lastName; //姓private Address address; //家庭地址private List<Family> familyList ; //家庭成员//有参构造方法public Person(String id, String firstName, String lastName, Address address, List<Family> familyList) {this.id = id;this.firstName = firstName;this.lastName = lastName;this.address = address;this.familyList = familyList;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public List<Family> getFamilyList() {return familyList;}public void setFamilyList(List<Family> familyList) {this.familyList = familyList;}@Overridepublic String toString() {return "Person{" +"id='" + id + '\'' +", firstName='" + firstName + '\'' +", lastName='" + lastName + '\'' +", address=" + address +", familyList=" + familyList +'}';}
}
五、创建自定义Repository接口
package net.zy.lesson08.repository;import net.zy.lesson08.bean.Person;
import org.springframework.data.repository.CrudRepository;//人仓库接口 2021.5.17
public interface PersonRepository extends CrudRepository<Person,String> {}
六、在全局配置文件配置Redis属性
七、编写测试方法
- 注入PersonRepository实例
1、 testAddPerson()
@Testpublic void testAddPerson(){// 添加第一个人 Address address = new Address("中国", "泸州"); Family family1 = new Family("儿子", "张晓刚"); Family family2 = new Family("女儿", "张晓霞"); List<Family> familyList = new ArrayList<Family>(); familyList.add(family1); familyList.add(family2); Person person = new Person("1", "无忌", "张", address, familyList); personRepository.save(person); System.out.println("成功地添加了1条记录");}
- 运行结果
- 再添加3个人
// 添加第二个人 address = new Address("中国", "上海"); family1 = new Family("儿子", "李功晨"); family2 = new Family("女儿", "李晓丽"); familyList = new ArrayList<Family>(); familyList.add(family1); familyList.add(family2); person = new Person("2", "承鹏", "李", address, familyList); personRepository.save(person); // 添加第三个人 address = new Address("中国", "北京"); family1 = new Family("儿子", "唐玉海"); family2 = new Family("女儿", "唐雨涵"); familyList = new ArrayList<Family>(); familyList.add(family1); familyList.add(family2); person = new Person("3", "大明", "唐", address, familyList); personRepository.save(person); // 添加第四个人 address = new Address("中国", "北京"); family1 = new Family("儿子", "张大明"); family2 = new Family("女儿", "张丽丽"); familyList = new ArrayList<Family>(); familyList.add(family1); familyList.add(family2); person = new Person("4", "文勇", "张", address, familyList); personRepository.save(person);
- 运行结果
- 打开Redis可视化工具查看
2、testFindAll()
@Testpublic void testFindAll(){//可迭代器Iterable<Person> persons = personRepository.findAll();persons.forEach((p -> System.out.println(p)) );}
- 运行结果
3、testFindById()
@Testpublic void testFindById(){Optional<Person> person = personRepository.findById("2");System.out.println(person.get());}
- 运行结果
八、测试自定义个性化查询方法
1、在PersonRepository接口定义方法
//自定义个性化查询,方法名需要符合特定的规范public List<Person> findByLastName(String lastName);public Page<Person> findPersonByLastName(String lastName, Pageable pageable);public List<Person> findPersonByLastNameAndFirstName(String lastName, String firstName);List<Person> findByAddress_City(String city);List<Person> findByFamilyList_Name(String name);
2、testFindPersonByLastName()
- 按姓进行分页查找
@Testpublic void testFindPersonByLastName(){Sort.Direction sort = Sort.Direction.DESC;Pageable pageable = PageRequest.of(0,2,sort,"id");Page<Person> page = personRepository.findPersonByLastName("张",pageable);for (Person person:page.getContent()){System.out.println(person);}}
- 运行结果
3、testFindByLastName()
@Testpublic void testFindByLastName(){List<Person> person = personRepository.findByLastName("张");for (Person person1:person){System.out.println(person1);}}
- 运行结果
4、testFindPersonByLastNameAndFirstName()
@Testpublic void testFindPersonByLastNameAndFirstName(){List<Person> person = personRepository.findPersonByLastNameAndFirstName("唐","大明");for (Person person1:person){System.out.println(person1);}}
- 运行结果
5、testFindByAddress_City()
@Testpublic void testFindByAddress_City(){List<Person> person = personRepository.findByAddress_City("北京");for (Person person1:person){System.out.println(person1);}}
- 运行结果
6、testFindByFamilyList_Name()
@Testpublic void testFindByFamilyList_Name(){List<Person> person = personRepository.findByFamilyList_Name("唐雨涵");for (Person person1:person){System.out.println(person1);}}
- 运行结果
Spring Boot 8:Spring Boot整合Redis相关推荐
- spring boot 1.5.4 整合redis、拦截器、过滤器、监听器、静态资源配置(十六)
上一篇:spring boot 1.5.4 整合webService(十五) 1 Spring Boot整合redis和缓存 Spring Boot中除了对常用的关系型数据库提供了优秀的自动 ...
- spring boot 1.5.4 整合 mybatis(十二)
上一篇:spring boot 1.5.4 整合log4j2(十一) Spring Boot集成Mybatis 更多更详细的配置参考文件:application.properties和<Spri ...
- Spring Boot(十三):整合Redis哨兵,集群模式实践
前面的两篇文章(Redis的持久化方案, 一文掌握Redis的三种集群方案)分别介绍了Redis的持久化与集群方案 -- 包括主从复制模式.哨兵模式.Cluster模式,其中主从复制模式由于不能自动做 ...
- spring boot使用Jedis整合Redis
文章目录 spring boot使用jedis整合redis 总结 Spring Boot整合Redis有两种方式,分别是Jedis和RedisTemplate,那么它们二者有什么区别呢? 1.Jed ...
- Spring boot - 整合 Redis缓存(上)
一.配置Pom文件 在使用spring boot 2.0整合redis时遇到了好多问题,网上很多例子都是1.x版本的.故2.0没有折腾好所以将2.0降到了1.5.降级后由于thymeleaf版本也会从 ...
- Spring Boot基础学习笔记08:Spring Boot整合Redis
文章目录 零.学习目标 1.熟悉Redis相关概念 2.掌握使用Spring Boot整合Redis 一.Redis概述 1.Redis简介 2.Redis优点 (1)存取速度快 (2)数据类型丰富 ...
- spring boot连接redis配置127.0.0.1_Java技术分享——Springboot整合redis
springboot提供了spring-data-redis的框架来整合redis的操作.下面主要介绍,springboot整合redis的配置,以及spring-data-redis是如何提供便捷的 ...
- 十一、Spring Boot整合Redis(一)
Spring Boot整合Redis 1. SpringBoot+单例Redis 1)引入依赖 <dependency> <groupId>org.springf ...
- Spring boot整合Redis(入门教程)
目录 源码分析 jedis VS lettuce 整合测试 导入依赖 配置连接 测试 存入字符串 存入对象 五大数据类型操作 自定义RedisConfig 存入对象 Redis工具类(常用API) 以 ...
- Spring Boot整合Redis缓存(Lettuce)
spring-boot-demo-cache-redis 此 demo 主要演示了 Spring Boot 如何整合 redis,操作redis中的数据,并使用redis缓存数据.连接池使用 Lett ...
最新文章
- 洛谷 P1387 最大正方形
- cbitmap 从内存中加载jpg_[转载]windows照片查看器无法显示图片内存不足
- 【linux】学习2
- 系统设置参数说明11
- 练习五十一:序列交换
- 猜数游戏c语言编程while,【游戏编程】猜数字游戏(C语言)
- android sdk 转移_腾讯微博java(android)sdk关系链api详细介绍
- 十年经验教你如何学习嵌入式系统
- java基础[多态基础,多态数组,多态参数方法]
- 智能制造,从smart到intelligent
- JeecgBoot商业版源码下载
- JEECG架构讲解及使用
- 电影影视网站搭建教程
- 凝思操作系统:U盘无法识别和读取问题
- 微信域名防红防屏蔽防封系统,轻松微信推广域名被屏蔽问题
- pygame的游戏窗口退出办法
- 机器学习 数学基础 学习笔记 (1) 导数
- 手机工行显示服务器,工行手机银行服务器安全证书验证失败
- VB查询数据库之结账——机房收费系统总结(五)
- gdb 调试多线程 神贴
热门文章
- 怎么清理C盘释放空间 ?
- 999999数码管显示c语言,定时器1中断动态刷新从999999~0倒计时,数码管只显示有效位的C语言程序怎么编?...
- ftp服务器文件无法删除,ftp服务器文件删除
- 计算机职业规划范文300字,【职业规划300字范文】_职业规划范文300字
- 维盟路由器pppoe服务配置(价值80元的帖子)
- CNTV视频回看下载地址
- iOS 开发者证书打包项目未包含最新的UDID
- 科来数据包生成器使用方法
- 【Excel】取消隐藏没反应
- oc引导windows蓝屏_电脑蓝屏你别怕,黑客教你代码查看问题