- pom.xml
<dependencyManagement>
<dependencies>
<!-- 導入SpringBoot需要使用的依賴信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 引入整合Redis所需的場景啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 引入SpringBoot測試的場景啓動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 創建application.yml配置文件:在main/resources目錄下創建application.yml,在這個配置文件中配置Redis連接信息
spring:
redis:
host: 192.168.19.88
port: 6379
- 創建主啓動類
@SpringBootApplication
public class SpringBootMainClass {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainClass.class, args);
}
}
- 創建測試類: 在test文件夾下創建一個SpringBootRedisTest測試類
1)帶泛型的模板類:雖然使用泛型可以指定各種具體數據類型,但是用着不方便。Redis中最基本的數據類型就是字符串。
2)StringRedisTemplate在繼承RedisTemplate時直接指定泛型爲String類型,所有操作都按照字符串處理,使用非常方便
package com.atguigu.spring.boot.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRedisTest {
// 帶泛型的模板類:雖然使用泛型可以指定各種具體數據類型,但是用着不方便。Redis中最基本的數據類型就是字符串。
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
// StringRedisTemplate在繼承RedisTemplate時直接指定泛型爲String類型,所有操作都按照字符串處理,使用非常方便
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void RedisTemplateTest(){
// 1. 根據要操作得類型獲取Operation對象
ValueOperations<Object, Object> operations = redisTemplate.opsForValue();
// 2. 執行操作
operations.set("cat","miaomiao");
}
@Test
public void StringRedisTemplateTest(){
// 1. 根據要操作得類型獲取Operation對象
ValueOperations operations = redisTemplate.opsForValue();
// 2. 執行操作
operations.set("daxiang","fengfeng");
}
}
- 查看結果:一般用StringRedisTemplate較多,RedisTemplate的出來得結果爲序列化後得值
> sudo bin/redis-cli -h 192.168.43.102 -p 6379
key相關操作
string類型操作
先獲取“操作對象”,然後調用“操作對象”的相關方法即可
ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
operations.set("hello", "hello-value");
list類型操作
先獲取“操作對象”,然後調用“操作對象”的相關方法即可
ListOperations<String, String> operations = stringRedisTemplate.opsForList();
List<String> fruitList = operations.range("fruit", 0, -1);
for (String fruit : fruitList) {
System.out.println(fruit);
}
set類型操作
SetOperations<String, String> operations = stringRedisTemplate.opsForSet();
Set<String> animalSet = operations.members("animal");
for (String animal : animalSet) {
System.out.println("animal="+animal);
}
hash類型操作
HashOperations<String, Object, Object> operations = stringRedisTemplate.opsForHash();
Map<Object, Object> studentMap = operations.entries("student");
Set<Map.Entry<Object, Object>> entries = studentMap.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key+"="+value);
}
zset類型操作
ZSetOperations<String, String> operations = stringRedisTemplate.opsForZSet();
Set<ZSetOperations.TypedTuple<String>> chengjiSet = operations.rangeWithScores("chengji", 0, -1);
for (ZSetOperations.TypedTuple<String> stringTypedTuple : chengjiSet) {
String value = stringTypedTuple.getValue();
Double score = stringTypedTuple.getScore();
System.out.println(value+"="+score);
}
基本原理
- SpringBoot的自動化配置包:spring-boot-autoconfigure-2.1.6.RELEASE.jar
- 自動化配置包中的重要配置文件:在META-INF目錄下有一個spring.factories文件
- 重要配置文件中配置了大量自動化配置類:屬性名是:org.springframework.boot.autoconfigure.EnableAutoConfiguration
- Redis的自動化配置類:org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
- 配置項的默認值:RedisProperties類中定義了Redis配置的默認值。
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}