pom配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring data redis 啟動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 創建連接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
redis 安裝
下載:Redis-x64-3.2.100 .msi
點擊安裝 cmd 進入安裝路徑
啟動redis-server.exe redis.windows.conf
依次輸入:
D:\application\redis>redis-cli.exe
127.0.0.1:6379> shutdown
not connected> exit
再次 redis-server.exe redis.windows.conf 啟動
配置:
# Redis數據庫索引(默認為0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=localhost
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.password=
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=-1
# 連接池中的最大空閑連接
spring.redis.jedis.pool.max-idle=8
# 連接池中的最小空閑連接
spring.redis.jedis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=10000
配置redis config 任意取名
package com.zjx.config;
import com.zjx.pojo.User;
import org.springframework.cache.annotation.CachingConfigurationSelector;
import org.springframework.cache.jcache.config.JCacheConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
//實例化字符串
// template.setValueSerializer(new StringRedisSerializer());
//實例化對象
// template.setValueSerializer(new JdkSerializationRedisSerializer());
return template;
}
}
啟動器
package com.zjx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class SpringbootredisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootredisApplication.class, args);
}
}
測試
package com.zjx;
import org.json.JSONObject;
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.test.context.junit4.SpringRunner;
import com.zjx.pojo.*;
import java.util.*;
import java.util.ArrayList;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootredisApplication.class)
public class SpringbootredisApplicationTests {
@Autowired
RedisTemplate<String,Object> redisTemplate;
private User user;
//添加字符串
@Test
public void setSet() {
this.redisTemplate.opsForValue().set("key","value");
}
//獲取字符串
@Test
public void getSet() {
String str = (String)this.redisTemplate.opsForValue().get("key");
System.out.println("你好"+str);
}
//添加對象
@Test
public void setUser(){
this.redisTemplate.opsForValue().set("user",new User("張三","24","上海"));
}
//獲取對象
@Test
public void getUser(){
System.out.println((User)this.redisTemplate.opsForValue().get("user"));
}
}
以上是自學總結。有借鑒前輩帖子望見諒。不足之處請多多指教