springboot 整合springdataredis

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"));
}

}
以上是自学总结。有借鉴前辈帖子望见谅。不足之处请多多指教 


posted @ 2019-07-09 14:37  易行舟  阅读(2029)  评论(0编辑  收藏  举报