需求:查询酒店房间列表
1、引入依赖
<!--redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
2、配置yml文件
server:port: 8083
spring:application:name: room-serviceredis:host: 120.27.149.6port: 6379password: redistimeout: 10000database: 0
3、实体类
注意:redis要求存入的对象必须序列化,不然会报没有序列化的错误。所以需要继承Serializable 接口序列化
package com.hmall.common.pojo;import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;import java.io.Serializable;/*** <p>* 房间表* </p>** @author itcast* @since 2023-08-25*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Room implements Serializable {private static final long serialVersionUID = 1L;/*** 主键id*/private Long id;/*** 地址*/private String address;/*** 房型
1:酒店
2:民宿*/private Integer type;/*** 楼栋*/private String loudong;/*** 房间号*/private Integer number;/*** 床位号*/private Long bed;/*** 入住状态
1:未入住
2:入住中*/private Integer status;/*** 房间人员关系表id*/private Integer userRomId;/*** 房间价格*/private Double price;/*** 损耗状态
1:未损耗
2:有损耗*/private Integer wastageStatus;}
4、业务层使用
@Autowiredprivate RedisTemplate redisTemplate;/*查询房间列表*/@Overridepublic List<Room> getRoomList(Integer type, Integer status) {List<Room> list = null;String keyId = "room_" + type + "_" + status;//查找redis缓存list = (List<Room>) redisTemplate.opsForValue().get(keyId);if (list != null) {return list;}LambdaQueryWrapper<Room> lwq = new LambdaQueryWrapper<>();if (status != 0) {lwq.eq(Room::getStatus, status);}if (type != 0) {lwq.eq(Room::getType, type);}list = list(lwq);System.out.println("list:" +list);//存储数据到redis,过期时间50分钟redisTemplate.opsForValue().set(keyId, list, 50, TimeUnit.MINUTES);return list;}/*新增房间*/@Overridepublic void add(Room room) {String keyId = "room_" + room.getType() + "_" + room.getStatus();//删除redis缓存redisTemplate.delete(keyId);//保存数据save(room);}