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

我们在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进行结果验证

之四 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

经验总结扩展阅读