SpringBoot整合redis及mongodb的详细过程

目录
  • 一、先看Redis的使用:
    • 1. 在pom.xml中添加Redis相关依赖项
    • 2. 在application.properties中添加Redis的相关配置
    • 3. 新建 service/RedisService 接口及其实现类 service/impl/RedisServiceImpl
    • 4. 新建 controller/RedisController
    • 5. 通过Postman进行结果验证
  • 二、MongoDB的使用
    • 1. 首先还是先添加MongoDB相关依赖项
    • 2. 然后是添加MongoDB相关配置
    • 3. 新建 repository/PersonRepository
    • 4. Service接口定义及实现
    • 5. Controller实现
    • 6. Postman验证结果
  • 三、基于Redis实现Session配置共享
    • 1. 添加相关依赖
    • 2. 新增两个RedisController方法

NoSQL数据库之中最具代表性的,当属键值对数据库类别下的Redis,以及文档型数据库的Mongodb,本节我们重点关注这两个产品在SpringBoot下的整合及使用

最近很忙,好不容易才抽出了时间,咱们接上回

上次我们主要讲了如何通过SpringBoot快速集成mybatis/mybatis-plus,以实现业务交互中的数据持久化,而这一切都是基于关系型数据库(SQL)实现的

本节我们来把关注点转向NoSQL

NoSQL的概念:

NoSQL,泛指非关系型的数据库。随着互联网web2.0网站的兴起,传统的关系数据库在处理web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,出现了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,特别是大数据应用难题。(——来自百度百科)

得益于其直接基于内存的存储方式,NoSQL的访问速度可以用“飞快”两个字来形容

在生产环境中,NoSQL常常配合传统关系型数据库来使用,比如构建一层数据缓存来极大的提升数据的读取速度

NoSQL在日常业务的驱动之下,逐渐发展出几个主要的类别:键值对数据库、文档型数据库、列存储数据库以及图形化数据库

这4类NoSQL数据库之中最具代表性的,当属键值对数据库类别下的Redis,以及文档型数据库的Mongodb,本节我们重点关注这两个产品在SpringBoot下的整合及使用

照惯例先上项目结构:

一、先看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=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
# redis默认基于lettuce内核
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.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/RedisServiceImpl

package 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;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    StringRedisTemplate 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/RedisController

