├── README.md ├── src ├── main │ ├── resources │ │ └── application.properties │ └── java │ │ └── com │ │ └── javatechie │ │ └── spring │ │ └── mongo │ │ └── api │ │ ├── repository │ │ └── BookRepository.java │ │ ├── SpringMongodbApplication.java │ │ ├── model │ │ └── Book.java │ │ └── resource │ │ └── BookController.java └── test │ └── java │ └── com │ └── javatechie │ └── spring │ └── mongo │ └── api │ └── SpringMongodbApplicationTests.java └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # spring-mongodb 2 | How to intigrate spring boot with mongodb 3 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.host=localhost 2 | spring.data.mongodb.port=27017 3 | spring.data.mongodb.database=BookStore -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/mongo/api/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.mongo.api.repository; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.javatechie.spring.mongo.api.model.Book; 6 | 7 | public interface BookRepository extends MongoRepository{ 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/mongo/api/SpringMongodbApplication.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.mongo.api; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringMongodbApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringMongodbApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/javatechie/spring/mongo/api/SpringMongodbApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.mongo.api; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringMongodbApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/mongo/api/model/Book.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.mongo.api.model; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | @Getter 11 | @Setter 12 | @ToString 13 | 14 | @Document(collection = "Book") 15 | public class Book { 16 | @Id 17 | private int id; 18 | private String bookName; 19 | private String authorName; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/mongo/api/resource/BookController.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.mongo.api.resource; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.DeleteMapping; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RequestBody; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | import com.javatechie.spring.mongo.api.model.Book; 15 | import com.javatechie.spring.mongo.api.repository.BookRepository; 16 | 17 | @RestController 18 | public class BookController { 19 | 20 | @Autowired 21 | private BookRepository repository; 22 | 23 | @PostMapping("/addBook") 24 | public String saveBook(@RequestBody Book book) { 25 | repository.save(book); 26 | return "Added book with id : " + book.getId(); 27 | } 28 | 29 | @GetMapping("/findAllBooks") 30 | public List getBooks() { 31 | return repository.findAll(); 32 | } 33 | 34 | @GetMapping("/findAllBooks/{id}") 35 | public Optional getBook(@PathVariable int id) { 36 | return repository.findById(id); 37 | } 38 | 39 | @DeleteMapping("/delete/{id}") 40 | public String deleteBook(@PathVariable int id) { 41 | repository.deleteById(id); 42 | return "book deleted with id : " + id; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | spring-mongodb 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-mongodb 12 | mongodb with spring boot example 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.0.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-mongodb 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-devtools 40 | runtime 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | --------------------------------------------------------------------------------