diff --git a/sky-server/pom.xml b/sky-server/pom.xml index 6172153..31ed4eb 100644 --- a/sky-server/pom.xml +++ b/sky-server/pom.xml @@ -115,6 +115,12 @@ org.apache.poi poi-ooxml + + + + org.springframework.boot + spring-boot-starter-data-redis + diff --git a/sky-server/src/main/java/com/sky/config/ReidsConfiguration.java b/sky-server/src/main/java/com/sky/config/ReidsConfiguration.java new file mode 100644 index 0000000..0275a9c --- /dev/null +++ b/sky-server/src/main/java/com/sky/config/ReidsConfiguration.java @@ -0,0 +1,27 @@ +package com.sky.config; + +import lombok.extern.slf4j.Slf4j; +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.StringRedisSerializer; + +@Configuration//当前是配置类 +@Slf4j +public class ReidsConfiguration { + + @Bean //将当前对象存入IOC容器 + //由于导入了spring的redis起步依赖,已经将redis的连接工厂类,加载到spring容器,只要定义参数默认会DI(依赖注入) + public RedisTemplate redisTemplate(RedisConnectionFactory factory){ + log.info("开始创建RedisTemplate对象"); + RedisTemplate redisTemplate = new RedisTemplate(); + //设置序列化器 todo 后面演示 + // 这里使用 StringRedisSerializer:Key 以字符串形式存储(避免默认 JdkSerializationFormat 导致的 Key 乱码) + redisTemplate.setKeySerializer(new StringRedisSerializer()); + //设置redis的连接工程 + // RedisConnectionFactory 是 "Redis 连接的工厂类",负责创建/管理 Redis 连接(底层由 Lettuce/Jedis 实现) + redisTemplate.setConnectionFactory(factory); + return redisTemplate; + } +} diff --git a/sky-server/src/main/resources/application-dev.yml b/sky-server/src/main/resources/application-dev.yml index 3e4fb57..271c4ac 100644 --- a/sky-server/src/main/resources/application-dev.yml +++ b/sky-server/src/main/resources/application-dev.yml @@ -9,4 +9,9 @@ sky: alioss: endpoint: https://oss-cn-shanghai.aliyuncs.com bucket-name: inmind-test - region: cn-shanghai \ No newline at end of file + region: cn-shanghai + redis: + host: localhost + port: 6379 + password: 123456 + database: 10 #不配置的话,默认是0号数据库(一共有16个,可以配置0~15) \ No newline at end of file diff --git a/sky-server/src/main/resources/application.yml b/sky-server/src/main/resources/application.yml index b682adc..a093bc1 100644 --- a/sky-server/src/main/resources/application.yml +++ b/sky-server/src/main/resources/application.yml @@ -12,6 +12,11 @@ spring: url: jdbc:mysql://${sky.datasource.host}:${sky.datasource.port}/${sky.datasource.database}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true username: ${sky.datasource.username} password: ${sky.datasource.password} + redis: + host: ${sky.redis.host} + port: ${sky.redis.port} + password: ${sky.redis.password} + database: ${sky.redis.database} #不配置的话,默认是0号数据库(一共有16个,可以配置0~15) mybatis: #mapper配置文件