引入依赖
<!--spring整合redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
package com.jt.test;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.params.SetParams;
public class TestRedis {
/**
* 1.spring整合redis
* 报错说明:
* 1).如果测试过程中报错 则检查redis配置文件 改3处
* 2).检查redis启动方式 redis-server redis.conf
* 3).检查Linux的防火墙
* 做完的 测试其他命令.
*/
@Test
public void testString01() {
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
//2.操作redis
jedis.set("a", "redis入门案例");
String value = jedis.get("a");
System.out.println(value);
}
@Test
public void testString02() {
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
//2.判断当前数据是否存在
if(jedis.exists("a")) {
System.out.println(jedis.get("a"));
}else {
jedis.set("a", "测试是否存在的方法");
}
}
/**
* 1.能否简化是否存在的判断
* 2.如果该数据不存在时修改数据,否则不修改
* setnx方法: 只有当数据不存在时赋值.
*/
@Test
public void testString03() {
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll(); //清空所有的redis缓存
jedis.setnx("a", "测试setnx方法1");
jedis.setnx("a", "测试setnx方法2");
System.out.println(jedis.get("a"));
}
/**
* 为数据添加超时时间
* @throws InterruptedException
* setex方法 保证赋值操作和添加超时时间的操作的原子性
* 原子性: 要么同时成功,要么同时失败(类似事务)
*/
@Test
public void testString04() throws InterruptedException {
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll(); //清空所有的redis缓存
jedis.set("a", "aaaa"); //如果程序报错,则超时方法将不会执行,改数据将永不超时
//程序报错,意外终止!!!!!!!
jedis.expire("a", 20); //添加超时时间 不是原子性操作
Thread.sleep(2000);
System.out.println("剩余存活时间:"+jedis.ttl("a"));
//2.实现原子性操作
jedis.setex("b", 20, "原子性测试");
System.out.println(jedis.get("b"));
}
/**
*
* 1.只有数据不存在时允许修改
* 2.要求实现添加超时时间,并且是原子性操作
* SetParams 参数说明:
* 1.NX 只有key不存在时才能修改
* 2.XX 只有key存在时,才能修改
* 3.PX 添加的时间单位是毫秒
* 4.EX 添加的时间单位是秒
*/
@Test
public void testString05(){
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll(); //清空所有的redis缓存
SetParams params = new SetParams();
params.xx().ex(20);
jedis.set("aa", "测试A", params);
jedis.set("aa", "测试B", params);
System.out.println(jedis.get("aa"));
}
/**
* 存储一类数据时,可以使用hash.
*/
@Test
public void testHASH(){
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll(); //清空所有的redis缓存
jedis.hset("user", "name", "tomcat");
jedis.hset("user", "id", "100");
System.out.println(jedis.hgetAll("user"));
}
@Test
public void testList(){
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll(); //清空所有的redis缓存
jedis.lpush("list", "1","2","3","4");
System.out.println(jedis.rpop("list"));
}
//控制事务
@Test
public void testTx(){
//1.创建jedis对象
Jedis jedis = new Jedis("192.168.126.129", 6379);
jedis.flushAll(); //清空所有的redis缓存
Transaction transaction = jedis.multi(); //2.开启事务
try {
transaction.set("aaa", "aaa");
transaction.set("bbb", "bbbbb");
transaction.set("ccc", "cccccc");
transaction.exec(); //事务提交
} catch (Exception e) {
e.printStackTrace();
transaction.discard(); //事务回滚
}
}
}
Spring整合Redis
编辑配置文件
redis.host=192.168.126.129
redis.port=6379
//配置类
@Configuration //我是一个配置类 一般都会与@Bean联用
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
//将返回值的结果交给spring容器进行管理,如果以后想要使用该对象则可以直接注入.
@Bean
public Jedis jedis() {
return new Jedis(host, port);
}
}
json格式转化问题
String< --> json< -->实体对象
//这是一个小测试类
import java.util.Date;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestObjectMapper {
//改对象就是工具api 有且只有一份即可.并且不允许别人修改.
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 目的: 实现对象与json串之间的转化
* 步骤1: 将对象转化为json
* 步骤2: 将json转化为对象
* 利用ObjectMapper 工具API实现
* @throws JsonProcessingException
*/
@Test
public void test01() throws JsonProcessingException {
ItemDesc itemDesc = new ItemDesc();
itemDesc.setItemId(101L).setItemDesc("json转化测试")
.setCreated(new Date()).setUpdated(itemDesc.getCreated());
//1.将对象转化为JSON 调用的是对象的get方法
String json = MAPPER.writeValueAsString(itemDesc);
System.out.println(json);
//2.将json转化为对象 传递需要转化之后的class类型 调用是对象的set方法
ItemDesc itemDesc2 = MAPPER.readValue(json, ItemDesc.class);
System.out.println(itemDesc2.getItemDesc());
}
}
把他封装成工具API
package com.jt.util;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperUtil {
//json与对象的转化 优化异常处理
private static final ObjectMapper MAPPER = new ObjectMapper();
//1.将对象转化为JSON
public static String toJSON(Object target) {
if(target == null) {
throw new NullPointerException("taget数据为null");
}
try {
return MAPPER.writeValueAsString(target);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException(e); //如果转化过程中有问题则直接抛出异常
}
}
//2. 将json串转化为对象 用户传递什么样的类型,就返回什么样的对象!!!
// <T> 定义了一个泛型对象 代表任意类型
public static <T> T toObject(String json,Class<T> targetClass) {
if(StringUtils.isEmpty(json) || targetClass == null) {
throw new NullPointerException("参数不能为null");
}
try {
return MAPPER.readValue(json, targetClass);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}