Spring Boot 整合Redis基础篇

开发环境

Spring Boot 2.4.0
Jdk 13
Idea 2020.1
Redis-x64 3.2.100
RedisDesktopManager 2020.4

Redis

redis windows下载:https://github.com/MicrosoftArchive/redis/releases

百度云盘:点击下载

提取码:s2cu

下载完成后直接解压至自定义目录

RedisDesktopManager 

百度云盘:点击下载

提取码:mqj6 

下载完成后打开一路下一步就ok

pom

        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.apache.commons
            commons-pool2
        

RedisConfig

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisTemplate redisTemplate,LettuceConnectionFactory lettuceConnectionFactory) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper()
                .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
                .activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

application.yml

server:
  port: 80
  servlet:
    contest-path: /

spring:
  application:
    name: springboot-redis
  redis:
    hostName: localhost
    port: 6379
    username:
    password:
    lettuce:
      #lettuce连接池
      pool:
        # 连接池最大连接数(使用负值表示没有限制)
        max-active: 32
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: 300
        # 连接池中的最大空闲连接
        max-idle: 16
        # 连接池中的最小空闲连接
        min-idle: 8
      database:
    connect-timeout: 10000

String

RedisTemplate

@SpringBootApplication
@RestController
public class RedisApplication {

    @Resource
    private RedisTemplate redisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }

    @GetMapping("/get/{key}")
    public Object getKey(@PathVariable("key") String key){
        return redisTemplate.opsForValue().get(key);
    }

    @PostMapping("/set")
    public boolean setKey(String key, String v){
        redisTemplate.opsForValue().set(key,v);
        return redisTemplate.hasKey(key);
    }
}

StringRedisTemplate

@SpringBootApplication
@RestController
public class RedisApplication {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }

    @GetMapping("/get/{key}")
    public Object getKey(@PathVariable("key") String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    @PostMapping("/set")
    public boolean setKey(String key, String v){
        stringRedisTemplate.opsForValue().set(key,v);
        return stringRedisTemplate.hasKey(key);
    }
}

测试

启动redis

win + r 输入 cmd

切换至redis安装目录"xxxxx\Redis-x64-3.2.100"

输入"redis-server.exe"回车

或者

直接打开"Redis-x64-3.2.100"文件夹,地址栏输入"cmd"按Enter键

输入"redis-server.exe"回车

 若输入"redis-server.exe"回车报错redis无法启动可以输入"redis-server.exe redis.windows.conf"回车

 generated-requests.http

直接点击下图箭头所示

或者直接创建 generated-requests.http都可以

请求

###
POST http://localhost/set
Content-Type: application/x-www-form-urlencoded

key=admin&v=张三

结果 

POST http://localhost/set

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 24 Nov 2020 09:08:34 GMT
Keep-Alive: timeout=60
Connection: keep-alive

true

Response code: 200; Time: 70ms; Content length: 4 bytes

启动Redis Desktop Manager 2020.4

Spring Boot 整合Redis基础篇_第1张图片

Spring Boot 整合Redis基础篇_第2张图片

请求 

###
GET http://localhost/get/admin

 结果

GET http://localhost/get/admin

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 6
Date: Tue, 24 Nov 2020 09:08:59 GMT
Keep-Alive: timeout=60
Connection: keep-alive

张三

Response code: 200; Time: 544ms; Content length: 2 bytes

基本就如上简述

GitHub:https://github.com/BookshelfBlog/practice/tree/master/springboot-redis

你可能感兴趣的:(#,Spring,Boot,redis,intellij,idea,java,RedisTemplate)