之四 2流高手速成记:SpringBoot整合redis及mongodb

最近很忙,好不容易才抽出了时间,咱们接上回
上次我们主要讲了如何通过SpringBoot快速集成mybatis/mybatis-plus,以实现业务交互中的数据持久化,而这一切都是基于关系型数据库(SQL)实现的
本节我们来把关注点转向NoSQL

NoSQL的概念:
NoSQL , 泛指非关系型的数据库 。随着互联网web2.0网站的兴起,传统的关系数据库在处理web2.0网站 , 特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,出现了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展 。NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,特别是大数据应用难题 。(——来自百度百科)
得益于其直接基于内存的存储方式,NoSQL的访问速度可以用“飞快”两个字来形容
在生产环境中,NoSQL常常配合传统关系型数据库来使用,比如构建一层数据缓存来极大的提升数据的读取速度
NoSQL在日常业务的驱动之下,逐渐发展出几个主要的类别:键值对数据库、文档型数据库、列存储数据库以及图形化数据库
这4类NoSQL数据库之中最具代表性的,当属键值对数据库类别下的Redis , 以及文档型数据库的Mongodb,本节我们重点关注这两个产品在SpringBoot下的整合及使用
照惯例先上项目结构:
之四 2流高手速成记:SpringBoot整合redis及mongodb

文章插图
一、先看Redis的使用:
1. 在pom.xml中添加Redis相关依赖项<!-- 引入redis依赖(基于lettuce) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>2. 在application.properties中添加Redis的相关配置# redis相关设置spring.redis.database=0spring.redis.host=127.0.0.1spring.redis.port=6379spring.redis.password=# redis默认基于lettuce内核spring.redis.lettuce.pool.max-active=8spring.redis.lettuce.pool.max-idle=8spring.redis.lettuce.pool.max-wait=-1msspring.redis.lettuce.pool.min-idle=0这里关于lettuce内核有必要给大家解释一下:
在SpringBoot2.x版本之前,其集成的默认Redis库是Jedis,而在2.x版本之后才改为默认基于Lettuce
Jedis默认和Redis直连 , 为非线程安全模型,并发环境下需要池化使用
而Lettuce则是线程安全的 , 并发环境下可以通过一个实例搞定
当然 , 你也可以在SpringBoot2.x环境下依然使用Jedis,只需要把 spring.redis.lettuce 相关配置替换为 spring.redis.jedis 即可
更多内容大家感兴趣可以从网上查阅相关资料,这里推荐一篇:https://blog.csdn.net/kenkao/article/details/127085687
3. 新建 service/RedisService 接口及其实现类 service/impl/RedisServiceImplpackage com.example.hellospringboot.service;public interface RedisService {void set(String key, String val);String get(String key);}package com.example.hellospringboot.service.impl;import com.example.hellospringboot.service.RedisService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service;@Servicepublic class RedisServiceImpl implements RedisService {@AutowiredStringRedisTemplate redis;public void set(String key, String val){ValueOperations<String,String> ops = redis.opsForValue();ops.set(key, val);}public String get(String key){ValueOperations<String,String> ops = redis.opsForValue();return ops.get(key);}}我们在Service中自动装载一个StringRedisTemplate实例 , 而后通过其创建Operation对象,进行可以进行各种Redis读写操作
4. 新建 controller/RedisControllerpackage com.example.hellospringboot.controller;import com.example.hellospringboot.service.RedisService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpSession;@RestController@RequestMapping("/redis")public class RedisController {@AutowiredRedisService service;@PostMapping("/set")public void set(String key, String val){service.set(key, val);}@GetMapping("/get")public String get(String key){return service.get(key);}}5. 通过Postman进行结果验证

推荐阅读