Project using SpringBoot, Data JPA and PostGreSQL

Project using SpringBoot, Data JPA and PostGreSQL

  1. Installed PostGreSQL and created a table named 'product' with columns 'id', 'name', 'type', 'place', 'warranty'. We created one extra column 'id' this time and set it as Primary key.
  2. Then created a Maven repository by adding Postgresql and VPA dependencies.
  3. Then I saved our Data into the database using Connection with query "insert into product(name, type, place, warranty) values (?,?,?,?)" where product is our table_name. And voila we now have data in the table.
  4. Then I created the SpringBoot application. It handles object creation and deletion itself. It provides a container. Then using getBeans() dependency Injection is handled for ProductService.
  5. I created ProductDb which extends JpaRepository where we need to tell in which class we are working and what's the Primary key type.
  6. I used Autowired which provides the object of ProductDB automatically.
  7. Using SpringBoot, large queries were converted by inbuilt methods. We get so many options from where we can fetch data from the table according to our requirements.

Available data in DataBase:

No alt text provided for this image

To fetch all the data from the database

public List<Product> getAllProducts(){
  return db.findAll();
}        

ProductService -

public static void getAllProducts(ProductService service){
  System.out.println("Available data in Database");
  List<Product> products = service.getAllProducts();
  for(Product p : products){
    System.out.println(p);
  }
}        


Assignment 1: Search by Product Name

public static void getProductByName(ProductService service){
  List<Product> products = service.getProductByName("BlackBeast");
  System.out.println("Data which is having name as BlackBeast in Database");
  for(Product p : products){
    System.out.println(p);
  }
}

        

ProductService -

public List<Product> getProductByName(String name){
return db.findByNameIgnoreCase(name.toLowerCase());
}
        

I used the findByNameIgnoreCase() method which takes the String as an argument to fetch the list of products by name, Where I handled the case-sensitive scenario as well.

Output result:

No alt text provided for this image


Assignment 2: Search by Products by text

public static void getProductsByText(ProductService service){
  List<Product> products = service.getProductsByText("laptop");
  System.out.println("Data which is having laptop text in any column in Database");
  for(Product p : products){
     System.out.println(p);
  }
}        

ProductService -

public List<Product> getProductsByText(String searchText){
  return db.findAllByAnyColumnContaining(searchText.toLowerCase());
}        

I have used the findAllByAnyColumnContaining() method where I have a query

Output result -

SELECT p FROM Product p WHERE LOWER(p.name) LIKE %:searchText% OR LOWER(p.type) LIKE %:searchText% OR LOWER(p.place) LIKE %:searchText%        

Where I have searched in columns "name, type, place" as these are string data types.

I have used the LOWER() to handle lowercase scenarios and I am converting the search string to lowercase first before passing it to the findAllByAnyColumnContaining method.

It returns the list of matching data.

Output result -

No alt text provided for this image


Assignment 3: Search for out-of-warranty products

I have used the findByWarrantyLessThan() method which takes the current year as an argument and provides the list of products having a warranty less than the current year.

public static void getOutOfWarranty(ProductService service){
  List<Product> products = service.getOutOfWarranty(2023);
  System.out.println("Out of warranty products");
  for(Product p : products){
    System.out.println(p);
  }
}        

ProductService -

public List<Product> getOutOfWarranty(Integer warranty){
  return db.findByWarrantyLessThan(warranty);
}        

Output result -

No alt text provided for this image

To view or add a comment, sign in

More articles by Shreyash Selukar

  • Quiz Application

    Quiz Application using Spring MVC and Angular application To generate a Quiz where Admin can 'Create Quiz' and User can…

  • Java New features - JAVA 10 to JAVA 21

    1) Switch expressions Instead of ':' and break, we can use "->". Use only one format throughout the switch block.

  • Interview Questions and Bitly

    Let's discuss some Interview Questions which are commonly asked. final, finally, finalize final: In Java, the final…

  • Annotations in JAVA

    Annotations are a form of metadata which we provide to classes, methods, fields, or parameters to provide additional…

  • Reflection API

    The Reflection APIs in Java are used to access classes, methods, interfaces, fields and constructors at runtime…

  • Spring Boot and Spring Web MVC

    I created a Maven-based Spring Boot project using (start.spring.

  • Stream API in JAVA

    The Stream API in Java provides an easier and more efficient way to work with collections of data. It allows us to…

  • Pascal Triangle: Iterative, Recursive, Memoization methods

    Pascal's triangle is a triangular arrangement of numbers which is constructed in such a way that each number is the sum…

Others also viewed

Explore content categories