Spring boot crud example with mysql Jpa database with github

0

 Spring boot crud example with mysql Jpa database with github


Spring boot crud example with mysql Jpa database with github



Project Structure Looked like



Spring boot crud example project structure




AppController.java

package com.javatipsandinterview.crud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.javatipsandinterview.crud.model.StudentModel;
import com.javatipsandinterview.crud.service.StudentService;

@RestController
public class AppController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/")
    public String app() {
        return "hello world !";
    }

    @GetMapping("/id/{id}")
    public StudentModel findById(@PathVariable Long id) {
        return studentService.findById(id);
    }

    @GetMapping("/list")
    public List<StudentModel> studentList() {
        return studentService.list();
    }

    @PostMapping("/save")
    public StudentModel saveStudent(@RequestBody StudentModel studentModel) {
        return studentService.save(studentModel);
    }

    @PutMapping("/update")
    public StudentModel updateStudent(@RequestBody StudentModel studentModel) {
        return studentService.update(studentModel);
    }

    @DeleteMapping("/delete/{id}")
    public boolean deleteStudent(@PathVariable Long id) {
        return studentService.deleteStudent(id);
    }

}


StudentModel.java

package com.javatipsandinterview.crud.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class StudentModel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "username", length = 30)
    private String userName;

    @Column(name = "firstname", length = 30)
    private String firstName;

    @Column(name = "lastname", length = 30)
    private String lastName;

    @Column(name = "rollnumber")
    private String rollNumber;

    @Column(name = "age")
    private String age;

   
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String username) {
        this.userName = username;
    }

    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 getRollNumber() {
        return rollNumber;
    }

    public void setRollNumber(String rollNumber) {
        this.rollNumber = rollNumber;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "StudentModel [Id=" + id + ", username=" + userName + ", firstName=" + firstName + ", lastName="
                + lastName + ", rollNumber=" + rollNumber + ", age=" + age + "]";
    }
}



StudentRepository.java

package com.javatipsandinterview.crud.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.javatipsandinterview.crud.model.StudentModel;

@Repository
public interface StudentRepository extends JpaRepository<StudentModel, Long> {

}


StudentService.java



package com.javatipsandinterview.crud.service;

import java.util.List;

import com.javatipsandinterview.crud.model.StudentModel;

public interface StudentService {
   
    public StudentModel findById(Long id);
    public List<StudentModel> list();
    public boolean deleteStudent(Long id);
    public StudentModel update(StudentModel student);
    public StudentModel save(StudentModel student);
   

}


StudentImpl.java


package com.javatipsandinterview.crud.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.javatipsandinterview.crud.model.StudentModel;
import com.javatipsandinterview.crud.repository.StudentRepository;
import com.javatipsandinterview.crud.service.StudentService;

@Service
public class StudentImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepo;

    @Override
    public StudentModel findById(Long id) {
        // TODO Auto-generated method stub
        Optional<StudentModel> student = studentRepo.findById(id);
        if (student.isPresent()) {
            return student.get();
        }

        return null;

    }

    @Override
    public List<StudentModel> list() {

        return studentRepo.findAll();
    }

    @Override
    public boolean deleteStudent(Long id) {
        Optional<StudentModel> student = studentRepo.findById(id);
        if (student.isPresent()) {
            studentRepo.delete(student.get());
            return true;
        }
        return false;
    }

    @Override
    public StudentModel update(StudentModel student) {
        Optional<StudentModel> studentTemp = studentRepo.findById(student.getId());
        if (studentTemp.isPresent()) {
            StudentModel s = studentTemp.get();
            s.setAge(student.getAge());
            s.setFirstName(student.getFirstName());
            s.setLastName(student.getLastName());
            s.setRollNumber(student.getRollNumber());
            s.setUserName(student.getUserName());

            studentRepo.save(s);
            return s;
        }
        return null;
    }

    @Override
    public StudentModel save(StudentModel student) {
        return studentRepo.save(student);
    }

}


application.properties


spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name =com.mysql.jdbc.Driver

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
To Top