springBoot项目中使用 redis

1.引入依赖

        <!-- Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Redis 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.配置

文件path: /项目目录/src/main/resources/application.properties

# Redis库的编号
spring.redis.database=0
# Redis实例地址
spring.redis.host=127.0.0.1
# Redis实例端口号,默认6379
spring.redis.port=6379
# Redis登录密码
#spring.redis.password=Easy@0122
# Redis连接池最大连接数
spring.redis.jedis.pool.max-active=10
# Redis连接池最大空闲连接数
spring.redis.jedis.pool.max-idle=10
# Redis连接池最小空闲连接数
spring.redis.jedis.pool.min-idle=0

等同于 文件path:/项目目录/src/main/resources/application.yml

spring:
  redis:
    database: 0           # Redis库的编号
    host: 127.0.0.1       # Redis实例地址
    port: 6379            # Redis实例端口号,默认6379
    password: Easy@0122    # Redis登录密码
    jedis:
      pool:
        max-active: 10    # Redis连接池最大连接数
        max-idle: 10      # Redis连接池最大空闲连接数
        min-idle: 0       # Redis连接池最小空闲连接数

3.序列化配置

文件path: /项目目录/src/main/java/com/example/demo/config/RedisConfig.java

package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();

        // 设置key和hashKey的序列化器
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        // 设置value和hashValue的序列化器
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);

        // return  template
        return template;
    }
}

4.如何使用

文件path: /项目目录/src/main/java/com/example/demo/controller/HomeController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;

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


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;


import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HomeController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/")
    public String home() {
        return "Hello World";
    }

    @GetMapping("/set")
    public String set() {
        Map<String, Object> user = new HashMap<>();
        user.put("name", "小明");
        user.put("enName", "xiao" + "ming");
        user.put("age", 18);

        redisTemplate.opsForValue().set("aaa", "aaa" + "aaa");
        redisTemplate.opsForValue().set("bbb", "bbb" + "bbb");
        redisTemplate.opsForValue().set("ccc", user);
        return "set";
    }

    @GetMapping("/get")
    public String get(HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject entries = new JSONObject();
        entries.set("aaa", redisTemplate.opsForValue().get("aaa"));
        entries.set("bbb", redisTemplate.opsForValue().get("bbb"));
        entries.set("ccc", redisTemplate.opsForValue().get("ccc"));
        return JSONUtil.toJsonStr(entries);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容