- spring data JPA covered
- Data Layer Repositories covered - Autowiring Repositories (Data Layer) to Service Layer Covered - Mapping Entities To Tables and Column attributes covered - Transient annotation covered - persisting data to DB through data layer covered
This commit is contained in:
parent
b655b6dbab
commit
1f40cc4675
@ -1,24 +1,51 @@
|
||||
package com.example.springcrashcourse.student;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Transient;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.SequenceGenerator;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class Student {
|
||||
@Id
|
||||
@SequenceGenerator(
|
||||
name = "student_sequence",
|
||||
sequenceName = "student_sequence",
|
||||
allocationSize = 1
|
||||
)
|
||||
@GeneratedValue(
|
||||
strategy = GenerationType.SEQUENCE,
|
||||
generator = "student_sequence"
|
||||
)
|
||||
private Long id;
|
||||
private String name;
|
||||
private String email;
|
||||
private LocalDate dob;
|
||||
@Transient
|
||||
private Integer age;
|
||||
|
||||
public Student() {
|
||||
|
||||
}
|
||||
|
||||
public Student(Long id, String name, String email, LocalDate dob, Integer age) {
|
||||
public Student(String name, String email, LocalDate dob) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.dob = dob;
|
||||
}
|
||||
|
||||
public Student(Long id, String name, String email, LocalDate dob) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.dob = dob;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@ -54,7 +81,7 @@ public class Student {
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return this.age;
|
||||
return Period.between(this.dob, LocalDate.now()).getYears();
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
|
||||
@ -0,0 +1,33 @@
|
||||
package com.example.springcrashcourse.student;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class StudentConfig {
|
||||
|
||||
@Bean
|
||||
CommandLineRunner commandLineRunner(StudentRepository repository) {
|
||||
return args -> {
|
||||
Student mariam = new Student(
|
||||
"Mariam",
|
||||
"mariam.jamal@gmail.com",
|
||||
LocalDate.of(2000, Month.JANUARY, 5)
|
||||
);
|
||||
|
||||
Student alex = new Student(
|
||||
"Alex",
|
||||
"alex.jamal@gmail.com",
|
||||
LocalDate.of(2004, Month.JANUARY, 5)
|
||||
);
|
||||
|
||||
repository.saveAll(List.of(mariam, alex));
|
||||
};
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.example.springcrashcourse.student;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface StudentRepository extends JpaRepository<Student, Long> {
|
||||
|
||||
}
|
||||
@ -4,19 +4,20 @@ import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StudentService {
|
||||
|
||||
private final StudentRepository studentRepository;
|
||||
|
||||
@Autowired
|
||||
public StudentService(StudentRepository studentRepository) {
|
||||
this.studentRepository = studentRepository;
|
||||
}
|
||||
|
||||
public List<Student> getStudents() {
|
||||
return List.of(
|
||||
new Student(
|
||||
1L,
|
||||
"Mariam",
|
||||
"mariam.jamal@gmail.com",
|
||||
LocalDate.of(2000, Month.JANUARY, 5),
|
||||
21
|
||||
)
|
||||
);
|
||||
return studentRepository.findAll();
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/student
|
||||
spring.datasource.username=
|
||||
spring.datasource.password=
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=testing
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgresSQLDialect
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
Loading…
x
Reference in New Issue
Block a user