product management crud test

application.properties
server.port=8051
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

# Enabling H2 Console
spring.h2.console.enabled=true

# Custom H2 Console URL
spring.h2.console.path=/h2-console

# create database schema from SQL files
spring.jpa.hibernate.ddl-auto=create

#Turn Statistics on and log SQL stmts
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=false
#logging.level.org.hibernate.type=trace
#logging.level.org.hibernate.stat=debug

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

H2databaseApplication .java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.practice.h2database.entity.Product;
import com.practice.h2database.service.ProductService;

@SpringBootApplication
public class H2databaseApplication {

public static void main(String[] args) {
SpringApplication.run(H2databaseApplication.class, args);


}

}

ProductController .java

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.practice.h2database.entity.Product;
import com.practice.h2database.service.ProductService;

@RestController
@RequestMapping(value="/api/products")
public class ProductController {
@Autowired
private ProductService prodService;
@PostMapping(value = "/create")
public Product createProduct(@RequestBody Product prod) {
return prodService.createProduct(prod);
}
@GetMapping(value = "/getProductById/{productId}")
public List<Product> getProductById(@PathVariable(value="productId")Integer productId) {
return prodService.getProductById(productId);
}
@GetMapping(value="/getProductByType/{prodType}")
public List<Product> getProductByType(@PathVariable(value="prodType") String prodTpye){
return prodService.getProductByType(prodTpye);
}
@GetMapping(value="/getProductBySize/{prodSize}")
public List<Product> getProductBySize(@PathVariable(value="prodSize") String prodSize){
return prodService.getProductBySize(prodSize);
}
@GetMapping(value="/getProductByPrice/{prodPrice}")
public List<Product> getProductByPrice(@PathVariable(value="prodPrice") Double prodPrice){
return prodService.getProductByPrice(prodPrice);
}
}

ProductDao.java
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.practice.h2database.entity.Product;

@Repository
public interface ProductDao extends CrudRepository<Product, Long>{

@Query 
("Select p from Product p where prod_type=:prodtype")
List<Product> getProdByTpe(@Param("prodtype")String prodType);
@Query 
("Select p from Product p where prod_id=:prodId")
List<Product> getProdById(@Param("prodId")Integer prodId);

@Query 
("Select p from Product p where prod_size=:productSize")
List<Product> getProdBySize(@Param("productSize")String productSize);
@Query 
("Select p from Product p where prod_price=:productPrice")
List<Product> getProdByPrice(@Param("productPrice")Double productPrice);
}


Product.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
//@Table(name="product")
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="prod_id")
private Integer productId;
@Column(name="prod_brand")
private String brand;
@Column(name="prod_type")
private String productType;
@Column(name="prod_price")
private Double productprice;
@Column(name="prod_size")
private String productSize;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public Double getProductprice() {
return productprice;
}
public void setProductprice(Double productprice) {
this.productprice = productprice;
}
public String getProductSize() {
return productSize;
}
public void setProductSize(String productSize) {
this.productSize = productSize;
}
}

ProductService.java
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.practice.h2database.dao.ProductDao;
import com.practice.h2database.entity.Product;

@Service
public class ProductService {
@Autowired
private ProductDao prodDao;
public Product createProduct(Product prod) {
return prodDao.save(prod);
}
public List<Product> getProductById(Integer id) {
return prodDao.getProdById(id);
}
public List<Product> getProductByType(String prodTpye) {
return prodDao.getProdByTpe(prodTpye);
}
public List<Product> getProductBySize(String prodSize) {
return prodDao.getProdBySize(prodSize);
}
public List<Product> getProductByPrice(Double prodPrice) {
// TODO Auto-generated method stub
return null;
}

}

pom.xml
<?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.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.practice.h2database</groupId>
<artifactId>h2database</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>h2database</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-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.apache.derby</groupId> -->
<!-- <artifactId>derby</artifactId> -->
<!-- <scope>runtime</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Comments

Popular posts from this blog

Sealed Classes and Interfaces in Java

Introduction of RabbitMQ

RabbitMQ Installation on Windows