1.首先new project
2文件目錄,按照文件目錄創建
3配置文件
Customer
package com.augmentum.quncrm.model; import org.springframework.data.annotation.Id; public class Customer { @Id public String id; public String firstName; public String lastName; public Customer() {} public Customer(String id,String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return String.format( "com.augmentum.quncrm.model.Customer[id=%s, firstName='%s', lastName='%s']", id, firstName, lastName); } }
CustomerRepository
package com.augmentum.quncrm.model; import org.springframework.data.annotation.Id; public class Customer { @Id public String id; public String firstName; public String lastName; public Customer() {} public Customer(String id,String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return String.format( "com.augmentum.quncrm.model.Customer[id=%s, firstName='%s', lastName='%s']", id, firstName, lastName); } }
testmongo
package com.augmentum.quncrm; import com.augmentum.quncrm.model.Customer; import com.augmentum.quncrm.repository.CustomerRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class testmongo implements CommandLineRunner { @Autowired private CustomerRepository repository; public static void main(String[] args) { SpringApplication.run(testmongo.class, args); } @Override public void run(String... args) throws Exception { //clear repository.deleteAll(); // insert a list of customers insert(); // fetch all customers print(); // fetch an individual customer System.out.println("com.augmentum.quncrm.model.Customer found with findByFirstName('Alice'):"); System.out.println("--------------------------------"); System.out.println(repository.findByFirstName("Alice")); System.out.println(); System.out.println("Customers found with findByLastName('Smith'):"); System.out.println("--------------------------------"); for (Customer customer : repository.findByLastName("Smith")) { System.out.println(customer); } // delete customers System.out.println("Customers deleteById('1') :"); System.out.println("-------------------------------"); repository.deleteById("1"); //repository.deleteById("3"); print(); System.out.println("Customers delete(customer):"); System.out.println("-------------------------------"); Customer customer = new Customer(); customer.setId("2"); repository.delete(customer); print(); //update customers System.out.println("Customers save(customer1) Jack White change to Jack Black:"); System.out.println("-------------------------------"); Customer customer1=new Customer(); customer1.setId("4"); customer1.setLastName("Black"); repository.save(customer1); print(); } void insert() { repository.insert(new Customer("1","Alice", "Smith")); repository.insert(new Customer("2","Bob", "Smith")); repository.insert(new Customer("3","May", "James")); repository.insert(new Customer("4","Jack", "White")); repository.insert(new Customer("5","Ethan", "Chen")); repository.insert(new Customer("6","Allen", "Wang")); repository.insert(new Customer("7","Baby", "Dollar")); } void print() { System.out.println("Customers found with findAll():"); System.out.println("-------------------------------"); for (Customer customer : repository.findAll()) { System.out.println(customer); } System.out.println(); } }
resources目錄下的application.yml
spring: data: mongodb: uri: mongodb://localhost:27017/springboot-db
pom.xml 這裏有springboot 的配置信息https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#getting-started-installing-spring-boot
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <groupId>mongo1</groupId> <artifactId>mongo1</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> </dependencies> </project>
一定要import,會有自動提示