Skip to content
Snippets Groups Projects
Commit a206ccbc authored by aishwarya's avatar aishwarya
Browse files

assignment3 changes

parent a2732b6e
Branches
No related tags found
No related merge requests found
<?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">
<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>
<groupId>org.example</groupId>
<artifactId>JavaFristProject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JavaAssignment-3</name>
<description>Demo project for Spring Boot</description>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</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>
package org.example.Controller;
import org.example.entity.Employees;
import org.example.services.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeservice;
@GetMapping(path ="/{id}")
public Employees getEmployees(@PathVariable int id) {
return employeeservice.getEmployees(id);
}
@PostMapping
public Employees addEmployees(@RequestBody Employees employee) {
return employeeservice.save(employee);
}
@PutMapping
public Employees updateEmployees(@RequestBody Employees employee) {
return employeeservice.update(employee);
}
@DeleteMapping
public Employees deleteEmployees(@RequestBody Employees employee) {
return employeeservice.delete(employee.getId());
}
}
\ No newline at end of file
package org.example;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages="")
@EnableAutoConfiguration
@SpringBootApplication//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
......@@ -9,5 +18,6 @@ public class Main {
System.out.println("Hello and welcome to my first Java Project!");
String name = "Niveus Project";
System.out.println("Hello "+name );
SpringApplication.run(Main.class, args);
}
}
\ No newline at end of file
package org.example.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
public class Employees {
@Id
@GeneratedValue
private int id;
private String name;
private int age;
private int salary;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.example.repository;
import org.example.entity.Employees;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employees,Integer> {
}
package org.example.services;
import org.example.entity.Employees;
import org.example.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public Employees save(Employees employee)
{
return employeeRepository.save(employee);
}
public Employees getEmployees(int id)
{
return employeeRepository.findById(id).get();
}
public Employees update(Employees employee)
{
Employees emp=employeeRepository.findById(employee.getId()).get();
emp.setName(employee.getName());
emp.setAge(employee.getAge());
emp.setSalary(employee.getSalary());
return employeeRepository.save(employee);
}
public Employees delete(int id)
{
employeeRepository.deleteById(id);
Employees emp=new Employees();
// emp.setStatus("deleted");
return emp;
}
public EmployeeRepository getEmployeeRepository(){
return employeeRepository;
}
public void setEmployeeRepository(EmployeeRepository employeeRepository){
this.employeeRepository=employeeRepository;
}
}
spring.jpa.database=POSTGRESQL
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/javaassignment
spring.datasource.username=postgres
spring.datasource.password=aishwarya@123
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect
#error.whitelabel.enabled=false
#server.port=5000
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment