-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6412787
commit d0779c8
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
whatsmars-spring-boot-samples/whatsmars-boot-sample-mongodb/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>whatsmars-spring-boot-samples</artifactId> | ||
<groupId>org.hongxi</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>whatsmars-boot-sample-mongodb</artifactId> | ||
<name>${project.artifactId}</name> | ||
|
||
<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>de.flapdoodle.embed</groupId> | ||
<artifactId>de.flapdoodle.embed.mongo</artifactId> | ||
</dependency>--> | ||
</dependencies> | ||
|
||
|
||
</project> |
27 changes: 27 additions & 0 deletions
27
...-boot-sample-mongodb/src/main/java/org/hongxi/whatsmars/boot/sample/mongodb/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.hongxi.whatsmars.boot.sample.mongodb; | ||
|
||
import org.springframework.data.annotation.Id; | ||
|
||
public class Customer { | ||
|
||
@Id | ||
private String id; | ||
|
||
private String firstName; | ||
private String lastName; | ||
|
||
public Customer() { | ||
} | ||
|
||
public Customer(String firstName, String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Customer[id=%s, firstName='%s', lastName='%s']", id, | ||
firstName, lastName); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...le-mongodb/src/main/java/org/hongxi/whatsmars/boot/sample/mongodb/CustomerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.hongxi.whatsmars.boot.sample.mongodb; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
public interface CustomerRepository extends MongoRepository<Customer, String> { | ||
|
||
Customer findByFirstName(String firstName); | ||
|
||
List<Customer> findByLastName(String lastName); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...ongodb/src/main/java/org/hongxi/whatsmars/boot/sample/mongodb/SampleMongoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.hongxi.whatsmars.boot.sample.mongodb; | ||
|
||
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 SampleMongoApplication implements CommandLineRunner { | ||
|
||
@Autowired | ||
private CustomerRepository repository; | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
this.repository.deleteAll(); | ||
|
||
// save a couple of customers | ||
this.repository.save(new Customer("Alice", "Smith")); | ||
this.repository.save(new Customer("Bob", "Smith")); | ||
|
||
// fetch all customers | ||
System.out.println("Customers found with findAll():"); | ||
System.out.println("-------------------------------"); | ||
for (Customer customer : this.repository.findAll()) { | ||
System.out.println(customer); | ||
} | ||
System.out.println(); | ||
|
||
// fetch an individual customer | ||
System.out.println("Customer found with findByFirstName('Alice'):"); | ||
System.out.println("--------------------------------"); | ||
System.out.println(this.repository.findByFirstName("Alice")); | ||
|
||
System.out.println("Customers found with findByLastName('Smith'):"); | ||
System.out.println("--------------------------------"); | ||
for (Customer customer : this.repository.findByLastName("Smith")) { | ||
System.out.println(customer); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SampleMongoApplication.class, args); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...mars-spring-boot-samples/whatsmars-boot-sample-mongodb/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spring: | ||
data: | ||
mongodb: | ||
uri: mongodb://mongo:731152a162a708c6@10.143.184.150:7974/admin | ||
database: dp_visual |