
0关注
552
文章
0
收藏
2
次赞
281266
查看
Ta的博客 更多
SpringBoot 五种获取ApplicationContext的方式java中进行日期时间比较的4种方法SpringBoot整合WebSocket实现前后端互推消息CSS怎么画五角星?SpringBoot中RedisUtils工具类配置及直接使用SpringBoot整合mongodb
一. 概述
参考开源项目https://github.com/xkcoding/spring-boot-demo
此Demo简单集成mongodb,使用官方的 starter 实现增删改查。
二. 安装mongodb
- 下载镜像:
docker pull mongo
- 运行容器:
docker run -d -p 27017:27017 --name mongo mongo:latest
三. SpringBoot工程
3.1 依赖
3.2 application.yml
3.3 启动类
3.4 实体类: Article.java
3.5 持久层: ArticleRepository.java
下标源于文章MongoRepository的生成规则
方法关键字 | 示例 | 等价于SQL |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname,findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1(参数绑定附加%) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1(参数与预先绑定%) |
Containing | findByFirstnameContaining | … where x.firstname like ?1(参数绑定%) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection ages) | … where x.age not in ?1 |
True | findByActiveTrue() | … where x.active = true |
False | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
3.6 简单的CURD
3.7 更新
3.8 高级查询
3.9 MongoTemplate 实现 联表/分页/排序查询
转载:https://www.jianshu.com/p/52290907a0df
0 0