• springboot整合redis


    一、引入maven依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.apache.commonsgroupId>
    7. <artifactId>commons-pool2artifactId>
    8. <version>2.11.1version>
    9. dependency>

    二、配置application

    1. redis:
    2. host: localhost
    3. port: 6379
    4. database: 0
    5. timeout: 1000
    6. jedis:
    7. pool:
    8. max-active: 8
    9. min-idle: 5
    10. max-wait: -1

    三、redis的set、get

    (1)RedisCtroller

    1. package com.mgx.controller;
    2. import com.mgx.service.RedisService;
    3. import org.apache.ibatis.annotations.Param;
    4. import org.springframework.web.bind.annotation.*;
    5. import javax.annotation.Resource;
    6. /**
    7. * @author mgx
    8. * @date 2023/9/17 2:35 PM
    9. */
    10. @RestController
    11. @RequestMapping("/redis")
    12. public class RedisController {
    13. @Resource
    14. private RedisService redisService;
    15. @PostMapping("/add")
    16. public String add(@Param("key") String key, @Param("value")String value) {
    17. return redisService.add(key,value);
    18. }
    19. @GetMapping("/get")
    20. public Object getUserInfo(@Param("key") String key) {
    21. return redisService.get(key);
    22. }
    23. }

    RedisService

    1. package com.mgx.service;
    2. /**
    3. * @author mgx
    4. * @date 2023/9/17 2:36 PM
    5. */
    6. public interface RedisService {
    7. String add(String key,String value);
    8. Object get(String key);
    9. }

    RedisServiceImpl

    1. package com.mgx.service.impl;
    2. import com.mgx.service.RedisService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.data.redis.core.RedisTemplate;
    5. import org.springframework.stereotype.Service;
    6. /**
    7. * @author mgx
    8. * @date 2023/9/17 2:37 PM
    9. */
    10. @Service
    11. public class RedisServiceImpl implements RedisService {
    12. @Autowired
    13. private RedisTemplate redisTemplate;
    14. @Override
    15. public String add(String key, String value) {
    16. redisTemplate.opsForValue().set(key,value);
    17. return "添加成功";
    18. }
    19. @Override
    20. public Object get(String key) {
    21. return redisTemplate.opsForValue().get(key);
    22. }
    23. }

    (2)使用postman接口调用

     

     四、redis操作成功,但这个时候发现redis库中的key和value乱码,后期会不利于我们的维护。查看redisTemplate源码,我们发现是redis序列化的问题,因此需要对redis进行序列化指定。

    1. package com.mgx.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.data.redis.connection.RedisConnectionFactory;
    5. import org.springframework.data.redis.core.RedisTemplate;
    6. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    7. import org.springframework.data.redis.serializer.StringRedisSerializer;
    8. /**
    9. * @author mgx
    10. * @date 2023/9/17 3:09 PM
    11. */
    12. @Configuration
    13. public class RedisConfig {
    14. @Bean
    15. public RedisTemplate redisTemplate(RedisConnectionFactory factory){
    16. RedisTemplate redisTemplate = new RedisTemplate<>();
    17. redisTemplate.setConnectionFactory(factory);
    18. //指定kv的序列化方式
    19. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    20. redisTemplate.setKeySerializer(new StringRedisSerializer());
    21. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    22. return redisTemplate;
    23. }
    24. }

    添加RedisConfig重启,调用接口,redis可视化界面已没有乱码

    五、项目结构

  • 相关阅读:
    大数据运维实战第二十四课 Yarn 资源调度 Fair Schedule 与 Capacity Scheduler 配置选型
    cat命令详解
    USB转IIC I2C SPI UART适配器模块可编程开发板应用工业数字接口转换
    大数据学习(17)-mapreduce task详解
    GNSS+IMU学习
    [Linux打怪升级之路]-秒懂进程地址空间
    神经网络案例分析
    滴灌通运营模式遭质疑:是P2P,是套利,还是模式创新?
    QT—信号与槽详解
    易点易动助力企业设备高效管理,提升设备利用率
  • 原文地址:https://blog.csdn.net/qq_42405688/article/details/132941982