This Spring boot Ajax Crud tutorial will teach you how to do basic database functions that are CREATE RETIEVE data using MySQL Database. The main purpose of we have use Ajax is without refresh the page.
The package structure, you must following the standard package structure of spring boot and spring framework.for the example i have created the project name SpringCrud
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | com.example.student -------------------->studentApplication.java // This is the main class of the programme where the programming runs. Spring Boot MVC Structure | -------------------->Studentservice.java com.example.student.service | --------------------->StudentController com.example.student.controller | ----------------------> Studentrepository.java com.example.student.repository | -----------------------> Student.java com.example.student.Entity |
First you must Create the package com.example.student.Entity. inside the package you have to create the class.
- Entity annotation @Entity indicate as Entity of class.
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | @Entity public class Student { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long id; private String studentname; private String course; private int fee; public Student() { } public Student(Long id, String studentname, String course, int fee) { this.id = id; this.studentname = studentname; this.course = course; this.fee = fee; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getStudentname() { return studentname; } public void setStudentname(String studentname) { this.studentname = studentname; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public int getFee() { return fee; } public void setFee(int fee) { this.fee = fee; } } |
after that you have to Create the package com.example.student.controller . inside the package you have to create the class
StudentController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | @RestController @RequestMapping("Student") public class StudentController { @Autowired private StudentService service; @RequestMapping(value = "/save", method = RequestMethod.POST) public String saveStudent(@ModelAttribute("student") Student std) { service.save(std); return "{\"status\":\"success\"}"; } @RequestMapping(value = "/list", method = RequestMethod.GET) public List<Student> listStudents() { List<Student> liststudent = service.listAll(); return liststudent; } @RequestMapping("/edit/{id}") public ModelAndView showEditStudentPage(@PathVariable(name = "id") int id) { ModelAndView mav = new ModelAndView("new"); Student std = service.get(id); mav.addObject("student", std); return mav; } @RequestMapping("/delete/{id}") public String deletestudent(@PathVariable(name = "id") int id) { service.delete(id); return "redirect:/"; } } |
First you must Create the package com.example.student.service . inside the package you have to create the class
Studentservice.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Service public class StudentService { @Autowired private StudentRepository repo; public List<Student> listAll() { return repo.findAll(); } public void save(Student std) { repo.save(std); } public Student get(long id) { return repo.findById(id).get(); } public void delete(long id) { repo.deleteById(id); } |
StudentRepository.java
First you must Create the package com.example.Student.repository inside the package you have to create the Interface here.
1 2 3 4 | @Repository public interface StudentRepository extends JpaRepository<Student, Long> { } |
After that you must set database path and project configuration in application.properties file.
1 2 3 4 5 6 7 8 9 10 11 12 | spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:mysql://localhost:3306/lindaschool?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC server.error.whitelabel.enabled=false server.port=7060 spring.datasource.username=root spring.datasource.password= spring.jpa.open-in-view=false spring.thymeleaf.cache=false api.base.path = http://localhost:7060 |
i have attached the video link below. which will do this tutorials step by step.