springboot jpa mongodb 打印查询语句.
By:Roy.LiuLast updated:2020-11-11
用了spring-boot-starter-data-mongodb来处理mongodb中的数据,经常需要观察查询的语句是否正确,所以需要把mongodb的查询语句打印出来.
application.properties 测试配置文件
server.servlet.context-path=/mongodb spring.application.name=springboot-mongodb server.port:3999 spring.data.mongodb.uri=mongodb://localhost:27017/test_mongodb #这是关键,打印查询语句就靠这个配置 logging.level.org.springframework.data.mongodb.core=DEBUG
maven 配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
JAVA 测试代码:
@Test public void findByExampleMatcher() { OnOfflineMsg msg = new OnOfflineMsg(); msg.setMac("8"); msg.setUserId(1); ExampleMatcher em = ExampleMatcher.matching().withMatcher("mac", ExampleMatcher.GenericPropertyMatchers.contains()) .withMatcher("userId", ExampleMatcher.GenericPropertyMatchers.exact()); List<OnOfflineMsg> list = onOfflineRepository.findAll(Example.of(msg, em), Sort.by(Direction.ASC, "mac")); list.stream().forEach(System.out::println); }
控制台打印语句:
From:一号门
COMMENTS