欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot+Mybatis+Redis的配置

程序员文章站 2022-03-10 16:12:16
文章目录一:引入依赖二:application.yml配置三:配置Redis序列化方式及连接四:测试如果不会配置mybatis的,可以参照这篇详细的博客没有安装Redis的参照博客不会建Springboot的,参照博客Springboot2.3+mybatis3.5+mysql8.0+Redis2.3一:引入依赖 org.spring...

如果不会配置mybatis的,可以参照这篇详细的博客
没有安装Redis的参照博客
不会建Springboot的,参照博客

Springboot2.3+mybatis3.5+mysql8.0+Redis2.3

一:引入依赖

<!--        Redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.3.0.RELEASE</version>
        </dependency>
  		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>

二:application.yml配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/learn?useSSL=true&characterEncoding=utf-8&serverTimezone=GMT
    username: root
    password: 123456
  redis:
    host: 127.0.0.1
    port: 6379
    database: 0
#    lettuce:
#      shutdown-timeout: 200ms
#      pool:
#        max-active: 7
#        max-idle: 7
#        min-idle: 2
#        max-wait: -1ms
    password: 123456

三:配置Redis序列化方式及连接

使用JackSon进行序列化,要引入maven依赖

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

        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // 设置value的序列化规则和 key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

四:测试

引入RedisTemplate就可以对Redis进行操作了

本人在serviceimpl层进行对Redis进行调用的

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
   public User getAllUser(Integer id) {
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        User user;
        String key ="user_"+id;
        boolean flag= redisTemplate.hasKey(key);
        if(flag){
            System.out.println("Redis获取数据中......");
           user = operations.get(key);
        }else {
            System.out.println("数据库获取数据中......");
            user=userMapper.getAllUser(id);
            System.out.println("Redis插入数据中......");
         operations.set(key,user);
        }
        return  user;
    }
    }

controller层代码
SpringBoot+Mybatis+Redis的配置
记得在实体类序列化
SpringBoot+Mybatis+Redis的配置

第一次查询:
SpringBoot+Mybatis+Redis的配置
SpringBoot+Mybatis+Redis的配置
第二次调用:
SpringBoot+Mybatis+Redis的配置

成功将对象序列化成JSon字符串

SpringBoot+Mybatis+Redis的配置
项目结构图
SpringBoot+Mybatis+Redis的配置

本文地址:https://blog.csdn.net/qq_43520913/article/details/109952345