目录
1.Redis是什么?
Redis (Remote Dictionary Server) 是一个使用ANST C编写的开源,支持开源,基于内存,可选持久性的键值对存储数据库,也是于开发或者运维都是必须掌握的非关系型数据库。
Redis 作为高性能Key-Value 服务器,拥有多种数据结构,并提供丰富的功能以及对高可用分布式的支持。
Redis 具有以下特点:1.速度快2.支持多种数据结构3.高可用,分布式等。
2.Redis的使用
步骤1)
1.我现在本地是安装了redis 安装步骤我这边就不展示了。在我的计算机-服务里面可以看到redis的服务已经启动好了。
步骤2)
2.在项目中引入redis
<!--SpringBoot中使用默认的redis的客户端-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3.在配置文件中配置redis的连接信息。
我这边用的是MySQL8.0.12 (非必须) 我下面这个例子没用到。
server.port=8006
# MySQL Database
spring.datasource.url=jdbc:mysql://localhost:3306/shiro_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# redis的使用
spring.session.store-type=redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
4. 编写一个简单的实体类 BosUserModel
如果要复制下面这个实体类的话,需要在pom.xml中引入这个jar包。
<!-- spring boot start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.test.model;
import javax.persistence.*;
import java.io.Serializable;
/**
* @Author tanghh
* @Date 2020/4/6 10:35
*/
@Entity
@Table(name = "bos_user", schema = "test3", catalog = "")
public class BosUserModel implements Serializable {
private Integer id;
private String userName;
private String password;
private String isDel="0";
public BosUserModel() {
}
public BosUserModel(Integer id, String userName, String password, String isDel) {
this.id = id;
this.userName = userName;
this.password = password;
this.isDel = isDel;
}
@Override
public String toString() {
return "BosUserModel{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", isDel='" + isDel + '\'' +
'}';
}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "user_name")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Basic
@Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Basic
@Column(name = "is_del")
public String getIsDel() {
return isDel;
}
public void setIsDel(String isDel) {
this.isDel = isDel;
}
}
5. 编写一个TestRedisController
package com.test.controller;
import com.test.service.BosUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author tanghh
* @Date 2020/4/7 14:00
*/
@RestController
public class TestRedisController {
@Autowired
private BosUserService bosUserService;
@ApiOperation(value = "1.1 测试Redis的使用")
@GetMapping(value = "/testRedis")
public void testRedis() {
//redis的使用总结
bosUserService.testRedis();
}
}
6. BosUserService
package com.test.service.impl;
import com.test.model.BosUserModel;
import com.test.service.BosUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
/**
* @Author tanghh
* @Date 2020/4/7 14:05
*/
@Service
public class BosUserServiceImpl implements BosUserService {
@Autowired
private static RedisTemplate<String, Object> redisTemplate;
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
BosUserServiceImpl.redisTemplate = redisTemplate;
}
/**
* 1.测试redis的使用
*/
@Override
public void testRedis() {
BosUserModel bosUserModel = new BosUserModel(1,"soup_tang","123456","0");
//1.将对象保存到redis中
redisTemplate.opsForValue().set("userModel",bosUserModel);
//2.从redis中取出这个对象。
BosUserModel userModel = (BosUserModel) redisTemplate.opsForValue().get("userModel");
System.out.println("用户数据:---"+userModel);
}
}
步骤3)
1.在浏览器上访问一下这个接口。http://localhost:8006/testRedis
2.控制台打印语句。
3. 从上图可以看到我已经将数据保存到了redis中,接下来我们可以通过 RedisDesktopManager 这个软件查看我们刚刚存到redis中的数据。
4. 存取的结果如下图。
3.补充redis的用法
在写这篇博客之前我有在网上做部分功课,发现有的博主用的是 StringRedisTemplate 我这篇博客用的是RedisTemplate,好奇的我看了一下俩者的区别,
俩个点,第一就是 StringRedisTemplate继承RedisTemplate,第二个就是StringRedisTemplate 和 RedisTemplate 俩者的数据是不共通的,StringRedisTemplate默认采用的是String的序列化策略,而RedisTemplate 采用的是JDK的序列化策略。
上面部分我们通过redisTemplate 完整的将一个对象存储到了 redis 中,其实redis不单单可以存储对象,还可以其他内容,下面这部分是小编的补充内容。
3.1 存取String类型的值:
//3.操作字符串
String blogName = "soup_tang";
redisTemplate.opsForValue().set("blogName",blogName);
//4.从redis中取出这个值。
System.out.println("取出来的值-----"+redisTemplate.opsForValue().get("blogName"));
关于RedisTemplate的详细使用,可以参考:https://blog.csdn.net/ruby_one/article/details/79141940
这篇文章就到这里啦,如果觉得小编写的不错的话,不妨给小编一个赞喔 文章中涉及的内容肯定是不全面的,如果你觉得有需要补充的,欢迎评论区留言。