redis+springboot+Cache缓存整合

首先启动redis

docker run --name redis -p 6379:6379 -d redis

然后连接redis

在这里插入图片描述
输入主机ip

yml配置文件

spring:
  redis:
    database: 0
    host: 192.168.249.137
    port: 6379
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t72jsp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    password: root
    username: root
server:
  port: 8080
mybatis:
  configuration:
    auto-mapping-behavior: FULL
  mapper-locations: classpath:mapper/*.xml
logging:
  level:
    com.lihu.redis.mapper: debug

Controller

package com.lihu.redis.controller;

import com.lihu.redis.biz.IStudentBiz;
import com.lihu.redis.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private IStudentBiz biz;
    @GetMapping("/findAll")
    public List<Student> findAll(){
        List<Student> list = biz.findAll();
        return  list;
    }

}

序列化配置类

package com.lihu.redis.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 java.net.UnknownHostException;

@Configuration
public class Config {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //修改默认的序列化规则
        //1.创建序列化规则对象
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        //2.更改默认的序列化规则
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        return template;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值