<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
You can set spring.data.mongodb.uri property to change the URL and configure additional settings such as the replica set:
spring.data.mongodb.uri=mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test
Let’s discuss about the application files
Configuration file application.properties
spring.data.mongodb.database=dojdb spring.data.mongodb.host=localhost spring.data.mongodb.port=27017
Following dependencies to be added with maven file
<?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> <groupId>com.dineshonjava.sbmdb</groupId> <artifactId>SpringBootMongoDB</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootMongoDB</name> <description>SpringBootMongoDB project for Spring Boot with MongoDB providing APIs</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
/** * */ package com.dineshonjava.sbmdb.models; import java.util.Date; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; /** * @author Dinesh.Rajput * */ @Document public class Booking{ @Id String id; String psngrName; String departure; String destination; Date travelDate; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPsngrName() { return psngrName; } public void setPsngrName(String psngrName) { this.psngrName = psngrName; } public String getDeparture() { return departure; } public void setDeparture(String departure) { this.departure = departure; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } public Date getTravelDate() { return travelDate; } public void setTravelDate(Date travelDate) { this.travelDate = travelDate; } }
package com.dineshonjava.sbmdb.models; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.transaction.annotation.Transactional; @Transactional public interface BookingRepository extends MongoRepository { /** * This method will find an Boooking instance in the database by its departure. * Note that this method is not implemented and its working code will be * automatically generated from its signature by Spring Data JPA. */ public Booking findByDeparture(String departure); }
/** * */ package com.dineshonjava.sbmdb.controller; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.dineshonjava.sbmdb.models.Booking; import com.dineshonjava.sbmdb.models.BookingRepository; /** * @author Dinesh.Rajput * */ @RestController @RequestMapping("/booking") public class BookingController { @Autowired BookingRepository bookingRepository; /** * GET /create --> Create a new booking and save it in the database. */ @RequestMapping("/create") public Map<String, Object> create(Booking booking) { booking.setTravelDate(new Date()); booking = bookingRepository.save(booking); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("message", "Booking created successfully"); dataMap.put("status", "1"); dataMap.put("booking", booking); return dataMap; } /** * GET /read --> Read a booking by booking id from the database. */ @RequestMapping("/read") public Map<String, Object> read(@RequestParam String bookingId) { Booking booking = bookingRepository.findOne(bookingId); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("message", "Booking found successfully"); dataMap.put("status", "1"); dataMap.put("booking", booking); return dataMap; } /** * GET /update --> Update a booking record and save it in the database. */ @RequestMapping("/update") public Map<String, Object> update(@RequestParam String bookingId, @RequestParam String psngrName) { Booking booking = bookingRepository.findOne(bookingId); booking.setPsngrName(psngrName); booking = bookingRepository.save(booking); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("message", "Booking updated successfully"); dataMap.put("status", "1"); dataMap.put("booking", booking); return dataMap; } /** * GET /delete --> Delete a booking from the database. */ @RequestMapping("/delete") public Map<String, Object> delete(@RequestParam String bookingId) { bookingRepository.delete(bookingId); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("message", "Booking deleted successfully"); dataMap.put("status", "1"); return dataMap; } /** * GET /read --> Read all booking from the database. */ @RequestMapping("/read-all") public Map<String, Object> readAll() { List<Booking> bookings = bookingRepository.findAll(); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("message", "Booking found successfully"); dataMap.put("totalBooking", bookings.size()); dataMap.put("status", "1"); dataMap.put("bookings", bookings); return dataMap; } }
package com.dineshonjava.sbmdb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootMongoDbApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMongoDbApplication.class, args); } }
Whole Source code here
Spring Boot Related Topics
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…