1、添加依赖
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
2、创建测试类
package com.bw;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.bw.domain.TbRole;
import com.bw.service.TbRoleService;
import com.bw.utils.R;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author 军哥
* @version 1.0
* @description: TODO
* @date 2024/7/30 13:34
*/
@SpringJUnitConfig(locations = "classpath:conf/spring.xml")
public class TestRedis {
@Autowired
TbRoleService tbRoleService;
@Autowired
RedisTemplate redisTemplate;
@Test
public void testRedis() {
//--1
Long times = redisTemplate.opsForValue().increment("times");
//--2
List<TbRole> list = tbRoleService.list();
redisTemplate.opsForList().leftPushAll("role:list", list);
Long size = redisTemplate.opsForList().size("role:list");
System.out.println("list size:" + size);
// 修改
TbRole tbRole = (TbRole)redisTemplate.opsForList().index("role:list", 2);
tbRole.setRoleName("aaaaaaa");
redisTemplate.opsForList().set("role:list", 2, tbRole);
System.out.println(redisTemplate.opsForList().index("role:list", 2));
//--3
redisTemplate.opsForValue().set("code", "12345", 1, TimeUnit.MINUTES);
String code = (String) redisTemplate.opsForValue().get("code");
if(code != null) {
System.out.println("code=" + code);
}
}
@Test
public void testAes() {
// 生产密钥
byte[] encoded = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
// 加密
AES aes = SecureUtil.aes(encoded);
byte[] encrypt = aes.encrypt("hello,world");
// 解密
byte[] decrypt = aes.decrypt(encrypt);
String s = new String(decrypt);
System.out.println(s);
}
}