
Table Content
1. What is CrudRepository Interface
2. The API we’ll build
3. Database and Table Creating
4. Creating New Spring Boot Project
5. pom.xml
6. application.properties
7. Student.java
8. StudentRepository.java
9. StudentController.java
10. SpringBootRestfulCrudApplication.java
11. Let’s the test application
What is CrudRepository Interface
The API we'll build
URLs | Method | Description |
---|---|---|
/students/add | @PostMapping | Create a new student |
/students/all | @GettMapping | Retrieve all students |
/students/student/{id} | @GettMapping | Get records of a particular student |
/students/update/{id} | @PuttMapping | Update existing student records |
/students/delete/{id} | @DeleteMapping | Delete a particular student record |
Database and Table Creating
CREATE TABLE `tbl_student` (
`student_id` int(11) NOT NULL,
`student_firstname` varchar(15) NOT NULL,
`student_lastname` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Creating New Spring Boot Project




See below complete project directory structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.onlyxcodes</groupId>
<artifactId>SpringBootRestfulCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootRestfulCRUD</name>
<description>Spring Boot Restful CRUD JPA MySQL</description>
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
# MySQL database connecting utility
spring.datasource.url=jdbc:mysql://localhost:3306/springbootrestcrud_db
spring.datasource.username=root
spring.datasource.password=
# JPA property utility
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
Settings properties elements (spring.datasource.*) in the application.properties file offer the DataSource configuration.
With the help of the properties configuration, the application code and configuration are separated. We can import data source configurations in this manner from systems that are configuration provider systems.
The properties of the MySQL database are shown in the setup above.
spring.datasource.url=jdbc:mysql://localhost:3306/springbootrestcrud_db :- Your database's location is indicated by the url. You're indicating that my database may be found at http://localhost:3306/springbootrestcrud_db, where springbootrestcrud_db is the name of the database.
spring.datasource.username=root:- The database username is the root.
spring.datasource.password=:- This shows the password for the database. Since there is no password in my database, I left this blank.
spring.jpa.hibernate.ddl-auto:- This is a Hibernate feature that more precisely regulates the behavior. For more on the upgrade, see below.
Update: After comparing the generated object model (annotations or XML) with the current schema, Hibernate modifies the schema by the differences. Even if the program no longer needs the existing tables or columns, it never deletes them.
spring.jpa.properties.hibernate.show_sql=true:- The following hibernate property can be set up to format the output SQL from JPA.
Student.java
package com.onlyxcodes.app.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 = "tbl_student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "student_id")
private int id;
@Column(name = "student_firstname")
private String firstname;
@Column(name = "student_lastname")
private String lastname;
public int getId() {
return id;
}
public void setId(int 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;
}
}
StudentRepository.java
After that, we must construct a fundamental repository layer that enables us to carry out CRUD operations on members of the above Student entity class.
We don't need to start from scratch with our own DAO implementation because Spring Data JPA is what we're utilizing. To create a functional repository implementation, we only need to extend the CrudRepository interface:
In our example, the model is Student and the ID type is Integer, and the CrudRespository interface accepts both.
package com.onlyxcodes.app.repository;
import org.springframework.data.repository.CrudRepository;
import com.onlyxcodes.app.model.Student;
public interface StudentRepository extends CrudRepository<Student, Integer>{
}
StudentController.java
package com.onlyxcodes.app.controller;
import java.util.List;
import java.util.Optional;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.onlyxcodes.app.model.Student;
import com.onlyxcodes.app.repository.StudentRepository;
@RestController
@RequestMapping("students")
public class StudentController {
@Autowired
StudentRepository studentrepository;
// retrieve all student from database
@GetMapping("all")
public List<Student> getAllStudent()
{
List<Student> student=(List<Student>) studentrepository.findAll();
return student;
}
// insert new student into database
@PostMapping("add")
public Student addStudent(@RequestBody Student student)
{
return studentrepository.save(student);
}
// get particular student by their ID
@GetMapping("student/{id}")
public Optional<Student> getStudentId(@PathVariable int id)
{
return studentrepository.findById(id);
}
// update existing student
@PutMapping("update/{id}")
public Student updateStudent(@RequestBody Student student)
{
return studentrepository.save(student);
}
// delete particular student from database
@DeleteMapping("delete/{id}")
public void deleteStudent(@PathVariable int id)
{
studentrepository.deleteById(id);
}
}
The annotation @GetMapping denotes that a GET request is processed by the method.
An annotation called @PostMapping indicates that a function handles a POST request.
The annotation @PutMapping shows that a function handles a PUT request.
The annotation @DeleteMapping indicates that a method handles a DELETE request.
SpringBootRestfulCrudApplication.java
package com.onlyxcodes.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRestfulCrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestfulCrudApplication.class, args);
}
}
Let's the test application
{
"firstname": "hamid",
"lastname": "shaikh"
}



{
"id": 1,
"firstname": "ankit",
"lastname": "patel"
}




Related Articles –
Sending Email using Spring Boot Rest API
Spring Boot MVC CRUD Example With MySQL
Consuming (CRUD) Restful Web Service in Java
Download Codes
No comments:
Post a Comment