Coder Social home page Coder Social logo

steven0038 / spring-boot-mongodb-crud Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rahul-ghadge/spring-boot-mongodb-crud

0.0 0.0 0.0 34 KB

Spring boot MongoDB CRUD (Create, Read, Update, Delete) example using MongoTemplate and MongoRepository

Java 100.00%

spring-boot-mongodb-crud's Introduction

spring-boot-mongodb-crud

This project explains CRUD (Create, Read, Update, Delete) operations using MongoTemplate and MongoRepository using spring boot and mongo DB. In this app we are using Spring Data JPA for built-in methods to do CRUD operations and Mongo queries using MongoTemplate.
@EnableMongoRepositories annotation is used on main class to Enable Mongo related configuration, which will read properties from application.properties file.

Prerequisites

Tools

  • Eclipse or IntelliJ IDEA (or any preferred IDE) with embedded Gradle
  • Maven (version >= 3.6.0)
  • Postman (or any RESTful API testing tool)

Build and Run application

GOTO > ~/absolute-path-to-directory/spring-boot-mongodb
and try below command in terminal

mvn spring-boot:run it will run application as spring boot application

or

mvn clean install it will build application and create jar file under target directory

Run jar file from below path with given command

java -jar ~/path-to-spring-boot-mongodb/target/spring-boot-mongodb-0.0.1-SNAPSHOT.jar

Or

run main method from SpringBootMongoDBApplication.java as spring boot application.

Note : In SpringBootMongoDBApplication.java class we have autowired both SuperHero and Employee repositories.
If there is no record present in DB for any one of that module class (Employee or SuperHero), static data is getting inserted in DB from HelperUtil.java class when we are starting the app for the first time.

Code Snippets

  1. Maven Dependencies

    Need to add below dependencies to enable Mongo related config in pom.xml. Lombok's dependency is to get rid of boiler-plate code.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    
  2. Properties file

    Reading Mongo DB related properties from application.properties file and configuring Mongo connection factory for mongoDB.

    src/main/resources/application.properties

    spring.data.mongodb.host=localhost
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=spring_boot_mongo_app
    spring.jackson.default-property-inclusion=NON_NULL
    #logging.level.ROOT=DEBUG  
    
  3. Model class

    Below are the model classes which we will store in MongoDB and perform CRUD operations.
    com.spring.mongo.demo.model.SuperHero.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Document(collection = "super_hero")
    public class SuperHero implements Serializable {
    
        @Id
        private String id;
    
        private String name;
        private String superName;
        private String profession;
        private int age;
        private boolean canFly;
    
        // Constructor, Getter and Setter
    }
    

    com.spring.mongo.demo.model.Employee.java

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class Employee implements Serializable {
    
        @Id
        private String id;
       
        private int empId;
        private String firstName;
        private String lastName;
        private float salary;
        
        // Constructor, Getter and Setter
    }
    
  4. CRUD operation for Super Heroes

    In com.spring.mongo.demo.controller.SuperHeroController.java class, we have exposed 5 endpoints for basic CRUD operations

    • GET All Super Heroes
    • GET by ID
    • POST to store Super Hero in DB
    • PUT to update Super Hero
    • DELETE by ID
    @RestController
    @RequestMapping("/super-hero")
    public class SuperHeroController {
        
        @GetMapping
        public ResponseEntity<List<?>> findAll();
    
        @GetMapping("/{id}")
        public ResponseEntity<?> findById(@PathVariable String id);
    
        @PostMapping
        public ResponseEntity<?> save(@RequestBody SuperHero superHero);
    
        @PutMapping
        public ResponseEntity<?> update(@RequestBody SuperHero superHero);
    
        @DeleteMapping("/{id}")
        public ResponseEntity<?> delete(@PathVariable String id);
    }
    

    In com.spring.mongo.demo.repository.SuperHeroRepository.java, we are extending MongoRepository<Class, ID> interface which enables CRUD related methods.

    public interface SuperHeroRepository extends MongoRepository<SuperHero, String> {
    }
    

    In com.spring.mongo.demo.service.impl.SuperHeroServiceImpl.java, we are autowiring above interface using @Autowired annotation and doing CRUD operation.

  5. JPA And Query operation for Employee

    In com.spring.mongo.demo.controller.EmployeeController.java class JPA related queries API Endpoints are placed. In com.spring.mongo.demo.controller.EmployeeQueryController.java class Mongo queries API Endpoints are placed.

API Endpoints

spring-boot-mongodb-crud's People

Contributors

jigar-kakadiya avatar rahul-ghadge avatar steven0038 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.