Commit 0c8f5bbc authored by gkhan's avatar gkhan

Mongodb POC coded added

parent 0115e651
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nisum</groupId>
<artifactId>springboot-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mongodb</name>
<description>Demo project for Spring Boot</description>
<properties>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.nisum.springboot;
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);
}
}
package com.nisum.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nisum.springboot.entity.Product;
import com.nisum.springboot.service.ProductService;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService service;
@PostMapping(consumes="application/json", produces="application/json")
public Product saveProduct(@RequestBody Product product) {
return service.saveProduct(product);
}
@GetMapping
public List<Product> getAllProducts(){
return service.getAllProducts();
}
@GetMapping("{productId}")
public Product getProduct(@PathVariable String productId){
return service.getProduct(productId);
}
@PutMapping
public Product updateProduct(@RequestBody Product product){
return service.updateProduct(product);
}
@DeleteMapping("{productId}")
public String deleteProduct(@PathVariable String productId) {
return service.deleteProduct(productId);
}
@GetMapping("/productName/{productName}")
public List<Product> getProductName(@PathVariable String productName){
return service.getProductName(productName);
}
}
package com.nisum.springboot.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "Products")// collection same as table in RDMS
public class Product {
@Id
private String productId;
private String productName;
private Double price;
}
package com.nisum.springboot.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import com.nisum.springboot.entity.Product;
@Repository
public interface ProductRepository extends MongoRepository<Product, String>{
List<Product> findByProductName(String productName);
@Query("{productName: ?0}")
List<Product> getWithProductName(String productName);
@Query(value="{productName: ?0}" , fields = "{'price' : 1}")
List<Product> getProductValues(String productName);
}
package com.nisum.springboot.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nisum.springboot.entity.Product;
import com.nisum.springboot.repository.ProductRepository;
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public Product saveProduct(Product product) {
return repository.save(product);
}
public List<Product> getAllProducts(){
return repository.findAll();
}
public Product getProduct(String productId){
return repository.findById(productId).get();
}
public Product updateProduct(Product product){
Product existingProduct=repository.findById(product.getProductId()).get();
existingProduct.setProductName(product.getProductName());
existingProduct.setPrice(product.getPrice());
return repository.save(existingProduct);
}
public String deleteProduct(String productId) {
repository.deleteById(productId);
return productId+" ProductId Deleted Successfully";
}
public List<Product> getProductName(String productName) {
//return repository.findByProductName(productName);
return repository.getWithProductName(productName);
//return repository.getProductValues(productName);
}
}
server:
port: 8888
spring:
data:
mongodb:
host: localhost
port: 27017
database: ProductsDB
logging:
level:
org:
springframework:
data:
mongodb:
core:
MongoTemplate: DEBUG
\ No newline at end of file
package com.nisum.springboot;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootMongodbApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment