This commit is contained in:
Vombit committed 2026-10-07 20:04:08 +05:00
1 parent a5d46d500b
commit 2675dc2681
23 files changed
+1358

No files matched your search

@@ -0,0 +1,13 @@
package com.labwork.MySpringBoot2Dbase;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBoot2DbaseApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBoot2DbaseApplication.class, args);
}
}
@@ -0,0 +1,123 @@
package com.labwork.MySpringBoot2Dbase.controller;
import com.labwork.MySpringBoot2Dbase.entity.Student;
import com.labwork.MySpringBoot2Dbase.entity.Subject;
import com.labwork.MySpringBoot2Dbase.service.StudentService;
import com.labwork.MySpringBoot2Dbase.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
public class MyController {
@Autowired
private StudentService studentService;
@Autowired
private SubjectService subjectService;
@GetMapping("/students")
public List<Student> allStudents() {
return studentService.getAllStudents();
}
@GetMapping("/students/{id}")
public Object getStudent(@PathVariable("id") int id) {
Student student = studentService.getStudent(id);
return student != null ? student : OperationStatus.FAILED;
}
@PostMapping("/students")
public OperationStatus saveStudent(@RequestBody Student student) {
try {
if (student.getId() != null && student.getId() == 0) {
student.setId(null);
}
studentService.saveStudent(student);
return OperationStatus.OK;
} catch (Exception e) {
return OperationStatus.FAILED;
}
}
@PutMapping("/students")
public OperationStatus updateStudent(@RequestBody Student student) {
try {
if (student.getId() == null || student.getId() == 0
|| studentService.getStudent(student.getId()) == null) {
return OperationStatus.FAILED;
}
studentService.saveStudent(student);
return OperationStatus.OK;
} catch (Exception e) {
return OperationStatus.FAILED;
}
}
@DeleteMapping("/students/{id}")
public OperationStatus deleteStudent(@PathVariable("id") int id) {
try {
if (studentService.getStudent(id) == null) {
return OperationStatus.FAILED;
}
studentService.deleteStudent(id);
return OperationStatus.OK;
} catch (Exception e) {
return OperationStatus.FAILED;
}
}
@GetMapping("/subjects")
public List<Subject> allSubjects() {
return subjectService.getAllSubjects();
}
@GetMapping("/subjects/{id}")
public Object getSubject(@PathVariable("id") int id) {
Subject subject = subjectService.getSubject(id);
return subject != null ? subject : OperationStatus.FAILED;
}
@PostMapping("/subjects")
public OperationStatus saveSubject(@RequestBody Subject subject) {
try {
if (subject.getId() != null && subject.getId() == 0) {
subject.setId(null);
}
subjectService.saveSubject(subject);
return OperationStatus.OK;
} catch (Exception e) {
return OperationStatus.FAILED;
}
}
@PutMapping("/subjects")
public OperationStatus updateSubject(@RequestBody Subject subject) {
try {
if (subject.getId() == null || subject.getId() == 0
|| subjectService.getSubject(subject.getId()) == null) {
return OperationStatus.FAILED;
}
subjectService.saveSubject(subject);
return OperationStatus.OK;
} catch (Exception e) {
return OperationStatus.FAILED;
}
}
@DeleteMapping("/subjects/{id}")
public OperationStatus deleteSubject(@PathVariable("id") int id) {
try {
if (subjectService.getSubject(id) == null) {
return OperationStatus.FAILED;
}
subjectService.deleteSubject(id);
return OperationStatus.OK;
} catch (Exception e) {
return OperationStatus.FAILED;
}
}
}
@@ -0,0 +1,21 @@
package com.labwork.MySpringBoot2Dbase.controller;
import com.fasterxml.jackson.annotation.JsonValue;
public enum OperationStatus {
OK("ok"),
FAILED("failed");
private final String value;
OperationStatus(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
}
@@ -0,0 +1,19 @@
package com.labwork.MySpringBoot2Dbase.dao;
import com.labwork.MySpringBoot2Dbase.entity.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StudentDAO {
List<Student> getAllStudents();
Student saveStudent(Student student);
Student getStudent(int id);
void deleteStudent(int id);
}
@@ -0,0 +1,44 @@
package com.labwork.MySpringBoot2Dbase.dao;
import com.labwork.MySpringBoot2Dbase.entity.Student;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Slf4j
public class StudentDAOImpl implements StudentDAO {
@Autowired
private EntityManager entityManager;
@Override
public List<Student> getAllStudents() {
Query query = entityManager.createQuery("from Student");
List<Student> allStudents = query.getResultList();
log.info("getAllStudents" + allStudents);
return allStudents;
}
@Override
public Student saveStudent(Student student) {
return entityManager.merge(student);
}
@Override
public Student getStudent(int id) {
return entityManager.find(Student.class, id);
}
@Override
public void deleteStudent(int id) {
Query query = entityManager.createQuery("delete from Student where id =:studentId");
query.setParameter("studentId", id);
query.executeUpdate();
}
}
@@ -0,0 +1,19 @@
package com.labwork.MySpringBoot2Dbase.dao;
import com.labwork.MySpringBoot2Dbase.entity.Subject;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SubjectDAO {
List<Subject> getAllSubjects();
Subject saveSubject(Subject subject);
Subject getSubject(int id);
void deleteSubject(int id);
}
@@ -0,0 +1,40 @@
package com.labwork.MySpringBoot2Dbase.dao;
import com.labwork.MySpringBoot2Dbase.entity.Subject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class SubjectDAOImpl implements SubjectDAO {
@Autowired
private EntityManager entityManager;
@Override
public List<Subject> getAllSubjects() {
Query query = entityManager.createQuery("from Subject");
return query.getResultList();
}
@Override
public Subject saveSubject(Subject subject) {
return entityManager.merge(subject);
}
@Override
public Subject getSubject(int id) {
return entityManager.find(Subject.class, id);
}
@Override
public void deleteSubject(int id) {
Query query = entityManager.createQuery("delete from Subject where id =:subjectId");
query.setParameter("subjectId", id);
query.executeUpdate();
}
}
@@ -0,0 +1,29 @@
package com.labwork.MySpringBoot2Dbase.entity;
import jakarta.persistence.*;
import lombok.*;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "STUDENTS")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "surname")
private String surname;
@Column(name = "faculty")
private String faculty;
@Column(name = "age")
private Integer age;
}
@@ -0,0 +1,20 @@
package com.labwork.MySpringBoot2Dbase.entity;
import jakarta.persistence.*;
import lombok.*;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "SUBJECTS")
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
}
@@ -0,0 +1,19 @@
package com.labwork.MySpringBoot2Dbase.service;
import com.labwork.MySpringBoot2Dbase.entity.Student;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface StudentService {
List<Student> getAllStudents();
Student saveStudent(Student student);
Student getStudent(int id);
void deleteStudent(int id);
}
@@ -0,0 +1,41 @@
package com.labwork.MySpringBoot2Dbase.service;
import com.labwork.MySpringBoot2Dbase.dao.StudentDAO;
import com.labwork.MySpringBoot2Dbase.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDAO studentDAO;
@Override
@Transactional
public List<Student> getAllStudents() {
return studentDAO.getAllStudents();
}
@Override
@Transactional
public Student saveStudent(Student student) {
return studentDAO.saveStudent(student);
}
@Override
@Transactional
public Student getStudent(int id) {
return studentDAO.getStudent(id);
}
@Override
@Transactional
public void deleteStudent(int id) {
studentDAO.deleteStudent(id);
}
}
@@ -0,0 +1,19 @@
package com.labwork.MySpringBoot2Dbase.service;
import com.labwork.MySpringBoot2Dbase.entity.Subject;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface SubjectService {
List<Subject> getAllSubjects();
Subject saveSubject(Subject subject);
Subject getSubject(int id);
void deleteSubject(int id);
}
@@ -0,0 +1,41 @@
package com.labwork.MySpringBoot2Dbase.service;
import com.labwork.MySpringBoot2Dbase.dao.SubjectDAO;
import com.labwork.MySpringBoot2Dbase.entity.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class SubjectServiceImpl implements SubjectService {
@Autowired
private SubjectDAO subjectDAO;
@Override
@Transactional
public List<Subject> getAllSubjects() {
return subjectDAO.getAllSubjects();
}
@Override
@Transactional
public Subject saveSubject(Subject subject) {
return subjectDAO.saveSubject(subject);
}
@Override
@Transactional
public Subject getSubject(int id) {
return subjectDAO.getSubject(id);
}
@Override
@Transactional
public void deleteSubject(int id) {
subjectDAO.deleteSubject(id);
}
}
@@ -0,0 +1,10 @@
spring.application.name=MySpringBoot2Dbase
spring.datasource.url=jdbc:h2:file:./testdb/students
spring.jpa.open-in-view=false
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=none
spring.h2.console.enabled=true
@@ -0,0 +1,13 @@
package com.labwork.MySpringBoot2Dbase;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MySpringBoot2DbaseApplicationTests {
@Test
void contextLoads() {
}
}