redis教程(二)——spring boot整合redis

                             spring boot整合redis

1、普通字符串存储

(1)pom文件中引入redis依赖的jar

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

(2)在application中添加redis配置

spring:

redis:

host: 10.18.101.141

port: 6379

(3)编写restful进行添加和查询操作

mport org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.web.bind.annotation.*;

 

@RestController

@RequestMapping(value = "/redis")

public class RedisStringController {

 

@Autowired

private StringRedisTemplate stringRedisTemplate;

 

@PutMapping(value = "/put")

public void put(String key, @RequestParam(required = false, defaultValue = "default") String value) {

stringRedisTemplate.opsForValue().set(key, value);

}

 

@GetMapping(value = "/get")

public Object get(String key) {

return stringRedisTemplate.opsForValue().get(key);

}

}

(4)使用postman进行接口测试

使用postman送请求:

put方式插入数据 localhost:8080/redis/put?key=hello&value=world

get方式获取数据 localhost:8080/string/get?key=hello

 

2、对象存储

(1)在pom文件中再引入序列化包,这里我们使用jackson

<!-- jackson 序列化包-->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-core</artifactId>

<version>2.9.5</version>

</dependency>

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.9.5</version>

</dependency>

(2)添加redis的配置类

/**

* 完成redis整合配置

*/

@Configuration

public class RedisConfig {

 

@Bean

public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

 

RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值

redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

 

//使用StringRedisSerializer来序列化和反序列化redis的ke

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setHashKeySerializer(new StringRedisSerializer());

 

//开启事务

redisTemplate.setEnableTransactionSupport(true);

redisTemplate.setConnectionFactory(redisConnectionFactory);

 

return redisTemplate;

}

}

(3)编写对象类users

public class Users {

private Integer id;

private String name;

private String age;

 

public Users(Integer id, String name, String age) {

this.id = id;

this.name = name;

this.age = age;

}

public Users() {

super();

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

 

public String getAge() {

return age;

}

 

public void setAge(String age) {

this.age = age;

}

 

}

(4)编写接口类进行操作

@RestController

@RequestMapping(value = "/object")

public class RedisObjectController {

 

@Autowired

private RedisTemplate<String, Object> redisTemplate;

 

@GetMapping("/get/{id}")

public Object get(@PathVariable String id) {

return redisTemplate.opsForValue().get(id);

}

 

@PutMapping("/put")

public void put(int id,String name, String age) {

Users user = new Users(id,name, age);

redisTemplate.opsForValue().set(id+"", user);

}

 

}

3、spring boot整合redis自动化配置原理解析

(1)spring boot通过application.yml配置加载redis配置

(2)解析配置分装到RedisProperties这个对象中

 

(3)根据@ConditionalOnClass判断使用哪个Redis客户端,封装成LettuceClientConfiguration并创建LettuceConnectionFactory

(4)通过@Bean创建我们自己的配置类在LettuceConnectionFactory基础上添加我们自己自定义的配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值