springboot整合redis,并解决乱码问题。
2024-08-31 14:38:29
热烈推荐:超多IT资源,尽在798资源网
springboot 版本为 1.5.9
//如果是2.x 修改 pom.xml 也可切换成 1.5.9
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
一、修改 pom.xml 文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
二、修改 application.properties 文件添加 redis 相关配置
#****************************redis****************************
# Redis数据库索引(默认为0)
spring.redis.database=5
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
三、新增 RedisConfig
@Configuration
public class RedisConfig {
@Bean(name="redisTemplate")
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(redisSerializer);
//value hashmap序列化
template.setHashValueSerializer(redisSerializer);
//key haspmap序列化
template.setHashKeySerializer(redisSerializer);
//
return template;
}
}
更改相关序列化,只是为了使得存储的key和value不出现乱码。使用 StringRedisTemplate 也可以解决同样的问题。
项目demo地址:spring-boot-redis
thanks~
最新文章
- java中 String StringBuffer StringBuilder的区别
- 使用 Jmeter 做 Web 接口测试
- IP的正则表达式
- 优秀的富文本编辑器 Kindeditor
- DEP受保护的问题(尤其是Outlook)
- uptime命令具体解释——linux性能分析
- magento里获取用户姓名
- ElasticSearch + Canal 开发千万级的实时搜索系统
- pwnable.kr详细通关秘籍(二)
- Android Studio 学习(五)网络
- 安装MongoDB(做成Windows服务)并加载C#驱动程序
- idea提示不区分大小写,解决方法
- 18核心的Intel i9将在2019年夏发布
- python学习目录(转载)
- ubuntu14.04 cpu-ssd
- 07机器学习实战k-means
- taro 开发注意点
- linux下串口调试工具
- PAT1021(dfs 连通分量)
- 结构体指针之 段错误 具体解释(segmentation fault)