当前位置:   article > 正文

springboot2.1.0、MongoDB简单使用_springboot 2.1 mongodb

springboot 2.1 mongodb

一)在pom.xml文件中引入MongoDB需要的jar

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.oysept.springboot</groupId>
  6. <artifactId>oysept-springboot-mongodb</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>oysept-springboot-mongodb</name>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.1.0.RELEASE</version>
  14. <relativePath/>
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <java.version>1.8</java.version>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-web</artifactId>
  34. </dependency>
  35. <!-- mongodb -->
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  39. </dependency>
  40. </dependencies>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

 

二)在application.properties中添加MongoDB配置

  1. server.port=8080
  2. spring.data.mongodb.uri=mongodb://localhost:27017/newdb
  3. #spring.data.mongodb.uri=mongodb://name:password@localhost:27017/newdb

 

三)添加一个MongoDB中的Entity类

  1. package com.oysept.springboot.entity;
  2. import org.springframework.data.annotation.Id;
  3. import org.springframework.data.mongodb.core.mapping.Document;
  4. /**
  5. * 实体类,默认命名customerEntity,也可以指定命名@Document(collection="customer")
  6. * @author ouyangjun
  7. */
  8. @Document(collection="customer")
  9. public class CustomerEntity {
  10. @Id
  11. private String id;
  12. private String firstName;
  13. private String lastName;
  14. public CustomerEntity() {}
  15. public CustomerEntity (String firstName, String lastName) {
  16. this.firstName = firstName;
  17. this.lastName = lastName;
  18. }
  19. public String getId() {return id;}
  20. public void setId(String id) {this.id = id;}
  21. public String getFirstName() {return firstName;}
  22. public void setFirstName(String firstName) {this.firstName = firstName;}
  23. public String getLastName() {return lastName;}
  24. public void setLastName(String lastName) {this.lastName = lastName;}
  25. }

 

四)添加一个MongoDB接口Repository操作工具类

  1. package com.oysept.springboot.repository;
  2. import java.util.List;
  3. import org.springframework.data.mongodb.repository.MongoRepository;
  4. import com.oysept.springboot.entity.CustomerEntity;
  5. /**
  6. * mongodb自定义查询
  7. * @author ouyangjun
  8. */
  9. public interface CustomerRepository extends MongoRepository<CustomerEntity, String> {
  10. public CustomerEntity findByFirstName(String firstName);
  11. public List<CustomerEntity> findByLastName(String lastName);
  12. }

 

五)新增一个MongoDB main方法测试工具类

  1. package com.oysept.springboot.test;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import com.oysept.springboot.entity.CustomerEntity;
  8. import com.oysept.springboot.repository.CustomerRepository;
  9. @RunWith(SpringRunner.class)
  10. @SpringBootTest
  11. public class MongoDBTest {
  12. @Autowired
  13. private CustomerRepository repository;
  14. @Test
  15. public void addMongoDB() {
  16. // 删除
  17. repository.deleteAll();
  18. // 添加
  19. repository.save(new CustomerEntity("ouyangjun", "oysept"));
  20. repository.save(new CustomerEntity("liujie", "oysept"));
  21. // 查询
  22. System.out.println("查询-------------------------------");
  23. for (CustomerEntity entity : repository.findAll()) {
  24. System.out.println("firstName: "+entity.getFirstName()+",\t lastName: "+entity.getLastName());
  25. }
  26. // 根据firstName查询
  27. System.out.println("\n根据firstName查询--------------------------------");
  28. CustomerEntity et = repository.findByFirstName("ouyangjun");
  29. System.out.println("firstName: "+et.getFirstName()+",\t lastName: "+et.getLastName());
  30. // 根据lastName查询
  31. System.out.println("\n根据lastName查询--------------------------------");
  32. for (CustomerEntity entity : repository.findByLastName("oysept")) {
  33. System.out.println("firstName: "+entity.getFirstName()+",\t lastName: "+entity.getLastName());
  34. }
  35. }
  36. }

 

六)添加一个springboot启动类,并展示项目结构图

  1. package com.oysept.springboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * springboot启动类
  6. * @author ouyangjun
  7. */
  8. @SpringBootApplication
  9. public class MongoDBApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(MongoDBApplication.class, args);
  12. }
  13. }

项目结构图:

最后启动MongoDBApplication类,可用main方法测试类测试。

 

识别二维码关注个人微信公众号


 本章完结,待续,欢迎转载!
 
本文说明:该文章属于原创,如需转载,请标明文章转载来源!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55997
推荐阅读
相关标签