Skip to content

Commit

Permalink
mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
javahongxi committed Feb 1, 2018
1 parent 6412787 commit d0779c8
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions whatsmars-spring-boot-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>whatsmars-boot-sample-session</module>
<module>whatsmars-boot-sample-logback</module>
<module>whatsmars-boot-sample-cache</module>
<module>whatsmars-boot-sample-mongodb</module>
</modules>

<!--<properties>
Expand Down
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>
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);
}

}
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);

}
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);
}

}
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

0 comments on commit d0779c8

Please sign in to comment.