This an application to test crud operations in springboot
This commit is contained in:
124
src/main/java/com/ebrains/cruddemo/CruddemoApplication.java
Normal file
124
src/main/java/com/ebrains/cruddemo/CruddemoApplication.java
Normal file
@ -0,0 +1,124 @@
|
||||
package com.ebrains.cruddemo;
|
||||
|
||||
import com.ebrains.cruddemo.dao.StudentDAO;
|
||||
import com.ebrains.cruddemo.entity.Student;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CruddemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(CruddemoApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner commandLineRunner(StudentDAO studentDAO) {
|
||||
return runner ->{
|
||||
createMultipleStudent(studentDAO);
|
||||
//queryForStudent(studentDAO);
|
||||
//queryForStudentByLastName(studentDAO);
|
||||
//updateStudent(studentDAO);
|
||||
//deleteStudent(studentDAO);
|
||||
//deleteAllStudent(studentDAO);
|
||||
};
|
||||
}
|
||||
|
||||
private void deleteAllStudent(StudentDAO studentDAO) {
|
||||
int numOfRowDeleted = studentDAO.deleteAll();
|
||||
System.out.println("Number of row deleted: " + numOfRowDeleted);
|
||||
}
|
||||
|
||||
|
||||
private void deleteStudent(StudentDAO studentDAO) {
|
||||
Student student = studentDAO.findById(3);
|
||||
System.out.println("Student to be deleted: "+student);
|
||||
studentDAO.delete(3);
|
||||
}
|
||||
|
||||
private void updateStudent(StudentDAO studentDAO) {
|
||||
//retrieve the student base on id
|
||||
Student student = studentDAO.findById(1);
|
||||
|
||||
//change the lastname
|
||||
System.out.println("updating the student ...");
|
||||
student.setLastName("Lobby");
|
||||
|
||||
//update the student
|
||||
studentDAO.update(student);
|
||||
|
||||
//Display the updated student
|
||||
System.out.println("student updated: "+student);
|
||||
}
|
||||
|
||||
private void queryForStudentByLastName(StudentDAO studentDAO) {
|
||||
//get the list of student
|
||||
List<Student> students = studentDAO.findByLastName("Doe");
|
||||
for (Student student : students) {
|
||||
System.out.println(student);
|
||||
}
|
||||
}
|
||||
|
||||
private void queryForStudent(StudentDAO studentDAO) {
|
||||
//get the list of student
|
||||
List<Student> students = studentDAO.findAll();
|
||||
|
||||
//display the student
|
||||
for (Student student : students) {
|
||||
System.out.println(student);
|
||||
}
|
||||
}
|
||||
|
||||
private void createStudent(StudentDAO studentDAO) {
|
||||
System.out.println("Creating student ....");
|
||||
Student student = new Student("paul","okeke","paul@gmail.com");
|
||||
|
||||
//saving the student object
|
||||
System.out.println("saving student ....");
|
||||
studentDAO.save(student);
|
||||
|
||||
System.out.println("Student saved successfully.Generated Id: " + student.getId());
|
||||
}
|
||||
|
||||
private void createMultipleStudent(StudentDAO studentDAO) {
|
||||
System.out.println("Creating student ....");
|
||||
Student student = new Student("John","smith","smith@gmail.com");
|
||||
Student student1 = new Student("Mary","Doe","doe@gmail.com");
|
||||
Student student2= new Student("Eric","onu","onu@gmail.com");
|
||||
|
||||
//saving the multiple student object
|
||||
studentDAO.save(student);
|
||||
studentDAO.save(student1);
|
||||
studentDAO.save(student2);
|
||||
|
||||
System.out.println("Students saved successfully");
|
||||
}
|
||||
|
||||
private void readStudent(StudentDAO studentDAO) {
|
||||
//create a student object
|
||||
System.out.println("Creating student ....");
|
||||
Student student = new Student("Onowu","Igbo","igbo@gmail.com");
|
||||
|
||||
//save the save
|
||||
studentDAO.save(student);
|
||||
System.out.println("Student saved successfully");
|
||||
|
||||
//display id of the saved student
|
||||
long theId= student.getId();
|
||||
System.out.println("saved student successfully.Generated Id: " + theId);
|
||||
|
||||
//retrieved the student based on the id ie the primary key
|
||||
System.out.println("retrieving student saved successfully");
|
||||
//Student savedStudent= studentDAO.findById(theId);
|
||||
|
||||
//display the student
|
||||
//System.out.println("student retrieved successfully: " + savedStudent);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
22
src/main/java/com/ebrains/cruddemo/dao/StudentDAO.java
Normal file
22
src/main/java/com/ebrains/cruddemo/dao/StudentDAO.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.ebrains.cruddemo.dao;
|
||||
|
||||
import com.ebrains.cruddemo.entity.Student;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface StudentDAO {
|
||||
void save(Student student);
|
||||
|
||||
Student findById(Integer id);
|
||||
|
||||
List<Student> findAll();
|
||||
|
||||
List<Student> findByLastName(String lastName);
|
||||
|
||||
void update(Student student);
|
||||
|
||||
void delete(Integer id);
|
||||
|
||||
int deleteAll();
|
||||
|
||||
}
|
||||
79
src/main/java/com/ebrains/cruddemo/dao/StudentDAOImpl.java
Normal file
79
src/main/java/com/ebrains/cruddemo/dao/StudentDAOImpl.java
Normal file
@ -0,0 +1,79 @@
|
||||
package com.ebrains.cruddemo.dao;
|
||||
|
||||
import com.ebrains.cruddemo.entity.Student;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public class StudentDAOImpl implements StudentDAO {
|
||||
//we define feilds for entity manager
|
||||
private EntityManager entityManager;
|
||||
|
||||
//eject entity manager using constructor injection
|
||||
public StudentDAOImpl(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
//implement the save method
|
||||
//this method saves the studnet to the database
|
||||
@Transactional
|
||||
@Override
|
||||
public void save(Student student) {
|
||||
entityManager.persist(student);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Student findById(Integer id) {
|
||||
//Student.class is the entity class
|
||||
return entityManager.find(Student.class, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Student> findAll() {
|
||||
//create the query
|
||||
TypedQuery<Student> query = entityManager.createQuery("from Student", Student.class);
|
||||
//TypedQuery<Student> query = entityManager.createQuery("from Student order by lastName desc", Student.class);
|
||||
|
||||
//return the list of student
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Student> findByLastName(String lastName) {
|
||||
//create the query
|
||||
TypedQuery<Student> query = entityManager.createQuery("from Student where lastName=:theData", Student.class);
|
||||
|
||||
//set query parameter
|
||||
query.setParameter("theData", lastName);
|
||||
|
||||
//return the list of student with the last name
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void update(Student student) {
|
||||
entityManager.merge(student);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void delete(Integer id) {
|
||||
//retrieve the student
|
||||
Student student = entityManager.find(Student.class, id);
|
||||
|
||||
//delete the student
|
||||
entityManager.remove(student);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public int deleteAll() {
|
||||
return entityManager.createQuery("delete from Student").executeUpdate();
|
||||
|
||||
}
|
||||
}
|
||||
70
src/main/java/com/ebrains/cruddemo/entity/Student.java
Normal file
70
src/main/java/com/ebrains/cruddemo/entity/Student.java
Normal file
@ -0,0 +1,70 @@
|
||||
package com.ebrains.cruddemo.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name="student")
|
||||
public class Student {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
@Column(name = "first_name")
|
||||
private String firstName;
|
||||
@Column(name = "last_name")
|
||||
private String lastName;
|
||||
@Column(name = "email")
|
||||
private String email;
|
||||
|
||||
public Student() {
|
||||
|
||||
}
|
||||
public Student(String firstName, String lastName, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
17
src/main/resources/application.properties
Normal file
17
src/main/resources/application.properties
Normal file
@ -0,0 +1,17 @@
|
||||
spring.application.name=cruddemo
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/student_tracker
|
||||
spring.datasource.username=springstudent
|
||||
spring.datasource.password=springstudent
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
|
||||
#Turn off the springboot logo
|
||||
spring.main.banner-mode=off
|
||||
|
||||
#reduces logging level
|
||||
logging.level.root=warn
|
||||
|
||||
#Add logging configs to displays SQL statements
|
||||
logging.level.org.hibernate.SQL= debug
|
||||
logging.level.org.hibernate.orm.jdbc.bind=trace
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
package com.ebrains.cruddemo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class CruddemoApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user