SpringBoot REST API CRUD Operations using Spring Data JPA | Postman | MySql #springboottutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to HJ programming Solutions so in today's video we are going to create a springboard project and we will perform the crud operations using rest API so these many rest points we are going to create very first one is to get all the students so if I hit this request in my Postman then I should get all the students which are there in my student database after that if I hit this request where I am passing a particular student ID then I should get only that student details for example if I am passing here the ID 1 or the roll number one then I should get the details of the student whose roll number is 1. same way we need to create one request which will create a new student or which will add new student in your student database right after that we will see how we can update the student details so I have one rest point here okay so localhost colon 8080 student then update so here I will pass the student ID let's suppose I am passing student ID as a 2 then I will able to update the details for the student whose ID is 2 and after that if you want to delete a particular student then you can have this request you can pass here the ID which you want to delete and once you hit this request then that student details should be deleted that student record should be deleted from our table so these are the crude operations and these are the rest points we are going to create today in our application now which are the dependencies we need to create this project see here very first dependency we need is a spring web dependency and why we need this dependency because we are creating a spring rest apis right so we need to add the spring web dependency then Dev tools so that every time if I make any changes then I don't have to restart the server every time automatically if you are making any changes and as soon as you save that file then automatically the server will reload okay so we don't have to restart the server again and again so we can add Dev tools dependency after that MySQL connector because I am going to connect my application to the MySQL and here we are going to use a spring data jpa okay so that's the reason we need spring dependency so these are the dependencies which are required to create our project okay now let's go to the STS and try to create the project okay so let me just go here okay so see here this is very clean window I have I have not created any project yet okay so how to create the spring boot project in the STS very simple you just have to go to this file and here you just click on the new and you have an option spring starter project you just have to click on that option then here you can give the name of your project okay so let's suppose spring boot rest API project right so this is my project name then here I want to choose the type as a maven right because I want to create a maven project then packaging will be jar and Java version you can just choose according to your requirements So currently I will just keep 17 okay then here group let's give it as com Dot SJ programming dot rest API okay and I will just copy this and put here in the package also okay then here the artifact let's keep that as it is okay then we will just click on the next here you need to choose which dependencies you require for your project as we discussed we need here spring web dependency this one then we need the MySQL okay so let me just write here this one not this one the SQL this one MySQL driver and then spring data JP and one more is there which is Dev tool okay so let's just search for spring boot Dev tools so see here as we discussed we need four dependencies very first one is Dev tool then data jpa MySQL driver and spring web once you select the required dependencies then just click on the next and then finish okay so it will just create a spring starter project for you see here it is just importing all the dependencies whatever we have chosen okay so now let this complete the importing so now our project is ready let's just go and see see here very first Source main Java folder is created inside the source main Java you will see that this is our main package and inside the package I have spring boot rest API project application so this is my main file already this file is created okay so let's just minimize the font okay this way okay so see here this is our main file inside this file I have the main method okay so very first or the entry point for our springboard project will be this file only which is having at the rate spring boot application annotation and main method okay now one more uh folder got created here see Source main resources inside this folder we have one file application dot properties and here we will mention all the properties whatever we are going to use in our project after that the important file file is this one the palm.xml you will see that all the dependencies whatever we have selected will be present here see very first one is spring boot starter data jpa then spring boot starter web right so we had selected data jpa spring web then Dev tools okay and after that this one the MySQL connector okay so these are the four dependencies we have selected and one more is the default which is a test okay so this is automatically created we have not added it so let's just keep that now here let me just close this file now we need to create our endpoints right so before that let me just go to my MySQL and here I need to create one database okay so let me just right click here and create a schema I want to create let's suppose student database right let's just click on the apply apply and close so now if we just close this yes so see here I have a student database right now inside the student database we have tables So currently I don't have any table so we will just create the table through our application itself okay so now let's just go to the STS here very first we need to create the entity so let me just create here one package right click click on the new then class okay and here you need to First change the package so let's suppose rest API dot NTT okay so I will just keep the package name as a entity then here let's give the entity name as a student right because we are creating a student management application now click on the Finish okay now here in the student class what are the fields I want very first one is let's suppose private int roll number of the student okay then private string name of the student after that private maybe float percentage of the student okay and one more field let's add private string spatialization or maybe the branch right so I have these many fields for my student entity okay now what I want I want to store all of this data of the student roll number name percentage and Branch all of the data should be stored in one table okay so I have one here the database okay which is the student database inside the student database I want to create one table let's suppose the table name will be student and in that table we want to store all the data okay so very first let's just go to here and we need to write some more annotations here see here very first we will write at the rate entity because this is the entity right so let me just write at the rate entity see here this entity annotation is coming from Jakarta persistence okay we have the this dependency spring data JPM that's the reason we are getting this at the rate entity annotation okay now one more annotation is there which is at the rate table okay so here what I am saying that this is my entity class and whenever I run this application I want spring automatically create the table for me so let's just give here the table name okay so maybe table name we can give as a student okay even if I don't write here the name then what will happen whatever is your class name that class name will consider as a table name only thing will be this s will become the capital okay so even if I will not provide this thing then also the table will get created with this name okay so just for your information I have written here name and student you can give any name here okay after that what I want I want this roll number as a column right then this name as a column percentage and Branch okay so for that we use at the rate column but this roll number should be unique right so this roll number will be a primary key for my student table so for that we will use here at the rate ID annotation and then I don't want to generate this I don't want to create or set this roll number automatically this roll number should get created from the SQL so I will write here at the rate generated value so here I have generated value okay dot no actually generated value and here you can write a strategy and I will just select the static strategy as an identity okay so ID is to make this roll number as a primary key and then then here I have returned generated value identity I don't want to create or I didn't I don't want to set this roll number okay so whenever we will insert the entry for the student automatically the roll number will get created okay after that let's just make this as a column column okay and same here also we can make it as a column for the branch also okay so if you want to give any different name then you can just write it here also the name and you can write here maybe student name okay and if you don't want to provide the explicitly the name and if you want like the whatever will be your field name as your column name then you don't have to write here automatically it will just create the column with name percentage okay but if you want to explicitly provide then you can write here name and then maybe student percentage okay and here also I will just write name is equal to student branch okay now let's create the getter and Setter for all of this Fields okay so let me just create right click here go to the source and here you have option generate getter and Setter okay now select all and click on the generate so here all the getter Setter methods will get generated one more thing I want to do just click on the source and go to here you have generated two string right so we need this two two string method also so let's just generate it from the ID itself and after that what we can do we can just create here one default Constructor okay so let me just write the student this will be my default Constructor and I want to create a parameterized Constructor also so just right click go to source go here you have one field generate source code sorry generate Constructor using Fields okay and here uh most probably we don't need the roll number because roll number will create automatically so let's just keep name percentage and branch and click on the generate okay so this way our entity is ready right now the next task is to connect our application to the mySQL database and for that what we need to do we have to go to this resources folder and here I have one file application dot properties so inside this application dot properties file I will just mention this one the properties okay so what will be the properties we want to connect our application to the mySQL database right so we need to provide the URL then username password right so these many things we need to provide so let's just add here very first one is data source dot URL right so here you will just get a two suggestions also so see here I want spring dot data source dot URL so let me just select this one after that I need data source uh data source this one spring dot data source dot name right so URL then my username and after that data source dot password this one spring.data source dot password right let me just write here the URL so I am going to connect to the MySQL so let me just write here jdbc colon MySQL colon slash slash then localhost and colon 3306 will be the port for our MySQL and then you have to give here your schema name so our schema name is student database student database right you just need to confirm the schema name from here see here I have created student database so same schema name we will give in our property also right so jdbc colon MySQL colon slash slash localhost this is our port and this is our schema name after that you have to give here the username okay so for me it is root so I am writing here root you can change according to your username after that we need to provide the password so I have the password root underscore pass underscore one two three four you need to change this username and password according to your username and password right so these are the basic properties which are required to connect your application to the mySQL database now what I want see I have written here if we go to the student entity I have written here at the rate table at the rate ID at the rate column why I have written all of this thing so that automatically my table with the name student get created in my student database student database schema right so this schema we have provided so inside this schema automatically one table should get created with the name student so for that you need to provide one more property here and what is the property name you just write here hibernate Dot this one the ddl auto just click and here you have to provide create or update whenever you are writing here create it means it will create the like it will first check if you already have the student table then it will drop that table and create the new table okay but instead of create if you are writing here update then what will happen if the already table with the name student is created or present in your database then it will just alter the existing table itself it will it will not drop the existing table okay so let me just keep this as a create first now here I have returned ddl Auto create which means automatically the table will get created right so behind the scene the query will get created to create new table right so that query I want to see what query is generated behind the scene I want to see that query and for that you just write here jpa dot show SQL and you just write it true so that whatever are the queries generated behind the scene by the jpa or hibernate so those queries we can see in our console itself right so now if we save this okay go to our spring boot rest API project application file this is our main file so here you just right click run as a spring boot project so now one table which uh one table should get created with the name student right so let's just see the console let me just increase it see here what happened hibernate drop table if exist which table Student First it will check do we have the student table if it is already there then drop that table and create a new table see here one query is a generated create a table student with this parameters roll number right then we have the percentage then Branch so the table got created how we can check it let's just go here in my MySQL workbench let me just refresh the student database see currently we don't have any table let me just refresh it and you will see that in the table section one table got created right see what are the columns we have roll number student percentage student branch and student what is this name okay so these are the columns we have created this table is created automatically and why this is created because we have written here in the application.properties ddl ATO as a create right and then in the student.java file which is our entity I have written at the rate NTT at the rate table at the rate ID then at the red column right so the table got created automatically so for now what are the things we are done with very first one creation of project then we have created The Entity and after that we have connected our spring boot application to the mySQL database and table is created with the name student right now I want to create this rest points see here we have this race point this one right so I want to create this rest points so very first one let's just create for the uh maybe very first we will just create for get student or get all students right so let me just go here okay and let me create the controller first so right click here click on new class let me just change the package name dot controller right so in this controller I will create uh this one the student controller right so this is my student controller okay uh for now let me just stop this otherwise it will just reload as soon as I just do command save automatically the project will get reloaded right because we have added Dev tools dependency okay so let me just close this one console okay and here this is our student controller right now let me just add uh there is a slight mistake here let me just rename this controller okay right click then refactor rename okay so let me just make it as controller okay now uh to make this as a controller we need to add here at the rate uh rest controller annotation right so now this is our controller and now inside this controller I want to create my rest apis right so very first one will be to get all the students right I want to get all the students which are there in my student table right so for that right here one method let's suppose public and we want all the students right so here I I want to return the list of the students so return time return type will be list of students right and then here let me write the method name get all students right and here we need to return the list of the student right so for now just return the null so this is my method right now this is our get request we want to get the student details right so for that here you need to write at the rate a request mapping you can write or maybe directly get mapping annotation get mapping right and here you need to provide the mapping right or the URL so for get all students I want to write the URL as a student because we want to get all the students right so in our Chrome or maybe in our Postman if I hit localhost colon 8080 slash students then I should get all the students which are there in my student database or student table right so that's the reason I have written here at the rate get mapping and this is my URI okay now see I want to use here spring data jpa so that we can get all the students from the database right so for that very first let's just go here uh let me just open this one and let's create here one repository first right click new interface and let me give here a name as a repository or maybe student uh let's just give Repository and the name of our interface will be student Repository right and now what we will do we will just extend it extends to uh the jpa repository extends to jpa Repository this one and here if you see I need to provide the class name so our class name is student so student dot class and then even don't need to provide the dot class just a student will be sufficient and then the primary key type is integer so let me just write here integer right so now what will happen in this jpa repository already we have the methods if we just go here see we have some methods okay let's suppose delete all batch or maybe let's just come here see here find all method then this is find all method again if you just see here this is jpa repository is extending to the list crud repository if you just go to the list crud repository you will see that this is again extending to the crud repository and inside this crud repository see here all the crude operation methods are already there save then save all if we come here we have find by ID Exist by ID find all right so all of these methods are already there and we will be using all of these methods for our current operations right so now let's just go to the student repository save this go to our controller and here I want to use that repository so how I can use that here I need to write student a repository and let me write here a repo okay and I want to add to wire this so at the rate at a wired okay now here you can use repo Dot find all students see here find all and it will return you a list of students right and we also want to return the list of student itself so this method will be perfect to fetch all the students from our student table right so repo dot find all then just assign the variable so assign a statement to new local variable see here list of students so let me write here it will return the students right and I want to return this list of students here okay now see what will happen I just created one end point which is of type get see here this is my URI so whenever I am writing localhost 8080 students then what will happen we will come here and by using our repository we are finding all the student details we are fetching all the student details and that students we are returning here okay now if I save okay let me just go to our database first if you see here currently in the database we don't have any data let me just create it so let's let's just um see let's suppose the roll number is 1 and student percentage let me keep as 98 what is the student Branch let me keep as it and student name let's suppose ABC and now let me create one more student the roll number will be 2 percentage will be 78 and the branch will be see you all at suppose and the student's name will be x y z so now in my database inside the student database I have a student table and in this table I have two records with the roll number one and two let me just apply apply and close now if you just hit this see here I have two records so these two records created manually by me right so just to test our end point this end point get to students right so before that what we need to do we just have to come here and make this create as a update so that our existing table should not be dropped the existing table only we will use okay so let me save and run the project so this project I need to run right click here run as a spring boot app let the project execute see here see now your table is not dropped it just altered the student table okay now what I want I want to go to this Postman and I want to hit this rest point this one if I just come here see this this rest point I want to hit and if I hit this rest point I should get all the student details okay so let's just go here uh to the postman and then let me just write here localhost colon 8080 because currently my project is running on the 8080 Port then slash students and see here we need to select the type we have different types right get post put So currently we have implemented the get request so let me just select here get this is my end point and let me just hit here send see now in the output you are able to see right see here in the output what are the things we got see okay let's just see here you got the response inside the response if you see first you got the student details of the Roll number one and then the student details for the roll number two right so we have two records in our student table and we have received the details of those two students with the status as a 200 okay right it means our students rest API successfully executed and we have received the required result right now if I just go here see the first end point was localhost 8080 students and if I hit this endpoint I should get all the student details and if we see in the postman we are able to get all the student details right so our first rest point is done which is get request get all the student details right so now let me just go here and let me just create the another rest endpoint which is to get the student based on ID for example if I pass the student ID 1 or student rule number one then I should get the details for that student only right so let me just write here the method for that at the rate this will be also a gate mapping so get mapping and URI let me just put here student or students and then we want to pass the ID also right so that ID we will pass as a path variable so let me write here at the uh in the curly brackets ID the method we will write public this method will return me a particular student because I am passing here the ID right now get to student this will be my method and this will return a particular student whose ID is this one Whatever we are passing right so very first let me write here the path variable at the rate path variable int ID right so this is my ID and this ID I will get in the URI itself so now how my URI will look like localhost students and then here I will pass my ID so this ID will be the path variable right now let me just return here the student for now let me just make it as a null okay now let's use our repository inside the repository you have the method find by ID see here find by ID and what this method is returning this is returning the optional of student so let me just use this pass the ID here okay now this method is returning optional but I want the student right so for that let me just use here get okay now if you just assign new variable assign statement to new local variable see here I am getting a particular student whose ID is one let's suppose I am sending here one right now this student I want to return here right now if we save this execute our program let me just restart the server see here our application is up now let's go to postman very first hit this service see here we have two students right now I want to get a data of the student whose role number is one now if I send this request see here now I am getting a particular student let's suppose I am passing here the ID as a 2 then I should get the student data whose roll number is 2 right so see here we are getting it based on the ID which we have passed in the URI right so this way we are done with two requests very first one get all students second one get student based on Roll number right now I want to create a new student right so for that let's go to our STS okay let me just stop the server because otherwise it will just reload again and again right so let me just stop it it's come here and now I want to create a new student right so for that let me create the method public void create student right now if we just see let me just change it to public okay now here what I want I want to create a new student right so to create the student see here in the postman you have to use this post request then you have to provide the URL for example here I want to write the URI as localhost 8080 student and then I want to just add here this add so this will be my URI right now in the body we need to pass the data right let me just select here raw so in this body we need to pass this student data for example let me just copy and paste here roll number we don't have to create because roll number automatically will get created name let's just pass as a maybe the name will be Max percentage let me make it as 77 and Branch Maybe entc okay so now see uh let me make it as Json see now we are passing the data here name percentage and Branch now what is the expectation whenever you are hitting this URL then automatically new student will get created and store in our student table right so for that let's just go to the STS here and we need to pass here the student in the request body so for that let me use here The annotation at the rate request body and in the request body what we are passing we are passing a student right now let me use here at the rate post mapping to create a new student you need to use a post mapping and URI will be student and then uh let me just add here okay so I want to use a student to add okay so whenever I will hit this URI the student details whatever I have passed here in my request body that student details should get saved in our database right so for that let me just write here report Dot save we have the save method this method will save this student in your table okay so let me just pass here the student okay now if I save this and let me just restart the server now if we just restart the server okay and for this end point if I pass the student details then automatically one student will get created let me just go to the postman first and let me use the new this one the get all student let's just use this one right so if I hit now currently how many students I have in the table two students now if we hit this post URL with localhost 8080 student ad and if I pass this details then automatically one more student will get created let me just hit on this see here 200 okay which means your request is processed successfully without any errors let me go to our get mapping see here get request where we will get all the student data if I hit this see one more student got created whose roll number is three the name is Max percentage 77 and branch is e and TC this is the newly created student which I have passed here in the request body the same data will be there in your MySQL also let me just hit this query and you will see that in the database also one more student entry is inserted with the roll number three and the name of the student is Max right so this way you can create a new student also let me create one more student in the postman you just have to write the URI select here a post and now let me just pass some more details let's suppose I want to add Ramina and percentage for Ravina will be 65 and Brands let me keep as a ID hit see here now here you got 200 okay which means your request is successfully processed now if you want to see the new entry let's just go to the database hit this query see here one more entry is created for the Ravina right also you can see in our get request also if I hit this see here now we have four students right the Max and Ravina is Created from the rest endpoints right so this way we can create a new student also okay now what we need to do this is our post mapping right but actually if you see in the postman what we are doing here we are creating new entry okay for this post we are creating new student but if you see here I am able to see 200 okay actually it should be 2 0 1 which means created right so for that we can just go here and what I can do uh in this post mapping itself I can write here at the rate response status and in this response status let me use the code AS created this one created now what will happen if I save this then whenever I create a new record or whenever I hit this post request then here the status will be 2 0 1 instead of 200 200 means Okay 201 means created right so now let me just to save this automatically our changes will get reloaded here let me go to the postman and change the data here let's suppose Puja percentage nine zero Branch Co send C here 201 created right so this way you can change the status also right now A1 see here one more entry should be there for Puja right so this way we can create new student also now what we need to do we need to update the details let's suppose in the postman if you see I have the student let's suppose whose ID is 2 right and now if you see the percentage of the student is 78 I want to update the percentage from 78 to 92 and name of this name of the student we will just update or we will just add here x y z and then something else right so maybe currently the name is x y z I will just update the name of the student so for that you need to use put request and here you need to give the URI okay so let me just create that rest point in the SDS itself so let's just write here at the rate post request uh post not post actually put request mapping right whenever you want to update the detail you will be using put mapping whenever you want to create new student then you will use post mapping okay so put mapping is for the update operation let me just give here URI student update okay and now the method will be let's suppose the method name will be public void update students okay or let me just return the updated student details okay so I will return here student okay and then what we can use here first C to update the details we need to pass the ID also right so let me just pass the ID so that whatever will be the ID for that particular ID we can update the details right so very first we need to fetch the details of the student whose ID is this one if user is passing the ID 1 then here we will first need to fetch the student whose id id means roll number okay is the let's suppose it is passing one then we will fetch the details for the student whose roll number is one how we can do that very simple just use here repo Dot find by ID okay not find by find by ID and you need to pass here the ID which is passed by user and for that you need to use at the rate path variable ID [Music] right now here we will get the student let me assign here uh variable okay it is returning optionals so you can just write here get right and then we will get the student okay now we need to update these student details right so let me just write here a student dot set we want to set the name previously it was XYZ now let's make it as uh maybe Poonam okay and then student dot percentage also I want to change so use here set method set percentage and maybe 92. okay and then I want to return the student okay but now see before returning the student whatever are the details we have updated we need to save the details also right so for that you can write report dot save and you can pass the student right now what will happen here you will get the student whose ID is one after that you will update the name of the student from XYZ to Poonam then you will update the percentage also and then you are saving the details the updated details and you are returning here right so now let me just save this student update ID this will be the URI let's just go to the postman and here let me use the put student uh then update and let me just pass the roll number as a 2 right now what will happen if I just send see here for the roll number 2 the name is updated to Poonam and percentage is updated to 92. branch is co the same details you will see that in the database also it should get updated see here for the roll number two currently it is 78 Co x y z right so let me just hit the query again you will see that instead of x y z it should get updated with the Poonam and then for the percentage also it should get updated to the 92 let me just hit the query and see here Poonam foreign with the percentage 92 which means the data is updated in the table also right so this way we can create the post sorry the put mapping right where we can update the data of a particular student now if you see we are done with get all student get student based on Roll number create the student update the student now let's suppose if you want to delete a particular student then you have to pass the student ID which you want to delete right so for that let me go to our controller and let me just create the request for uh Delete okay so let me use here delete mapping at the rate delete mapping you need to pass the URI so URI will be student delete and which record you want to delete so here we will pass the ID also based on the ID the record will get deleted okay so now let me add public void uh sorry public void a remove student the most student whose ID is this one so at the rate path variable into ID this is our method right now we want to delete the particular student for that use here report Dot delete the student right so here I have one method which will be required required the student as a input okay so very first let me use here repo dot find by ID method and I will pass here the ID right so that I can get the student whose ID is this one Whatever will be passed by the user let me assign a statement to this variable okay and then the student whose ID is one that student we need to delete so that repo dot delete method we will be using and which student you want to delete that student we will pass here as a parameter right now see first we need to fetch the details of the student which you want to delete and once you got that details then you will pass that student object to this delete method then these two this particular student will get deleted from your table okay so let me just save reload the changes go here in the postman okay now if we just hit this service which is to get all the students see here currently we have how many records one two two three four five So currently we have five records right now I want to delete a student whose ID is one okay so for that let me create one more window here so let's just use localhost 8080 student delete and which student I want to delete the student whose role number is one and here you need to select the delete mapping right now just hit see here 200 okay which means your student is deleted from the table now let's just go here hit the service see here you will not get the student whose roll number was one that record is deleted from your table see currently you have only four records even if you go to the database and hit this query see here now the record whose roll number is 1 is deleted from your table also right so this way you can create the delete rest API also right so now see we have created the student right so we have just inserted the student by using post mapping to update the student we have used put mapping to delete the student to delete the mapping and to get the particular student or to get the all of the student we have used at the rate get mapping right now if I just go to our initial slide see here the requirement was to create a spr spring wood project okay which will perform the crud operations right so now we have created the project and we have created all of this rest points get all student get the student based on Roll number create the student update the student and delete the student right I hope each and everything is clear to you and now you are you will able to create your spring boot application which will perform or which will create the rest apis to perform the crude operations right if you have any queries let me know in the comment section or you can connect with me on the given mail address also because maybe when you practice it you may get different errors right so if you are stuck somewhere and you need the help you can connect with me through the mail address or you can comment also in the video itself okay so that I can just go and see the comments and help you okay so that's it for today's video okay so in this video we have covered all the crud operations and we are we have learned how to create the rest endpoints to perform the crud operations okay so this is very basic project now we need to improve this project and that we will be doing in our next video okay so that's it for today if you like the video please share with your friends and don't forget to subscribe the channel thank you
Info
Channel: SJ Programming Solutions
Views: 4,959
Rating: undefined out of 5
Keywords: springbootprojects, springbootrestaicrudoperations, springdatajpa, springboottutorials, studentmanagementproject, postman, spring, howtoconnectspringbootprojecttodb, springtutorials, how create springboot project, how to connect springboot application to mysl database, springboot tutorials for beginner, how to test rest end pointa in postman client, student management application in springboot, what is spring data jpa, what is responsestatus annotation
Id: 9fZyVWkBDgk
Channel Id: undefined
Length: 59min 51sec (3591 seconds)
Published: Sun Sep 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.