1.新建Spring Boot项目
添加spring-boot-starter-data-redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
本文会根据StringRedisTemplate、RedisTemplate这两个模板来介绍,其中StringRedisTemplate继承自RedisTemplate,只能操作键值都是String类型的数据。在实际开发中建议使用RedisTemplate<K,V>。
查询源码可知RedisTemplate默认使用JdkSerializationRedisSerializer序列化,而StringRedisTemplate则使用StringRedisSerializer。
2.配置redis
在application.properties中配置如下:
########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000
3.编写代码
1.创建model
package com.haq.entity; import java.io.Serializable; /**
* Created by on 2017/2/24.
*/
public class User implements Serializable { private static final long serialVersionUId = 1L; private String id;
private String name; public User(){
super();
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
2.创建RedisService
package com.haq.common.redis; 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.Service; import javax.annotation.Resource; /**
* Created by on 2017/3/1.
*/
@Service
public class RedisService { @Autowired
StringRedisTemplate stringRedisTemplate; @Resource(name = "stringRedisTemplate")
ValueOperations<String, String> valOpsStr; @Autowired
RedisTemplate<Object, Object> redisTemplate; @Resource(name = "redisTemplate")
ValueOperations<Object, Object> valOpsObj; /**
* 根据指定key获取String
* @param key
* @return
*/
public String getStr(String key){
return valOpsStr.get(key);
} /**
* 设置Str缓存
* @param key
* @param val
*/
public void setStr(String key, String val){
valOpsStr.set(key,val);
} /**
* 删除指定key
* @param key
*/
public void del(String key){
stringRedisTemplate.delete(key);
} /**
* 根据指定o获取Object
* @param o
* @return
*/
public Object getObj(Object o){
return valOpsObj.get(o);
} /**
* 设置obj缓存
* @param o1
* @param o2
*/
public void setObj(Object o1, Object o2){
valOpsObj.set(o1, o2);
} /**
* 删除Obj缓存
* @param o
*/
public void delObj(Object o){
redisTemplate.delete(o);
} }
3.创建RedisController
package com.haq.web; import com.haq.common.redis.RedisService;
import com.haq.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by on 2017/3/1.
*/
@RestController
@RequestMapping("/redis")
public class RedisController { @Autowired
RedisService redisService; /**
* 设置Str缓存
* @param key
* @param val
* @return
*/
@RequestMapping(value = "setStr")
public String setStr(String key, String val){
try {
redisService.setStr(key, val);
return "success";
} catch (Exception e){
e.printStackTrace();
return "error";
}
} /**
* 根据key查询Str缓存
* @param key
* @return
*/
@RequestMapping(value = "getStr")
public String getStr(String key){
return redisService.getStr(key);
} /**
* 根据key产出Str缓存
* @param key
* @return
*/
@RequestMapping(value = "delStr")
public String delStr(String key){
try {
redisService.del(key);
return "success";
} catch (Exception e){
return "error";
}
} /**
* 设置obj缓存
* @param key
* @param user
* @return
*/
@RequestMapping(value = "setObj")
public String setObj(String key, User user){
try {
redisService.setObj(key, user);
return "success";
} catch (Exception e){
e.printStackTrace();
return "error";
}
} /**
* 获取obj缓存
* @param key
* @return
*/
@RequestMapping(value = "getObj")
public Object getObj(String key){
return redisService.getObj(key);
} /**
* 删除obj缓存
* @param key
* @return
*/
@RequestMapping(value = "delObj")
public Object delObj(String key){
try {
redisService.delObj(key);
return "success";
} catch (Exception e){
e.printStackTrace();
return "error";
}
} }
4.运行测试
1.StringRedisTemplate
http://127.0.0.1:8080/redis/setStr?key=aa&val=name
http://127.0.0.1:8080/redis/getStr?key=aa
http://127.0.0.1:8080/redis/delStr?key=aa
2.RedisTemplate
http://127.0.0.1:8080/redis/setObj?key=aa&id=1&name=test
http://127.0.0.1:8080/redis/getObj?key=aa
http://127.0.0.1:8080/redis/delObj?key=aa
以上地址都能通过测试,在这里就不一一截图了
后记
可以直接使用redistemplate来保存string类型,不过使用redis client看起来就不太直观,因为JdkSerializationRedisSerializer是使用二进制形式存储数据。
最后希望大家有问题能多多指教,O(∩_∩)O谢谢