Monday, 25 November 2019

Spring boot: MongoDB: Hello World in memory application


Step 1: Create Employee entity.
Employee.java
package com.sample.app.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document("employees")
public class Employee {
  @Id
  private String id;

  private String firstName;

  private String lastName;

  public Employee() {
  }

  public Employee(String firstName, String lastName) {
    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;
  }

  public String getFullName() {
    return this.firstName + "," + this.lastName;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
        .append(lastName).append("]");
    return builder.toString();
  }

}

@Document annotation is used to map a java model to mondoDB document.

Step 2: Create EmployeeRepository interface.

EmployeeRepository.java
package com.sample.app.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.sample.app.entity.Employee;

@Repository
public interface EmployeeRepository extends MongoRepository<Employee, String> {

}

Step 3: Create App.java

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.sample.app.entity.Employee;
import com.sample.app.repository.EmployeeRepository;

@SpringBootApplication
public class App {

  @Autowired
  private EmployeeRepository empRepository;

  public static void main(String[] args) {

    SpringApplication.run(App.class, args);
  }

  @Bean
  public CommandLineRunner demo() {
    return (args) -> {

      Employee emp1 = new Employee("Phalgun", "Garimella");
      Employee emp2 = new Employee("Sankalp", "Dubey");
      Employee emp3 = new Employee("Arpan", "Garimella");
      Employee emp4 = new Employee("Phalgun", "Dubey");

      empRepository.saveAll(Arrays.asList(emp1, emp2, emp3, emp4));

      List<Employee> emps = empRepository.findAll();
      emps.forEach(System.out::println);

    };
  }

}

Step 4: Update pom.xml with below dependencies.


pom.xml
<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>
  <groupId>springDataMongo</groupId>
  <artifactId>springDataMongo</artifactId>
  <version>1</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
  </parent>

  <dependencies>
    <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>
      <!-- <scope>test</scope> -->
    </dependency>
  </dependencies>
</project>


Total project structure looks like below.


Run App.java, you can see below messages in console.
Employee [id=5d568ddc37218f797c3e8bee, firstName=Phalgun, lastName=Garimella]
Employee [id=5d568ddc37218f797c3e8bef, firstName=Sankalp, lastName=Dubey]
Employee [id=5d568ddc37218f797c3e8bf0, firstName=Arpan, lastName=Garimella]
Employee [id=5d568ddc37218f797c3e8bf1, firstName=Phalgun, lastName=Dubey]

You can download complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment