17- department-service-A(coding part)

CODING


Entity Class

src/main/java  --> com.zeek.departmentserviceA.entity --> Department.java

package com.zeek.departmentserviceA.entity;

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="departments")
public class Department{


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long departmentId;
   
    @Column(name="department_name")
    private String departmentName;
   
    @Column(name="department_address")
    private String departmentAddress;
   
    @Column(name="department_code")
    private String departmentCode;
   
   
  //Constructors using superclass
    public Department() {
        super();
    }
   
    //Constructors using fields
    public Department(Long departmentId, String departmentName, String departmentAddress, String departmentCode) {
        super();
        this.departmentId = departmentId;
        this.departmentName = departmentName;
        this.departmentAddress = departmentAddress;
        this.departmentCode = departmentCode;
    }
   
    //Getters and Setters
        public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public String getDepartmentAddress() {
        return departmentAddress;
    }

    public void setDepartmentAddress(String departmentAddress) {
        this.departmentAddress = departmentAddress;
    }

    public String getDepartmentCode() {
        return departmentCode;
    }

    public void setDepartmentCode(String departmentCode) {
        this.departmentCode = departmentCode;
    }


}






Repository Interface

src/main/java  -->com.zeek.departmentserviceA.repository -> DepartmentRepository.java

package com.zeek.departmentserviceA.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.zeek.departmentserviceA.entity.Department;

public interface DepartmentRepository extends JpaRepository<Department, Long> {

 Department findByDepartmentId(Long departmentId);

}






Service Interface

src/main/java  -->com.zeek.departmentserviceA.service -> DepartmentService.java

package com.zeek.departmentserviceA.service;

import java.util.List;
import java.util.Optional;
import com.zeek.departmentserviceA.entity.Department;


public interface DepartmentService {
   
     Department saveDepartment(Department department);
   
    List<Department> getAllDepartments();
   
    Department findDepartmentById(Long departmentId);

}




ServiceImpl Class

src/main/java --> com.zeek.departmentserviceA.service.impl  -->DepartmentServiceImpl.java

package com.zeek.departmentserviceA.service.impl;

import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import com.zeek.departmentserviceA.entity.Department;
import com.zeek.departmentserviceA.repository.DepartmentRepository;
import com.zeek.departmentserviceA.service.DepartmentService;


@Service
public class DepartmentServiceImpl implements DepartmentService {
   
private DepartmentRepository departmentRepository;
   
    public DepartmentServiceImpl(DepartmentRepository departmentRepository) {
        super();
        this.departmentRepository = departmentRepository;
    }

    @Override
    public Department saveDepartment(Department department) {
    return departmentRepository.save(department);
    }

    @Override
    public List<Department> getAllDepartments() {
        return departmentRepository.findAll();
    }

    @Override
    public Department findDepartmentById(Long departmentId) {
        return departmentRepository.findByDepartmentId(departmentId);
    }

}





Controller Class

src/main/java --> com.zeek.departmentserviceA.controller  -->DepartmentController.java

package com.zeek.departmentserviceA.controller;

import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.zeek.departmentserviceA.entity.Department;
import com.zeek.departmentserviceA.service.DepartmentService;


@RestController
@RequestMapping("/departments")
public class DepartmentController {

    private DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {
        super();
        this.departmentService = departmentService;
    }

    // build create department REST API
    // http://localhost:8080/departments------- POST request on Postman App

    @PostMapping()
    public ResponseEntity<Department> saveDepartment(@RequestBody Department department) {

        return new ResponseEntity<Department>(departmentService.saveDepartment(department), HttpStatus.CREATED);
    }

    // build get all departments REST API
    // http://localhost:8080/departments------- GET request on Postman App
   
    @GetMapping
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }
   
    // build get department by id REST API
        // http://localhost:8080/departments/1---------- GET request on Postman App
           
       @GetMapping("{id}")
            public Department getDepartmentById(@PathVariable("id") long departmentId){
             return departmentService.findDepartmentById(departmentId);
        }

}




application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/department?useSSL=false
spring.datasource.username=root
spring.datasource.password=root

# Hibernate properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

#create,create-drop
spring.jpa.hibernate.ddl-auto=update




CONSOLE


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.4)

2023-05-30 19:44:00.545  INFO 26260 --- [           main] c.z.d.DepartmentServiceAApplication      : Starting DepartmentServiceAApplication using Java 18.0.1 on Zeeshan with PID 26260 (C:\Users\zeesh\eclipse-workspace\A_workspace\department-service-A\target\classes started by ZeeK in C:\Users\zeesh\eclipse-workspace\A_workspace\department-service-A)
2023-05-30 19:44:00.547  INFO 26260 --- [           main] c.z.d.DepartmentServiceAApplication      : No active profile set, falling back to 1 default profile: "default"
2023-05-30 19:44:01.112  INFO 26260 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-30 19:44:01.166  INFO 26260 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 46 ms. Found 1 JPA repository interfaces.
2023-05-30 19:44:01.748  INFO 26260 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-05-30 19:44:01.760  INFO 26260 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-30 19:44:01.760  INFO 26260 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.58]
2023-05-30 19:44:01.853  INFO 26260 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-30 19:44:01.853  INFO 26260 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1258 ms
2023-05-30 19:44:02.062  INFO 26260 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-05-30 19:44:02.130  INFO 26260 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.5.Final
2023-05-30 19:44:02.322  INFO 26260 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-05-30 19:44:02.421  INFO 26260 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-05-30 19:44:02.562  INFO 26260 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-05-30 19:44:02.584  INFO 26260 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2023-05-30 19:44:03.186  INFO 26260 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-30 19:44:03.192  INFO 26260 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-30 19:44:03.467  WARN 26260 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-05-30 19:44:03.753  INFO 26260 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-30 19:44:03.762  INFO 26260 --- [           main] c.z.d.DepartmentServiceAApplication      : Started DepartmentServiceAApplication in 3.595 seconds (JVM running for 4.076)





Comments

Popular posts from this blog

INDEX OF JAVA 8 and MICROSERVICES BLOG