springboot整合redis

一、引入maven依赖

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
            org.apache.commons
            commons-pool2
            2.11.1
        

二、配置application

springboot整合redis_第1张图片

  redis:
    host: localhost
    port: 6379
    database: 0
    timeout: 1000
    jedis:
      pool:
        max-active: 8
        min-idle: 5
        max-wait: -1

三、redis的set、get

(1)RedisCtroller

package com.mgx.controller;

import com.mgx.service.RedisService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author mgx
 * @date 2023/9/17 2:35 PM
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Resource
    private RedisService redisService;

    @PostMapping("/add")
    public String add(@Param("key") String key, @Param("value")String value) {
        return redisService.add(key,value);
    }

    @GetMapping("/get")
    public Object getUserInfo(@Param("key") String key) {
        return redisService.get(key);
    }

}

RedisService

package com.mgx.service;

/**
 * @author mgx
 * @date 2023/9/17 2:36 PM
 */
public interface RedisService {

    String add(String key,String value);

    Object get(String key);

}

RedisServiceImpl

package com.mgx.service.impl;

import com.mgx.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author mgx
 * @date 2023/9/17 2:37 PM
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String add(String key, String value) {
        redisTemplate.opsForValue().set(key,value);
        return "添加成功";
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

(2)使用postman接口调用

springboot整合redis_第2张图片

springboot整合redis_第3张图片 springboot整合redis_第4张图片

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

springboot整合redis_第5张图片

package com.mgx.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author mgx
 * @date 2023/9/17 3:09 PM
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        //指定kv的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        return redisTemplate;

    }
}

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

springboot整合redis_第6张图片

五、项目结构

springboot整合redis_第7张图片

你可能感兴趣的:(spring,boot,redis,bootstrap)