热烈推荐:超多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~

最新文章

  1. java中 String StringBuffer StringBuilder的区别
  2. 使用 Jmeter 做 Web 接口测试
  3. IP的正则表达式
  4. 优秀的富文本编辑器 Kindeditor
  5. DEP受保护的问题(尤其是Outlook)
  6. uptime命令具体解释——linux性能分析
  7. magento里获取用户姓名
  8. ElasticSearch + Canal 开发千万级的实时搜索系统
  9. pwnable.kr详细通关秘籍(二)
  10. Android Studio 学习(五)网络
  11. 安装MongoDB(做成Windows服务)并加载C#驱动程序
  12. idea提示不区分大小写,解决方法
  13. 18核心的Intel i9将在2019年夏发布
  14. python学习目录(转载)
  15. ubuntu14.04 cpu-ssd
  16. 07机器学习实战k-means
  17. taro 开发注意点
  18. linux下串口调试工具
  19. PAT1021(dfs 连通分量)
  20. 结构体指针之 段错误 具体解释(segmentation fault)

热门文章

  1. linux怎么开启telnet服务
  2. Linux下DNS服务器搭建详解
  3. java语言体系的技术简介之JSP、Servlet、JDBC、JavaBean(Application)
  4. 搞笑OI
  5. xBIM 基础14 使用LINQ实现最佳性能(优化查询)
  6. POJ 1944 并查集(模拟)
  7. 51nod 1065 最小正字段和 解决办法:set存前缀和,二分插入和二分查找
  8. ListNode的python 实现
  9. jQuery第一课 加载页面弹出一个对话框
  10. 查看centos7启动项