注:示例代码参考《springboot 实战》一书
1、已安装redis服务器
2、添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
3、配置redis,使用默认配置即可
#数据库名称,默认db0 spring.redis.database=0 spring.redis.host=127.0.0.1 #spring.redis.password= spring.redis.port=6379 spring.redis.pool.max-active=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-idle=8 #spring.redis.pool.max-wait=-1 #spring.redis.sentinel.master= #spring.redis.sentinel.nodes= #spring.redis.timeout=
4、配置redis模板
package com.yu.rest_demo.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 { @Bean public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<Object,Object>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om=new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//值的序列化采用Jackson2JsonRedisSerializer redisTemplate.setKeySerializer(new StringRedisSerializer());//键的序列化采用StringRedisSerializer redisTemplate.afterPropertiesSet(); return redisTemplate; } }
5、编写domain(或vo、pojo、po)
package com.yu.rest_demo.vo; import java.io.Serializable; public class RedisVo implements Serializable { private static final long serialVersionUID = -7489358750561796085L; private String name; private Integer age; private String type; public RedisVo(){ super(); } public RedisVo(String name, Integer age, String type) { super(); this.name = name; this.age = age; this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
6、编写dao(当然你也可以放到service里面去做)
package com.yu.rest_demo.dao; import com.yu.rest_demo.vo.RedisVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Repository; import javax.annotation.Resource; @Repository public class RedisDao { @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private RedisTemplate<Object,Object> redisTemplate; @Resource(name = "stringRedisTemplate") ValueOperations<String,String> valOpsStr; @Resource(name = "redisTemplate") ValueOperations<Object,Object> valOps; public void stringRedisTemplateDemo(){ valOpsStr.set("xx","yy"); } public void save(RedisVo redisVo){ valOps.set(redisVo.getName(),redisVo); } public String getString(){ return valOpsStr.get("xx"); } public RedisVo getRedisVo(){ return (RedisVo)valOps.get("redis"); } }
7、编写controller
package com.yu.rest_demo.controller; import com.yu.rest_demo.dao.RedisDao; import com.yu.rest_demo.util.AjaxResponse; import com.yu.rest_demo.vo.RedisVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisConreoller { @Autowired RedisDao redisDao; @RequestMapping("/redis/set") public AjaxResponse set(){ AjaxResponse response = new AjaxResponse(); RedisVo vo = new RedisVo("redis",1,"redis1"); redisDao.save(vo); redisDao.stringRedisTemplateDemo();//放到缓存 response.set("redisVo",vo); response.setResult(true); return response; } @RequestMapping("/redis/getStr") public String getStr(){ return redisDao.getString(); } @RequestMapping("/redis/getRedisVo") public RedisVo getRedisVo(){ return redisDao.getRedisVo(); } }
8、在访问之前先开启redis, AjaxResponse是一个工具类,封装了返回前端的一些固定格式的东西,可以自己替换为别的