zoukankan      html  css  js  c++  java
  • springboot整合redis

    步骤:

    一、引入redis的starter

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    二、在springboot配置文件中配置连接redis的主机地址

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://192.168.1.6:3306/spring_cache?useUnicode=true
      &characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=123456
    mybatis.configuration.map-underscore-to-camel-case=true
    logging.level.com.xj.springboot.mapper=debug
    spring.redis.host=192.168.1.6

    三、使用

    当我们导入了redis的starter后,springboot中的自动配置类RedisAutoConfiguration就生效了,RedisAutoConfiguration自动配置类会自动往容器中添加两个组件:StringRedisTemplate和RedisTemplate。我们要使用的话直接通过Autowired注解将这两个组件注入就可以使用了。

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
    • StringRedisTemplate:用于操作key和value都是字符串的数据。
    • RedisTemplate:用于操作key和value都是对象的数据。

     注意:使用RedisTemplate操作对象时,对象必须是可序列化的,即需要实现序列化接口。

  • 相关阅读:
    面试题33:把数组排成最小的数
    面试题32:从1到n整数中1出现的次数
    面试题31:连续子数组的最大和
    HTTPS 及加密信息全解析
    面试题30:最小的k个数
    linux退出vi
    linux清除当前屏幕
    java web开发环境配置
    jQuery积累
    html5离线应用详摘
  • 原文地址:https://www.cnblogs.com/bear7/p/13694155.html
Copyright © 2011-2022 走看看