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

文章插图
通过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=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
经验总结扩展阅读
- 物流管理专业主要学什么 有哪些课程
- 十一 【Kubernetes】K8s笔记:Ingress 集群进出流量总管
- 微服务组件--限流框架Spring Cloud Hystrix分析
- 交管12123电动车罚款怎么缴费流程一览
- 上海迪士尼雷鸣山漂流需要带雨衣吗 上海迪士尼雷鸣山漂流雨衣多少钱
- 雷鸣山漂流会不会弄湿衣服 雷鸣山漂流会弄湿鞋子吗
- 雷鸣山漂流坐哪里不会湿 雷鸣山漂流多长时间一次
- 雷鸣山漂流要戴鞋套吗 雷鸣山漂流可以穿拖鞋吗
- 雷鸣山漂流可以脱鞋吗 雷鸣山漂流好玩吗
- 雷鸣山漂流有身高限制吗 雷鸣山漂流有体重限制吗