Table of contents
No headings in the article.
Student CRUD Project
Create a database Schema: Create a database and Create a student table
Create a SpringBoot application in STS name it Studentdal (Data access layer), add dependencies JPA and MySQL driver
Create the model class Student.java in module com.user.studentdal.Entities Define the data types and it’s fields. Generate getters and setters Generate toString method Mark the class with @Entity , @Table(name=”tabname”) Mark id field with @Id If other fields name and column name are different then use @Column(name=”colomnname”)
Next step is to create studentrepository which is Data access layer Create an interface StudentRepository which implements CRUDRepository This takes 2 generic values, First one is the entity name and second is the type of Id public interface StudentRepository extends CrudRepository { }
As the Id field is auto generated we need to specify the tag @GeneratedValue(strategy=GenerationType.AUTO)
Configure the data source in application.properties file
Create operations in CRUD Open the StudentApplicationTest.java file
Create a StudentRepository type object testCreateStudent is a function used to test if we could add student object to our database Create a Student object and set the attribute values StudentRepository,save(student) is used to insert the data into our database Read and Update operation in CRUD Open the StudentApplicationTest.java file
Tap into student object using StudentRepository use findOne or findById method to fetch the record from the db and to read the data you can just print it using sys.out To update the data, fetch it and then set the value and save
DELETE operation in CRUD
Link to github repo: github.com/shashankshet/Java-Spring-Boot/tr..