引言:
本文主要分享了Redis如何在IDEA中部署,运行;模拟加入Redis的操作;
目录结构
1. 在IDEA中搭建Redis
1.1 创建项目
新建含有web的SpringBoot项目;
搭建项目参考:SpringBoot超详细笔记:https://blog.csdn.net/weixin_42601136/article/details/108396511
1.2 添加依赖(不够手动添加)
redis.clients
jedis
2.9.0
junit
junit
4.12
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.2
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
1.3 测试环境是否搭建成功
@Test
public void test0() {
//连接Redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
//获取kak
String kaka = jedis.get("kak");
log.info(kaka);
//创建datex
String datex = jedis.set("datex", new Date().toString());
//获取datex
String datex1 = jedis.get("datex");
log.info(datex1);
//关闭资源
jedis.close();
}
1.4 利用json存储数据
@Test
public void test1() {
JedisPool pool = new JedisPool("127.0.0.1", 6379);
Jedis jedis = pool.getResource();
Student student = new Student(1001, "kak", "man", "20");
String key = "student";
String field = "1001";
String s = JsonUtils.objectToJson(student);
String value = s;
jedis.hset(key, field, value);
String hget = jedis.hget(key, field);
log.info(hget);
jedis.close();
}
1.5 以byte的形式存储对象
@Test
public void test02() {
//连接redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
String key = "Student";
Student value = new Student(1002, "taotao", "woman", "20");
//将key和value转换为byte[]
byte[] byteKey = SerializationUtils.serialize(key);
byte[] byteValue = SerializationUtils.serialize(value);
//将key和value存储到redis中
jedis.set(byteKey, byteValue);
//获取value值
byte[] bytes = jedis.get(byteKey);
//bytes反序列化为Student对象
Student stu = (Student) SerializationUtils.deserialize(bytes);
System.out.println(stu);
jedis.close();
}
1.6 加入连接池的操作
使用连接池操作,可以避免频繁的创建和销毁连接对象的消耗资源
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
poolConfig.setMaxIdle(10);//最大空闲数
poolConfig.setMinIdle(5);//最小空闲数
poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时
//创建连接池
JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);
//通过连接池获取Jedis对象
Jedis resource = pool.getResource();
//获取上面添加的datex
String value = resource.get("datex");
log.info(value);
resource.close();
}
1.7 管道操作
Redis的管道操作,执行一个命令需要先发送请求到Redis,需要经历网络延迟,Redis还需给客户端一个响应;如果需要一次操作多个命令,可以通过管道,将命令放到客户端的一个Pipeline中,之后一次将命令发送到服务端,服务端一次性返回到客户端;
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
poolConfig.setMaxIdle(10);//最大空闲数
poolConfig.setMinIdle(5);//最小空闲数
poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时
//创建连接池
JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);
//通过连接池获取Jedis对象
Jedis resource = pool.getResource();
//获取上面添加的datex
String value = resource.get("datex");
log.info(value);
resource.close();
}
2. 从数据中查询数据
场景:
将数据存入Redis中,避免访问量过大,造成数据库奔溃;
2.1 生成实体类
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
poolConfig.setMaxIdle(10);//最大空闲数
poolConfig.setMinIdle(5);//最小空闲数
poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时
//创建连接池
JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);
//通过连接池获取Jedis对象
Jedis resource = pool.getResource();
//获取上面添加的datex
String value = resource.get("datex");
log.info(value);
resource.close();
}
2.2 生成实体模板类
package com.sx.kak.po;
import java.util.ArrayList;
import java.util.List;
public class StudentExample {
protected String orderByClause;
protected boolean distinct;
protected List oredCriteria;
public StudentExample() {
oredCriteria = new ArrayList();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List getAllCriteria() {
return criteria;
}
public List getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andIdIsNull() {
addCriterion("id is null");