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


where aaa = trueFalsefindByAaaFalsewhere aaa = falseIgnoreCasefindByNameIgnoreCasewhere 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;@Servicepublic class MongoServiceImpl implements MongoService {@AutowiredPersonRepository 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 {@AutowiredMongoService 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中写入一条数据

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

文章插图

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

文章插图
之后是几种读取操作:
之四 2流高手速成记:SpringBoot整合redis及mongodb

文章插图

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

文章插图

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

文章插图
不论是与或操作 , 我们都可以得到正确的结果
到这里 , mongodb的集成就完成了
三、基于Redis实现Session配置共享
这部分纯属附送内容 ^ ^
【之四 2流高手速成记:SpringBoot整合redis及mongodb】前边我们已经完成了对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>

经验总结扩展阅读