文章目录
一、创建工程
读者自行创建
二、导入MongoDB依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
三、application.yml
spring: data: mongodb: uri: mongodb://localhost:27017/test
四、创建实体类
package com.example.springboot_mongdb.domain; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; /** * @Author: Ljx * @Date: 2021/12/5 21:57 * @role: */ /** * 使用@Document建立的是实体类和collection的关系 * @author jijunxiang */ @Document("test") public class Article { /** * 用来标识主键 */ @Id private Integer id; /** * 使用Field建立实体类中属性跟collection中字段的映射关系,如果省略,代表两个名称一致 */ // @Field private String name; private String content; private Integer hits; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Integer getHits() { return hits; } public void setHits(Integer hits) { this.hits = hits; } @Override public String toString() { return "Article{" + "id=" + id + ", name='" + name + '\'' + ", content='" + content + '\'' + ", hits=" + hits + '}'; } }
五、创建Dao
package com.example.springboot_mongdb.dao; import com.example.springboot_mongdb.domain.Article; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import java.util.List; /** * @Author: Ljx * @Date: 2021/12/5 22:08 * @role: */ @Repository public interface ArticleDao extends MongoRepository<Article,Integer> { /** * 根据标题查询 * @param name * @return */ List<Article> findByNameLike(String name); /** * 根据点击量查询 * @param hits * @return */ List<Article> findByHitsGreaterThan(Integer hits); }
六、测试MongoDBTest
package com.example.springboot_mongdb; import com.example.springboot_mongdb.dao.ArticleDao; import com.example.springboot_mongdb.domain.Article; import org.junit.jupiter.api.Test; 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.data.domain.Sort; import java.util.List; import java.util.Optional; @SpringBootTest class ArticleDaoTest { @Autowired private ArticleDao articleDao; //保存 @Test public void testSave(){ Article article = new Article(); article.setId(1); article.setName("测试"); article.setContent("成功添加"); article.setHits(100); articleDao.save(article); } //保存 @Test public void testUpdate(){ Article article = new Article(); article.setId(1); article.setName("测试1"); article.setContent("成功添加1"); article.setHits(200); articleDao.save(article); } //删除 @Test public void testDelete(){ articleDao.deleteById(1); } //添加数据 @Test public void makeDate(){ for (int i = 1; i <= 10; i++) { Article article = new Article(); article.setId(i); article.setName("测试"+i); article.setContent("成功添加"+i); article.setHits(200+i); articleDao.save(article); } } //主键查询 @Test public void testFindAll(){ List<Article> articles = articleDao.findAll(); for (Article article : articles) { System.out.println(article); } } //主键查询 @Test public void testFindById(){ Optional<Article> optional = articleDao.findById(1); System.out.println(optional.get()); } //分页和排序 @Test public void testFindAllWithPageAndSort(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); //设置分页条件 PageRequest pageRequest = PageRequest.of(1, 3, sort); Page<Article> page = articleDao.findAll(pageRequest); for (Article article : page.getContent()) { System.out.println(article); } } //根据标题查询 @Test public void testFindByName(){ List<Article> articles = articleDao.findByNameLike("li"); for (Article article : articles) { System.out.println(article); } } //根据点击量查询 @Test public void testFindByHitsGreaterThan(){ List<Article> articles = articleDao.findByHitsGreaterThan(105); for (Article article : articles) { System.out.println(article); } } }