springboot整合redis进行数据操作(一)
2017-10-13 14:33
666 查看
redis是一种常见的nosql,日常开发中,我们使用它的频率比较高,因为它的多种数据接口,很多场景中我们都可以用到,并且redis对分布式这块做的非常好。
springboot整合redis比较简单,并且使用redistemplate可以让我们更加方便的对数据进行操作。
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、在application.properties中加入相关配置
spring.redis.database=0
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=50003、编写配置类
这里定义了两个bean,一个是redisTemplate,另一个是stringRedisTemplate,它们的序列化方式不同,前者默认jdk序列方式,后者默认string的序列化方式,后者一般专门用于存储string格式,前者我们可以用来保存对象等,这里我们都配置上,根据不同业务进行不同使用。
4、编写实体类
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3221700752972709820L;
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
}
5、编写测试service
@Service
public class UserService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void set(String key, User user) {
redisTemplate.opsForValue().set(key, user);
}
public User get(String key) {
return (User) redisTemplate.boundValueOps(key).get();
}
public void setCode(String key, String code) {
stringRedisTemplate.opsForValue().set(key, code, 60, TimeUnit.SECONDS);
}
public String getCode(String key) {
return stringRedisTemplate.boundValueOps(key).get();
}
}
这里我们模拟两种操作,一种是根据key存储user对象,另一种是存储key value均为string的操作,并且赋予数据过期时间,这种操作我们可以用于验证码存储,在setcode方法中,我们存储了一个有效时长为60s的数据,当60s过后,数据会自动销毁。
6、编写测试controller访问
@RestController
@RequestMapping("rest_redis")
public class RedisController {
@Resource
private UserService userService;
@GetMapping("set")
public void set() {
userService.set("key1", new User(1, "meepoguan", 26));
}
@GetMapping("get")
public String get() {
return userService.get("key1").getName();
}
@GetMapping("stringset")
public void stringset() {
userService.setCode("stringkey", "meepoguan_coke");
}
@GetMapping("stringget")
public String stringget() {
return userService.getCode("stringkey");
}
}对service中的方法进行测试。
springboot整合redis比较简单,并且使用redistemplate可以让我们更加方便的对数据进行操作。
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、在application.properties中加入相关配置
spring.redis.database=0
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=50003、编写配置类
import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; @Configuration @EnableCaching public class RedisConfig { @Bean public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) { CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(factory); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(factory); return stringRedisTemplate; } }
这里定义了两个bean,一个是redisTemplate,另一个是stringRedisTemplate,它们的序列化方式不同,前者默认jdk序列方式,后者默认string的序列化方式,后者一般专门用于存储string格式,前者我们可以用来保存对象等,这里我们都配置上,根据不同业务进行不同使用。
4、编写实体类
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3221700752972709820L;
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
}
5、编写测试service
@Service
public class UserService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void set(String key, User user) {
redisTemplate.opsForValue().set(key, user);
}
public User get(String key) {
return (User) redisTemplate.boundValueOps(key).get();
}
public void setCode(String key, String code) {
stringRedisTemplate.opsForValue().set(key, code, 60, TimeUnit.SECONDS);
}
public String getCode(String key) {
return stringRedisTemplate.boundValueOps(key).get();
}
}
这里我们模拟两种操作,一种是根据key存储user对象,另一种是存储key value均为string的操作,并且赋予数据过期时间,这种操作我们可以用于验证码存储,在setcode方法中,我们存储了一个有效时长为60s的数据,当60s过后,数据会自动销毁。
6、编写测试controller访问
@RestController
@RequestMapping("rest_redis")
public class RedisController {
@Resource
private UserService userService;
@GetMapping("set")
public void set() {
userService.set("key1", new User(1, "meepoguan", 26));
}
@GetMapping("get")
public String get() {
return userService.get("key1").getName();
}
@GetMapping("stringset")
public void stringset() {
userService.setCode("stringkey", "meepoguan_coke");
}
@GetMapping("stringget")
public String stringget() {
return userService.getCode("stringkey");
}
}对service中的方法进行测试。
相关文章推荐
- springboot整合redis进行数据操作
- springboot使用protostuff进行序列化和反序列化整合redis的redisTemplate的各种方法的写法
- springboot整合redis进行数据操作(推荐)
- springboot与redis整合案例(下)
- SpringBoot 整合Redis 数据库的方法
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- 【转载】Springboot整合 一 集成 redis
- Spring Boot 整合 Shiro 进行登录认证
- Springboot+redis 整合
- Java爬虫初体验:简单抓取IT之家热评(整合Spring Boot+Elasticsearch+Redis+Mybatis)
- springboot同时整合redis和ehcache
- springboot整合spring-data-redis中的坑
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【二】【整合springSecurity】
- spring boot 整合 redis
- springboot整合Redis
- Spring Boot 整合 Redis 实现缓存操作
- SpringBoot非官方教程 | 第九篇: springboot整合Redis
- Redis---------springboot整合Redis实例
- spring boot整合redis(单机+集群)
- (转)spring boot整合redis