一、Maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- 集成commons工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- 集成lombok 框架 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<!-- SpringBoot-整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
二、工具类
package com.demo.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @Author: JYC
* @Title: RedisTemplateUtils
* @Description: TODO
* @Date: 2022/4/28 10:45
*/
@Component
public class RedisTemplateUtils {
@Resource
private RedisTemplate<String, Object> redisTemplate;
public void setObject(String key, Object object) {
setObject(key, object, null);
}
public void setObject(String key, Object object, Long timeOut) {
redisTemplate.opsForValue().set(key, object);
if (timeOut != null) {
redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);
}
}
public Object getObject(String key) {
return redisTemplate.opsForValue().get(key);
}
}
三、控制层
package com.demo.controller;
import com.alibaba.fastjson.JSONObject;
import com.demo.entity.UserEntity;
import com.demo.utils.RedisTemplateUtils;
import com.demo.utils.RedisUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: JYC
* @Title: RedisController
* @Description: TODO
* @Date: 2022/4/27 16:34
*/
@RestController
public class RedisController {
@Autowired
private RedisUtils redisUtils;
@Autowired
private RedisTemplateUtils redisTemplateUtils;
@RequestMapping("/addUser")
public String addUser(UserEntity userEntity) {
// 1.将我们的对象转换成json
// String json = JSONObject.toJSONString(userEntity);
// redisUtils.setString("userEntity", json);
redisTemplateUtils.setObject("userEntity", userEntity);
return "存储成功!";
}
@RequestMapping("/getUser")
public UserEntity getUser(String key) {
// String json = redisUtils.getString(key);
// UserEntity userEntity = JSONObject.parseObject(json, UserEntity.class);
return (UserEntity) redisTemplateUtils.getObject(key);
}
}
四、实体类
package com.demo.entity;
import lombok.Data;
import java.io.Serializable;
/**
* @Author: JYC
* @Title: userEntity
* @Description: TODO
* @Date: 2022/4/27 16:33
*/
@Data
public class UserEntity implements Serializable {
private Long userId;
private String userName;
}
五、配置文件
spring:
redis:
host: 192.168.1.100
password: root
port: 6379
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/131243.html