mongodb 是一种文档型数据库。跟 Redis 一样是非关系型数据库,Redis 属于那种小而快的数据库,常常用作缓存。
而如果我们需要存一些类似于日志的那种,可以尝试用 mongodb (当然也有人用 MySQL,就是有点慢)。我们尝试用 mongodb 来存储博客的日志信息。
本文主要介绍 SpringBoot 和 mongodb 整合,和基本的查询操作。
创建 springboot 项目,引入 web 和 lombok,然后再添加 mongodb 依赖
1、pom.xml
类似 Spring Data JPA
2、application.properties
saysky是数据库名称,确保你本地启动了 mongodb
1、日志实体
2、LogRepository
3、测试类
我们通过mongodb可视化工具可以看到数据
因为引入的 MongoRepository 是一种 JPA 框架,所以增删改查都很容易。
可以参考 Spring Data JPA 方法命名规范
而如果我们需要存一些类似于日志的那种,可以尝试用 mongodb (当然也有人用 MySQL,就是有点慢)。我们尝试用 mongodb 来存储博客的日志信息。
本文主要介绍 SpringBoot 和 mongodb 整合,和基本的查询操作。
一、依赖和配置
创建 springboot 项目,引入 web 和 lombok,然后再添加 mongodb 依赖
1、pom.xml
- <!--mongodb-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-mongodb</artifactId>
- </dependency>
类似 Spring Data JPA
2、application.properties
- spring.data.mongodb.uri=mongodb://localhost:27017/saysky
saysky是数据库名称,确保你本地启动了 mongodb
二、代码实例
1、日志实体
- package com.liuyanzhao.mongodb.model;
- import lombok.Data;
- import java.util.Date;
- /**
- * 日志
- * @author 言曌
- * @date 2018/9/3 20:00
- */
- @Data
- public class Log {
- private Long id;
- private Long userId;
- private Integer type;
- private String url;
- private String desc;
- private Date createTime;
- }
2、LogRepository
- package com.liuyanzhao.mongodb.dao;
- import com.liuyanzhao.mongodb.model.Log;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.mongodb.repository.MongoRepository;
- import org.springframework.stereotype.Repository;
- import java.util.Date;
- import java.util.List;
- /**
- * @author 言曌
- * @date 2018/9/3 14:17
- */
- @Repository
- public interface LogRepository extends <Log, Long> {
- /**
- * 根据用户ID查询
- * @param userId
- * @return
- */
- List<Log> findByUserId(Long userId);
- /**
- * 根据描述查询
- * @param desc
- * @return
- */
- List<Log> findByDesc(String desc);
- /**
- * 根据创建日期范围查询
- * @param startTime
- * @param endTime
- * @return
- */
- List<Log> findByCreateTimeBetween(Date startTime, Date endTime);
- /**
- * 根据描述查询
- * 分页查询
- * @param desc
- * @return
- */
- Page<Log> findByDesc(String desc, Pageable pageable);
- /**
- * 根据创建日期范围查询
- * 分页查询
- * @param startTime
- * @param endTime
- * @return
- */
- Page<Log> findByCreateTimeBetween(Date startTime, Date endTime,Pageable pageable);
- }
3、测试类
- package com.liuyanzhao.mongodb.dao;
- import com.liuyanzhao.mongodb.model.Log;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.test.context.junit4.SpringRunner;
- import java.util.Date;
- /**
- * @author 言曌
- * @date 2018/9/3 14:21
- */
- @SpringBootTest
- @RunWith(SpringRunner.class)
- public class LogRepositoryTest {
- @Autowired
- private LogRepository logRepository;
- @Test
- public void save() {
- Log log = new Log();
- log.setId(7L);
- log.setType(1);
- log.setDesc("更新用户");
- log.setUserId(10002L);
- log.setUrl("/user/update");
- log.setCreateTime(new Date());
- logRepository.save(log);
- }
- @Test
- public void findById() {
- Log Log = logRepository.findById(1L).get();
- System.out.println(Log);
- }
- @Test
- public void findByDes() {
- //不分页
- // List<Log> LogList = logRepository.findByDesc("添加用户");
- // System.out.println(LogList);
- //分页查询
- //查询第1页,每页显示2条
- PageRequest pageRequest = new PageRequest(0,2);
- Page<Log> logPage = logRepository.findByDesc("添加用户",pageRequest);
- System.out.println(logPage);
- }
- @Test
- public void findByCreateTimeBetween() {
- //根据时间区间查询
- //不分页
- Date createdAtStart = new Date(1535974057016L);
- Date createdAtEnd = new Date(1535974145009L);
- // List<Log> LogList = logRepository.findByCreateTimeBetween(createdAtStart,createdAtEnd);
- // System.out.println(LogList);
- //分页查询
- //查询第1页,每页显示2条
- PageRequest pageRequest = new PageRequest(0,2);
- Page<Log> logPage = logRepository.findByCreateTimeBetween(createdAtStart,createdAtEnd,pageRequest);
- System.out.println(logPage);
- }
- }
我们通过mongodb可视化工具可以看到数据

三、更多查询
因为引入的 MongoRepository 是一种 JPA 框架,所以增删改查都很容易。
可以参考 Spring Data JPA 方法命名规范
方法关键字 | 示例 | 等价于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) |
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