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

之四 2流高手速成记:SpringBoot整合redis及mongodb
文章插图
通过RDM查看写入redis的数据:

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

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

文章插图
至此我们便完成了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=adminspring.data.mongodb.database=localspring.data.mongodb.host=127.0.0.1spring.data.mongodb.port=27017#spring.data.mongodb.username=admin#spring.data.mongodb.password=admin
各注释项内容依次是:身份验证库、目标数据库、主机地址、端口以及用户名和口令
由于我没有设置用户名和口令,所以直接注释掉这两项
3. 新建 repository/PersonRepositorypackage com.example.hellospringboot.repository;import com.example.hellospringboot.model.Person;import org.springframework.data.mongodb.repository.MongoRepository;import org.springframework.stereotype.Repository;@Repositorypublic 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字句AndfindByNameAndPwdwhere name= ? and pwd =?OrfindByNameOrSexwhere name= ? or sex=?Is,EqualsfindById,findByIdEqualswhere id= ?BetweenfindByIdBetweenwhere id between ? and ?LessThanfindByIdLessThanwhere id < ?LessThanEqualfindByIdLessThanEqualwhere id <= ?GreaterThanfindByIdGreaterThanwhere id > ?GreaterThanEqualfindByIdGreaterThanEqualwhere id > = ?AfterfindByIdAfterwhere id > ?BeforefindByIdBeforewhere id < ?IsNullfindByNameIsNullwhere name is nullisNotNull,NotNullfindByNameNotNullwhere name is not nullLikefindByNameLikewhere name like ?NotLikefindByNameNotLikewhere name not like ?StartingWith
findByNameStartingWithwhere name like '?%'EndingWithfindByNameEndingWithwhere name like '%?'ContainingfindByNameContainingwhere name like '%?%'OrderByfindByIdOrderByXDescwhere id=? order by x descNotfindByNameNotwhere name <> ?InfindByIdIn(Collection<?> c)where id in (?)NotInfindByIdNotIn(Collection<?> c)where id not in (?)TruefindByAaaTue
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);}}

推荐阅读