外卖项目day05-在Java中操作Redis_Spring Data Redis使用方式_环境搭建
This commit is contained in:
@@ -115,6 +115,12 @@
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--springData-redis-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,8 @@ sky:
|
||||
endpoint: https://oss-cn-shanghai.aliyuncs.com
|
||||
bucket-name: inmind-test
|
||||
region: cn-shanghai
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
password: 123456
|
||||
database: 10 #不配置的话,默认是0号数据库(一共有16个,可以配置0~15)
|
||||
@@ -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配置文件
|
||||
|
||||
Reference in New Issue
Block a user