1.下载redis,启动服务端
下载redis windows版本,打开服务端:redis-server.exe
2.application.properties redis连接配置
#redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait= -1ms
spring.redis.jedis.pool.max-idle=500
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.lettuce=
spring.redis.lettuce.shutdown-timeout=0ms
这里注意,刚下载的redis,默认是没有设置密码的,所以要配置成:spring.redis.password= ,否则最后就会报错:
JedisConnectionException: Could not get a resource from the pool
3.pom.xml 相关依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.6.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
4.Redis配置类
package com.springboot.config;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
}
@Bean
public RedisTemplate<?, ?> getRedisTemplate() {
JedisConnectionFactory factory = getConnectionFactory();
RedisTemplate<?, ?> template = new StringRedisTemplate(factory);
return template;
}
}
这个配置类,spring会自己去加到spring容器中,不需要再
//@Import({RedisConfig.class})
5.redis常用方法
package com.springboot.service.redis;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
@Resource
private RedisTemplate<String, ?> redisTemplate;
public boolean set(final String key, final String value) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
public boolean remove(final String key) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.del(key.getBytes());
return true;
}
});
return result;
}
}
6.测试类
package com.springboot;
import com.alibaba.fastjson.JSONObject;
import com.springboot.service.redis.RedisService;
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.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests {
private JSONObject json = new JSONObject();
@Autowired
private RedisService redisService;
@Test
public void contextLoads() throws Exception {
}
/**
* 插入字符串
*/
@Test
public void setString() {
redisService.set("redis_string_test", "springboot redis test");
}
/**
* 获取字符串
*/
@Test
public void getString() {
String result = redisService.get("redis_string_test");
System.out.println(result);
}
/**
* 插入对象
*/
@Test
public void setObject() {
Person person = new Person("person", "male");
redisService.set("redis_obj_test", json.toJSONString(person));
}
/**
* 获取对象
*/
@Test
public void getObject() {
String result = redisService.get("redis_obj_test");
Person person = json.parseObject(result, Person.class);
System.out.println(json.toJSONString(person));
}
/**
* 插入对象List
*/
@Test
public void setList() {
Person person1 = new Person("person1", "male");
Person person2 = new Person("person2", "female");
Person person3 = new Person("person3", "male");
List<Person> list = new ArrayList<>();
list.add(person1);
list.add(person2);
list.add(person3);
redisService.set("redis_list_test", json.toJSONString(list));
}
/**
* 获取list
*/
@Test
public void getList() {
String result = redisService.get("redis_list_test");
List<String> list = json.parseArray(result, String.class);
System.out.println(list);
}
@Test
public void remove() {
redisService.remove("redis_test");
}
}
class Person {
private String name;
private String sex;
public Person() {
}
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
注意:貌似类名一定要是SpringbootApplicationTests
执行结果:
打开redis客户端,可以看到已经有值了。