POJO CLASS:
POJO (Plain Old Java Object) is a Java object not bound by any restriction other than those forced by the Java Language Specification.
Properties of POJO:
Example of a POJO class:
package com.alphalearning.example.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import org.springframework.lang.NonNull; @Entity @Table(name="demo") public class ModelClass implements Serializable { @Id @Column(name="name") private String name; @Column(name="address") @NonNull private String address; public ModelClass() { System.out.println(this.getClass().getSimpleName()+"CREATED"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Pom.xml:
This file is by default created by Spring Boot where we can add all our dependencies required to develop a spring Boot Application.
Example of pom.xml 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.project</groupId> <artifactId>demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.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-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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>
ControllerClass:
The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.
package com.alphalearning.example.controller; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; 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.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alphalearning.example.model.ModelClass; import com.alphalearning.example.service.ServiceClass; @RestController @RequestMapping(value="/") public class ControllerClass { @Autowired private ServiceClass service; public ControllerClass() { System.out.println(this.getClass().getSimpleName()+"CREATED"); } @GetMapping(value="/get/{name}") public Optional<ModelClass> getData(@PathVariable String name) { Optional<ModelClass> a=service.getData(name); return a; } @PostMapping(value="/add") public ResponseEntity<ModelClass> addData(@RequestBody ModelClass model) { System.out.println("Add datas"); ModelClass m=service.addData(model); return new ResponseEntity<ModelClass>(m, HttpStatus.OK); } @DeleteMapping(value="/delete/{name}") public void deleteData(@PathVariable String name) { service.deleteData(name); } }
ServiceClass:
The service layer which acts as a transaction boundary. It is also responsible for authorization and contains the business logic of our application. The service layer manages the domain model objects and communicates with other services and the repository layer.
package com.alphalearning.example.service; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import com.alphalearning.example.model.ModelClass; import com.alphalearning.example.repository.dao.RepositoryClass; @Service public class ServiceClass { @Autowired private RepositoryClass rep; public Optional<ModelClass> getData(String name) { Optional<ModelClass> n=rep.findById(name); return n; } public ModelClass addData(ModelClass model) { return rep.save(model); } public void deleteData(String name) { rep.deleteById(name); } }
Repository Class:
DAO stands for Data Access Object. It’s a design pattern in which a data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanisms.
package com.alphalearning.example.repository.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.alphalearning.example.model.ModelClass; @Repository public interface RepositoryClass extends JpaRepository<ModelClass, String>{ }