package 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 {

    @Autowired
    RedisService 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进行结果验证

通过RDM查看写入redis的数据:

之后是读操作:

至此我们便完成了SpringBoot中集成Redis的操作

二、MongoDB的使用

1. 首先还是先添加MongoDB相关依赖项

 <!-- 引入mongodb依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

2. 然后是添加MongoDB相关配置

# mongodb相关设置
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=local
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
#spring.data.mongodb.username=admin
#spring.data.mongodb.password=admin

各注释项内容依次是:身份验证库、目标数据库、主机地址、端口以及用户名和口令

由于我没有设置用户名和口令,所以直接注释掉这两项

3. 新建 repository/PersonRepository

package com.example.hellospringboot.repository;

import com.example.hellospringboot.model.Person;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends MongoRepository<Person, Integer> {
    Person findByNameIs(String name);
    Person findByIdIs(int id);
    Person findByIdAndName(int id, String name);
    Person findByIdOrName(int id, String name);
}

这里出现了非常神奇的一幕:

我们仅需要提供一个接口,而不用提供具体实现!

仅凭方法的命名规范,spring.data.mongodb就能自行分析开发者的意图,进行补全内部的业务逻辑!

而同样具备这种智能化能力的还有spring.jpa,后者也是一种非常便捷高效数据库驱动,与mybatis属于同类产品

顺便也给大家提供一份方法命名规范清单,请各位在方法命名时务必遵循以下规则:

关键字 方法命名 sql where字句
And findByNameAndPwd where name= ? and pwd =?
Or findByNameOrSex where name= ? or sex=?
Is,Equals findById,findByIdEquals where id= ?
Between findByIdBetween where id between ? and ?
LessThan findByIdLessThan where id < ?
LessThanEqual findByIdLessThanEqual where id <= ?
GreaterThan findByIdGreaterThan where id > ?
GreaterThanEqual findByIdGreaterThanEqual where id > = ?
After findByIdAfter where id > ?
Before findByIdBefore where id < ?
IsNull findByNameIsNull where name is null
isNotNull,NotNull findByNameNotNull where name is not null
Like findByNameLike where name like ?
NotLike findByNameNotLike where name not like ?

StartingWith

findByNameStartingWith where name like '?%'
EndingWith findByNameEndingWith where name like '%?'
Containing findByNameContaining where name like '%?%'
OrderBy findByIdOrderByXDesc where id=? order by x desc
Not findByNameNot where name <> ?
In findByIdIn(Collection<?> c) where id in (?)
NotIn findByIdNotIn(Collection<?> c) where id not in (?)
True
findByAaaTue

where aaa = true
False findByAaaFalse where aaa = false
IgnoreCase findByNameIgnoreCase where UPPER(name)=UPPER(?)

4. Service接口定义及实现

package com.example.hellospringboot.service;

import com.example.hellospringboot.model.Person;

public interface MongoService {
    public void insert(Person person);
    public Person findByName(String name);
    public Person findById(int id);
    public Person findByIdAndName(int id, String name);
    public Person findByIdOrName(int id, String name);
}
package com.example.hellospringboot.service.impl;

import com.example.hellospringboot.model.Person;
import com.example.hellospringboot.repository.PersonRepository;
import com.example.hellospringboot.service.MongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MongoServiceImpl implements MongoService {

    @Autowired
    PersonRepository repository;

    public void insert(Person person){
        repository.insert(person);
    }

    public Person findByName(String name){
        return repository.findByNameIs(name);
    }

    public Person findById(int id){
        return repository.findByIdIs(id);
    }

    public Person findByIdAndName(int id, String name){
        return repository.findByIdAndName(id, name);
    }

    public Person findByIdOrName(int id, String name){
        return repository.findByIdOrName(id, name);
    }
}

5. Controller实现

package com.example.hellospringboot.controller;

import com.example.hellospringboot.model.Person;
import com.example.hellospringboot.service.MongoService;
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;

@RestController
@RequestMapping("/mongo")
public class MongoController {

    @Autowired
    MongoService service;

    @PostMapping("/insert")
    public void insert(Person person){
        service.insert(person);
    }

    @GetMapping("/findByName")
    public Person findByName(String name){
        return service.findByName(name);
    }

    @GetMapping("/findById")
    public Person findById(int id){
        return service.findById(id);
    }

    @GetMapping("/findByIdAndName")
    public Person findByIdAndName(int id, String name){
        return service.findByIdAndName(id, name);
    }

    @GetMapping("/findByIdOrName")
    public Person findByIdOrName(int id, String name) {
        return service.findByIdOrName(id, name);
    }
}

Service及Controller的实现不再做过多赘述,还是老一套

6. Postman验证结果

向mongodb中写入一条数据

之后是几种读取操作:

不论是与或操作,我们都可以得到正确的结果

到这里,mongodb的集成就完成了

三、基于Redis实现Session配置共享

这部分纯属附送内容 ^ ^

前边我们已经完成了对Redis的集成操作,而基于Redis我们可以非常便捷的实现服务端Session配置的跨节点共享

服务端Session默认存储在本地,而当我们需要多台服务器共享一套Session配置时,本地化Session便不再满足我们的要求

而基于SpringSession,我们可以完全透明化的替换掉默认的Session容器,直接改为基于Redis存储

1. 添加相关依赖

 <!-- 引入spring session无缝替换原有的session系统 -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

2. 新增两个RedisController方法

@PostMapping("/setSession")
    public void setSession(String key, String val, HttpSession session){
        session.setAttribute(key, val);
    }

    @GetMapping("/getSession")
    public Object getSession(String key, HttpSession session){
        return session.getAttribute(key);
    }

就完事儿了?对!就完事儿了 ^ ^,超级简单是吧?

到此,我们就完成了SpringBoot对于Redis以及MongoDB的集成和使用

非常感慨于SpringBoot框架设计的智能化及人性化,就像身边有一哥们说的:这年头,框架都能直接听懂人话了!哈哈

到此这篇关于SpringBoot整合redis及mongodb的文章就介绍到这了,更多相关SpringBoot整合redis内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springboot2整合redis使用lettuce连接池的方法(解决lettuce连接池无效问题)

    lettuce客户端 Lettuce 和 Jedis 的都是连接Redis Server的客户端程序.Jedis在实现上是直连redis server,多线程环境下非线程安全(即多个线程对一个连接实例操作,是线程不安全的),除非使用连接池,为每个Jedis实例增加物理连接.Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问(即多个线程公用一个连接实例,线程安全),同时它是可伸缩的设计,一个连接

  • SpringBoot整合Redis的步骤

    1.添加配置文件: Redis.properties # 配置单台redis服务器 redis.host=192.168.126.129 ip地址 redis.port=6379 端口号 设置配置类: RedisConfig 2.将对象转化为Json格式入门案例 API: MAPPER.writeValueAsString(itemDesc); 3.将对象转化为Json格式格式优化 4.AOP实现Redis缓存 AOP实现Redis @cacheFind()注解 实现策略: A.需要自定义注解C

  • Springboot整合MongoDB进行CRUD操作的两种方式(实例代码详解)

    1 简介 Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库.两者在分布式.微服务架构中使用率极高,本文将用实例介绍如何在Springboot中整合MongoDB的两种方法:MongoRepository和MongoTemplate. 代码结构如下: 2 项目准备 2.1 启动MongoDB实例 为了方便,使用Docker来启动MongoDB,详细指导文档请参考:基于Docker的MongoDB实现授权访问的方法,这里不再赘述. 2.2 引入相关依赖

  • 详解springboot整合mongodb

    这篇文章主要介绍springboot如何整合MongoDB. 准备工作 安装 MongoDB jdk 1.8 maven 3.0 idea 环境依赖 在pom文件引入spring-boot-starter-data-mongodb依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifa

  • 使用SpringBoot中整合Redis

    目录 SpringBoot中整合Redis SpringBoot整合Redis改不了database问题 SpringBoot中整合Redis 本次,我们以IDEA + SpringBoot作为 Java中整合Redis的使用 的测试环境,如果对创建SpringBoot项目有不清楚的地方,可以参考这篇文章:使用Idea创建我的第一个SpringBoot项目 首先,我们需要导入Redis的maven依赖 <!-- Redis的maven依赖包 -->         <dependency

  • SpringBoot整合redis及mongodb的详细过程

    目录 一.先看Redis的使用: 1. 在pom.xml中添加Redis相关依赖项 2. 在application.properties中添加Redis的相关配置 3. 新建 service/RedisService 接口及其实现类 service/impl/RedisServiceImpl 4. 新建 controller/RedisController 5. 通过Postman进行结果验证 二.MongoDB的使用 1. 首先还是先添加MongoDB相关依赖项 2. 然后是添加MongoDB

  • SpringBoot整合Redis的示例

    redis是最常用的缓存数据库,常用于存储用户登录token.临时数据.定时相关数据等. redis是单线程的,所以redis的操作是原子性的,这样可以保证不会出现并发问题. redis基于内存,速度非常快,据测试,redis读的速度是110000次/s,写的速度是81000次/s 本节介绍SpringBoot引入redis,以及使用RedisTemplate来操作redis数据. 采用SpringBoot 2.1.9.RELEASE,对应示例代码在:https://github.com/lao

  • Springboot整合Redis与数据持久化

    目录 Springboot整合Redis 使用json方式存储 序列化方式存储数据 MySQL与Redis一致性解决同步问题 Redis持久化机制 全量同步与增量同步 RDB与AOF RDB AOF Springboot整合Redis 有两种存储数据的方式: 方案1:在Redis存放一个对象 使用json序列化与反序列化 方案2:直接使用redis自带序列化方式存储对象 maven依赖 <parent> <groupId>org.springframework.boot</g

  • SpringBoot整合Redis实现常用功能超详细过程

    目录 1 登陆功能 1.1 基于Session实现登录流程 1.1.1 session共享问题 1.2 Redis替代Session 1.2.1.设计key的结构 1.2.2.设计key的具体细节 1.2.3.整体访问流程 2 缓存功能 2.1 什么是缓存? 2.1.1 为什么要使用缓存 2.1.2 如何使用缓存 2.2.使用缓存 2.2.1 .缓存模型和思路 2.3 缓存更新策略 2.3.1 .数据库缓存不一致解决方案: 2.3.2 .数据库和缓存不一致采用什么方案 2.4 缓存穿透问题的解决

  • Springboot整合Redis的详细教程分享

    目录 1.Docker 安装 Redis 1.1 下载镜像 1.2 创建配置文件 1.3 启动Redis 1.4 进入Redis容器 1.5 redis 可视化工具 2.SpringBoot 整合Redis缓存 2.1 安装Redis 2.2 引入依赖 2.3 配置Redis地址端口 2.4 测试 1.Docker 安装 Redis 1.1 下载镜像 docker pull redis:6.2.6 1.2 创建配置文件 mkdir -p /mydata/redis/conf touch /myd

  • SpringBoot整合Redis、ApachSolr和SpringSession的示例

    本文介绍了SpringBoot整合Redis.ApachSolr和SpringSession,分享给大家,具体如下: 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多繁琐的配置.SpringBoot整合Druid.Mybatis已经司空见惯,在这里就不详细介绍了.今天我们要介绍的是使用SpringBoot整合Redis.ApacheSolr和SpringSession. 二.SpringBoot整合Redis Redis是大家比

  • Springboot整合Redis最简单例子分享

    1. 编写目的 最简单的例子,Springboot整合Redis. 2. 详细过程 pom 文件添加依赖 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr

  • SpringBoot整合Redis正确的实现分布式锁的示例代码

    前言 最近在做分块上传的业务,使用到了Redis来维护上传过程中的分块编号. 每上传完成一个分块就获取一下文件的分块集合,加入新上传的编号,手动接口测试下是没有问题的,前端通过并发上传调用就出现问题了,并发的get再set,就会存在覆盖写现象,导致最后的分块数据不对,不能触发分块合并请求. 遇到并发二话不说先上锁,针对执行代码块加了一个JVM锁之后问题就解决了. 仔细一想还是不太对,项目是分布式部署的,做了负载均衡,一个节点的代码被锁住了,请求轮询到其他节点还是可以进行覆盖写,并没有解决到问题啊

  • springboot整合redis修改分区的操作流程

    springboot整合redis修改分区 问题由来 最近使用springboot整合redis,一个系统动态数据源连接不同数据库,缓存使用的redis,那么就需要将不同数据库的数据缓存到redis不同的分区,也就是不同的库中. 老版解决 这里的老版指的是2.0之前的,我使用的1.5.9是ok的. redis的配置类这里就不贴了,网上很多. 1.使用JedisConnectionFactory修改 @Autowired JedisConnectionFactory jedisConnection

随机推荐