一、连接Jedis
1、添加依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
@Test
void contextLoads() {
// new Jedis即可
Jedis jedis = new Jedis("127.0.0.1",6379);
// jedis所有的命令都是之前 redis的命令
System.out.println(jedis.ping());
}
二、Jedis的操作?
1、List类
@Test
void List(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
//存储数据到列表中
jedis.lpush("site-list", "Runoob");
jedis.lpush("site-list", "Google");
jedis.lpush("site-list", "Taobao");
// 获取存储的数据并输出
List<String> list = jedis.lrange("site-list", 0 ,2);
for(int i=0; i<list.size(); i++) {
System.out.println("列表项为: " + list.get(i));
}
}
2、Keys类
@Test
void Keys(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
// 获取数据并输出
Set<String> keys = jedis.keys("*");
Iterator<String> it=keys.iterator() ;
while(it.hasNext()){
String key = it.next();
System.out.println(key);
}
?
?3、String类
@Test
void String(){
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("连接成功");
//设置 redis 字符串数据
jedis.set("runoobkey", "www.runoob.com");
// 获取存储的数据并输出
System.out.println("redis 存储的字符串为: "+ jedis.get("runoobkey"));
}
?4、事务
@Test
void Transaction(){
Jedis jedis = new Jedis("127.0.0.1");
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name","zhangsan");
String result = jsonObject.toJSONString();
System.out.println("字符串后:"+result);
// 开启事务
Transaction multi = jedis.multi();
try {
multi.set("user1",result);
multi.set("user2",result);
multi.exec();// 执行事务
}catch (Exception e){
multi.discard(); // 取消事务
e.printStackTrace();
}finally {
jedis.close();
}
}
二、SpringBoot集成Redis
?1、创建项目
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置文件
# 配置Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
3、测试
?
package com.gg;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class RedisSpringBoot02ApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
/*
opsForValue:操作字符串
opsForList:操作List
opsForSet:操作Set
.....
*/
redisTemplate.opsForValue().set("name","张三");
System.out.println(redisTemplate.opsForValue().get("name"));
}
}
问题:中文在终端一串变成字符
?查看 redisAutoConfigration 配置类??
测试执行
@Test
void test() throws JsonProcessingException {
User user = new User("张三", 18);
String jsonUser = new ObjectMapper().writeValueAsString(user);
redisTemplate.opsForValue().set("user",jsonUser);
System.out.println(redisTemplate.opsForValue().get("user"));
}
?执行结果
解决方案:
package com.gg.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User implements Serializable {
private String name;
private Integer age;
}
测试执行依旧成功
?自定义配置类
package com.gg.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.net.UnknownHostException;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// Key采用String的序列化
template.setKeySerializer(stringRedisSerializer);
// hash采用String的序列化
template.setHashKeySerializer(stringRedisSerializer);
// hash的value采用String的序列化
template.setHashValueSerializer(stringRedisSerializer);
// value采用String的序列化
template.setValueSerializer(stringRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
执行后:此时编程了正常的字符串
?
|