redis中文官網:http://www.redis.cn/
redis-windows安裝下載:https://github.com/tporadowski/redis/releases
redis-linux下載:https://github.com/redis/redis/tags
接下來正式實作整合,在springboot中使用redis
1,創建springboot專案
2,需要的依賴
jedis,同時需要使用下面的fastjson
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
fastjson
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
也可以去官網中下載其他版本
3,application.properties配置
#配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
4,在專案加載完之后,可以分析一波原始碼
在外部庫中就能發現這個redis已經加載進來

點進去后可以發現,在每個對應的自動配置類里面,都有對應的組態檔

在點開組態檔類中就可以發現,redis基本操作就在這個對應的組態檔中了
如一些使用的默認資料庫為第0個,url,主機等,不過實際開發中一般在linux下,并且這些命令一般不會用代碼實作,而是使用組態檔,在組態檔中修改這些指令

5,進行測驗,看是否可以使用redis
package com.example.bootredis;
import com.example.bootredis.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class BootredisApplicationTests {
@Autowired
//@Qualifier("redisTemplate")
//重新撰寫的模板
private RedisTemplate redisTemplate;
long s1 = System.currentTimeMillis();
@Test
void contextLoads() {
//opsForValue:操作字串
//opsForList:操作list
//set,hash,geo,zset...等
//redisTemplate可以操作我們的常用方法
//獲取redis的連接物件
//RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
redisTemplate.opsForValue().set("name","ZhengHuiSheng");
String name = (String)redisTemplate.opsForValue().get("name");
long s2 = System.currentTimeMillis();
System.out.println(name);
System.out.println(s2 - s1);
}
// @Test
// public void test(){
// //在開發中一般都使用json來傳遞物件
// User user = new User("鄭輝盛",18);
// //向redis中添加資料
// redisTemplate.opsForValue().set("user",user);
// System.out.println(redisTemplate.opsForValue().get("user"));
// }
}
6,測驗發現并不能成功
原因:沒有在我們安裝在本地的redis下啟動redis-server.exe以及redis-cli.exe,安裝連接在一開始就已經說了,
這里再說一下:https://github.com/tporadowski/redis/releases
安裝好之后也不用配置環境,主機和埠默認為127.0.0.1 6379

7,在windows中測驗一下,依次打開redis-server.exe和redis-cli.exe,在redis-cli.exe中輸入ping,如果能得到pong,name就連接成功,本地redis就可以正常使用了

8,重新進行測驗,專案就能運行成功了,可以自己試著debug一下,這樣才能知道程式干了什么,以及快速進行排錯
9,再保存一個物件試試
新建一個pojo包,下面建一個User類,一般開發中物體類都是需要進行序列化的
package com.example.bootredis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private String name;
private Integer age;
}
10,再進行測驗,加入到上一段測驗代碼中
@Test
public void test(){
//在開發中一般都使用json來傳遞物件
User user = new User("鄭輝盛",18);
//向redis中添加資料
redisTemplate.opsForValue().set("user",user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
11,然而物體類序列化只是在jdk中序列化,在我們進行物件保存時可能使用json進行序列化,因此在的查詢設定的物件可以發現一串亂碼,原因是在RedisTemplate類中并未進行序列化處理

在原始碼中可以發現并未進行序列化,序列化默認為jdk序列化


12,新建一個config包,下面新建一個RedisConfig配置類,相當于自定義一個模板配置類
package com.example.bootredis.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
@Configuration
public class RedisConfig {
//自定義了一個RedisTemplate
@Bean
@SuppressWarnings("all") //告訴編譯器忽略指定的警告,不用在編譯完成后出現警告資訊
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//為了自己開發方便,使用String,Object型別
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//序列化配置,使用json決議任意的物件,將物件決議成可以序列化的物件
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//使用Mapper物件進行轉義
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);
//value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value序列化方式采用jackson
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
13,先在redis中flushdb(清空)一下資料庫,在重新進行測驗,那么使用的模板配置類肯定就要使用自定義配置的模板類了,終極測驗
@Autowired
@Qualifier("redisTemplate")
//重新撰寫的模板
private RedisTemplate redisTemplate;
@Test
public void test(){
//在開發中一般都使用json來傳遞物件
User user = new User("鄭輝盛",18);
//向redis中添加資料
redisTemplate.opsForValue().set("user",user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
14,結果 user獲取成功

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243308.html
標籤:java
上一篇:Java基本語法
