springboot集成mongodb
https://github.com/DNYDYS/spring-boot-demo-mongodb/tree/master 注意 monogodb为windows 5.0.4版本 安装教程:https://blog.csdn.net/weixin_42254034/article/details/121756370
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-demo-mongodb</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-demo-mongodb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.dnydys</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-demo-mongodb</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
data:
mongodb:
host: localhost
port: 27017
database: article_db
logging:
level:
org.springframework.data.mongodb.core: debug
Article.java
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Article {
@Id
private Long id;
private String title;
private String content;
private Date createTime;
private Date updateTime;
private Long thumbUp;
private Long visits;
}
ArticleRepository.java
public interface ArticleRepository extends MongoRepository<Article, Long> {
List<Article> findByTitleLike(String title);
}
ArticleRepositoryTest.java
@Slf4j
public class ArticleRepositoryTest extends SpringBootDemoMongodbApplicationTests {
@Autowired
private ArticleRepository articleRepo;
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private Snowflake snowflake;
@Test
public void testSave() {
Article article = new Article(1L, RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil.date(), DateUtil
.date(), 0L, 0L);
articleRepo.save(article);
log.info("【article】= {}", JSONUtil.toJsonStr(article));
}
@Test
public void testSaveList() {
List<Article> articles = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
articles.add(new Article(snowflake.nextId(), RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil
.date(), DateUtil.date(), 0L, 0L));
}
articleRepo.saveAll(articles);
log.info("【articles】= {}", JSONUtil.toJsonStr(articles.stream()
.map(Article::getId)
.collect(Collectors.toList())));
}
@Test
public void testUpdate() {
articleRepo.findById(1L).ifPresent(article -> {
article.setTitle(article.getTitle() + "更新之后的标题");
article.setUpdateTime(DateUtil.date());
articleRepo.save(article);
log.info("【article】= {}", JSONUtil.toJsonStr(article));
});
}
@Test
public void testDelete() {
articleRepo.deleteById(1L);
articleRepo.deleteAll();
}
@Test
public void testThumbUp() {
articleRepo.findById(1L).ifPresent(article -> {
article.setThumbUp(article.getThumbUp() + 1);
article.setVisits(article.getVisits() + 1);
articleRepo.save(article);
log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article.getVisits());
});
}
@Test
public void testThumbUp2() {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(1L));
Update update = new Update();
update.inc("thumbUp", 1L);
update.inc("visits", 1L);
mongoTemplate.updateFirst(query, update, "article");
articleRepo.findById(1L)
.ifPresent(article -> log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article
.getVisits()));
}
@Test
public void testQuery() {
Sort sort = Sort.by("thumbUp", "updateTime").descending();
PageRequest pageRequest = PageRequest.of(0, 5, sort);
Page<Article> all = articleRepo.findAll(pageRequest);
log.info("【总页数】= {}", all.getTotalPages());
log.info("【总条数】= {}", all.getTotalElements());
log.info("【当前页数据】= {}", JSONUtil.toJsonStr(all.getContent()
.stream()
.map(article -> "文章标题:" + article.getTitle() + "点赞数:" + article.getThumbUp() + "更新时间:" + article.getUpdateTime())
.collect(Collectors.toList())));
}
@Test
public void testFindByTitleLike() {
List<Article> articles = articleRepo.findByTitleLike("更新");
log.info("【articles】= {}", JSONUtil.toJsonStr(articles));
}
}
心决定的东西嘴解释不了 这个答案太长 我想用余生来告诉你
|