Springboot[1]

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

SPRING BOOT

application.properties:
spring.application.name=Springboot428

server.port=9000

spring.datasource.url=jdbc:mysql://
localhost:3306/employee1?
useSSL=false&serverTimezone=UTC
spring.datasource.driver-class-
name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Excelr@2024

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-
platform=org.hibernate.dialect.MySQL8Dialect

controller:

package com.example.demo.controller;

import com.example.demo.model.Employee;
import com.example.demo.service.EmployeeService;
import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

@PostMapping
public Employee createEmployee(@RequestBody
Employee employee) {
return employeeService.addEmployee(employee);
}

@GetMapping
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
@GetMapping("/{id}")
public ResponseEntity<Employee>
getEmployeeById(@PathVariable Long id) {
return employeeService.getEmployeeById(id)
.map(employee ->
ResponseEntity.ok().body(employee))
.orElse(ResponseEntity.notFound().build());
}

@PutMapping("/{id}")
public ResponseEntity<Employee>
updateEmployee(@PathVariable Long id, @RequestBody
Employee employeeDetails) {
Employee updatedEmployee =
employeeService.updateEmployee(id, employeeDetails);
if (updatedEmployee != null) {
return ResponseEntity.ok(updatedEmployee);
}
return ResponseEntity.notFound().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void>
deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
return ResponseEntity.ok().build();
}
}

model:
package com.example.demo.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
private double salary;

// Constructors, Getters and Setters


public Employee() {}

public Employee(String name, String department, double


salary) {
this.name = name;
this.department = department;
this.salary = salary;
}

public Long getId() {


return id;
}

public void setId(Long id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getDepartment() {


return department;
}

public void setDepartment(String department) {


this.department = department;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}
}

Repository:
package com.example.demo.repository;

import com.example.demo.model.Employee;
import
org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends
JpaRepository<Employee, Long> {
}

service:
package com.example.demo.service;

import com.example.demo.model.Employee;
import com.example.demo.repository.EmployeeRepository;
import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class EmployeeService {

@Autowired
private EmployeeRepository employeeRepository;

public Employee addEmployee(Employee employee) {


return employeeRepository.save(employee);
}

public Employee updateEmployee(Long id, Employee


employeeDetails) {
Optional<Employee> optionalEmployee =
employeeRepository.findById(id);
if (optionalEmployee.isPresent()) {
Employee employee = optionalEmployee.get();
employee.setName(employeeDetails.getName());
employee.setDepartment(employeeDetails.getDepartment())
;
employee.setSalary(employeeDetails.getSalary());
return employeeRepository.save(employee);
}
return null;
}

public void deleteEmployee(Long id) {


employeeRepository.deleteById(id);
}

public List<Employee> getAllEmployees() {


return employeeRepository.findAll();
}

public Optional<Employee> getEmployeeById(Long id) {


return employeeRepository.findById(id);
}
}
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
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>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.excelr</groupId>
<artifactId>Springboot428</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Springboot428</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</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>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>11.0.0-M20</version>
</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>

UPDATE
GET/POST

DELETE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy