spring boot CRUD REST API with MYSQL for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the Lambda code YouTube channel I'm Roshan Vikram Raj I'm a software engineer with many years of experience today I am going to teach about simple print boot application this tutorial actually for the beginners today I am going to implement the thread operation using spring boot it's a representation of the application today I am going to implement I implement the without client side I have only implement the API this is the API layer I implement the get request to implement the API for the Post request and post request and delete request and this is the service layer I Implement all the business logic in this layer and this is the data access layer all the logic related to database I Implement display in this layer and this is the database in this tutorial I use mySQL database in this tutorial I use Postman for the API testing so I Implement several apis first one is get use API so I have to enter the user data and I can get response this kind of n other apis get user list API we can get list of data and other apis we can get user data using user ID and other API is we can update user data in this API we use only status code I Implement another API for the update one attribute in user model in this API update only name the API is used to delete specific user using user ID so we have to generate this memory project so in this tutorial I use spring initializer in date print board project go to the your favorite browser and search spring any share price so then go to the this link this spring initializer we can generate the frame boot project in this tutorial I use Maven because I mostly prefer to working with Maven and I use remote 3.0.2 is the latest stable version of frame Boot and you can change group ID and then we have to give name of the project and in this project I use Java 17 in this recording time this is the latest stable version of java then we have to add dependencies with the button and we must add web dependency and you can see the list of dependency so we have to select the spring web then the dependency will be added to the test and other one is loanbook and other one is my skill because we use mySQL database in this tutorial and we have to add ping data jpa dependent so we can simply search APA so then we can add spring data JP dependency so then we can easily regenerate project using pressing the same rate button so then spring initializer enter the project and will be downloaded so we can go to the project we can is trapped so we can put the necessary location this created file I put the project in this location so then we can open the this project this is a project I have downloaded so in this tutorial I use IntelliJ IDEA because IntelliJ IDEA is a very powerful and most developers use this IntelliJ IDEA I also recommend you to use the IntelliJ IDEA but you can use any other preferred idea like Clips netbean PS code so then we have to open the previously download so then navigate to the project so we we can identify the this is the project we have previously downloaded so then we have to double click home.xml file so then we'll open the project so then it will take few minutes depending on your internet connection and performance of the your PC so then we'll download necessary dependence and Java to the project so then finish we can run the project using this triangle but so then the project will start if I try to start the project so then will be some mirror so the error say URL attribute is not acceptable and no embed database could not be configured because a is still not configure the database configuration because previously we added the MySQL driver as a dependence but we didn't configure the database configuration so then we have to configure the properties to the application so we have to navigate those and Main and resource then so we have to identify this application dot properties file so I mostly preferred working with Yamaha so I have to rename this file as so then we can add necessary properties in this ml file I will let the custom Port so usually the spring boot project run in Port 8080s actually I want to change the port as 80 81. so I add this configuration because in future we want to see their messages so we have to add this configuration also then I have to add configuration of the database we have to add username actually we have to add the MySQL data configuration so we have to add username I actually I install mySQL database and MySQL workbench so the when we set up the mySQL database we have to set username password I have to enter the username password in here my name is root and URL so actually my mySQL database running on 3306 Port actually this is the default for pi SQL database running so then we have to add the schema name the scheme on name actually I use the free money Master user okay this is the user DBS the schema name and then we have to add password this is a mySQL database pass is my password so we have to add extra properties also so then we add ddl auto update so then when we change the some dto and entity then automatically tables will be updated then I have added to SQL through so then all the queries will be plugs in in the terminal then I add a show SQL through so then we can see the all the look in related to SQL so we add all the necessary properties in this project I will show to how to create a schema in mySQL database really we have to create the schema name user underscore DB so in this tutorial I use MySQL web page for the current the mySQL database so open the MySQL workbench and click the local instance so then we have to add schema so that so you can hear the schemas so then right click in the face and so there is a create schema so click the create schema so then we have to add schema name so the in properties file in our springboard project actually we use schema name as a user DB so the schema name will be user Maybe so then we have to apply button then press the apply so then we'll create the schema so then we have to refresh and we can see a lot of schemas I have previously created and you can see the user DB scheme previously when we run the project there is a error so then we have add some properties and we create the schema so we can run the project so you can see there is no errors and project actually will be running on Port 80 81 so the so we we previously added the sport so now we can implement the web application so we can start the implement the project so navigate this location and right click this location right click the for the path and create the okay so we have to create the several package then first package is controller next one is DTU next one is entity next one is Repository foreign service so then we have to add entities in this tutorial simply I use user data like name age address we have to create the use entity first go to the class and give a name of the entity I will give a name as a user then this class will be generated so we have to add data other one is entity other one is structure the one is for lock construct so then we can add attribute of the user we have to give the ID of the user private integer integer ID so that we have to generate the ID in automatically so the spring APA can generate the ID automatically so the ID will be unique key of the table so we have to add annotation ID and we have to add generate value so the other one is name we had necessary attributes for the entity so then we have to add Repository in the package of repository I click and go to the new and click the Java class and click the interface and give the name I will give the name as a user sorry o c user repository so then I press the enter so then we have to extend JP Repository extends APA reposited so then we have to add here our entity name our entity name is user and we have to add integer so the this integer is in user repository we previously had ID so the ID is the actual type is integer so then we have to add integer also here so then we can add service interface create a service package and go to the new and click Java class and use the interface name user this service click interface so then we'll create the interface and in the service package we have to create that in the package called impl package by npl in the impl package we have to add user impd then you have to implement user service the user Services we previously implement the user service interface so the we have to implement new service here so then we have to add service annotation then then go to the implement the controllers so right click the controller package and create new class I will give a name as a user use control so you can see there is a spelling mistake so the intelligent idea will indicate the spelling mistake spelling mistake double click and right click and refactor and go to the real name so we can rename after write the correct name we can press enter button so then it will automatically update in class name here and here so we have to add annotation like rest controller and request Maybe so the IntelliJ IDEA actually generate the required annotation when we typing so we have to select the request mapping annotation so i weekly path name as a user so you can add a frequent name in here so then we have to Auto wired user service here your service new service and so then I am going to implement the first rest API and first one is the git API by previously explained that the first API is we have to put the user data to the database so otherwise we have to store the user data in the database so we implement the first API for the add user to the database so I used annotation host mapping so I gave a path readable as a flash that you can use any Freebird name so in here I use the return type as a string so in the implementation further you can use prepared return type as your requirement so I give your name as ADD user so I use the request body annotation because I pass the all data in the body and you call the endpoint so we use user model so we use with a service in new service we have to implement necessary method so I implement the method name as ADD user so you can use any private name so I have to pass the all the data coming from the client to the method so we have to pass the user actually we didn't Implement any return type if API is Success so we have to return something so in this API I returned some simple message so you can see the intelligent idea will give the compile time mirror you can put the cursor in the add user name so then the Integrity idea will indicate some error message it will say cannot resolve the method at in user service so we can click the this link so the link is say create method add user into the service when we click the this URL so then automatically the IntelliJ IDEA will create the interface the necessary interface in the interface so then also there is some compiled time mirror in here so one related problem so we can click the here so then we will navigate the service class so this we can put the mouse here so then indicate some error there is a link the link say Implement method so then pop out these windows and we can click OK button so then you can see the IntelliJ IDEA automatically generate boilerplate code if you use any other kind of ideas so there is some features also that ideas also but you if there is no features like this you have to implement the boilerplate code in manually but in here I will show the the easiest method to generate these developed code it will be saved your time so then we have to add user repository in to here foreign so then we can save the user data in database so here you can use a repository then enter the code so you can search save so the IntelliJ IDEA automatically generates something so there is a list so you can see the save save o save o land plus lot of things so in this API I used to save so you can save as entity and double click so we have to put the data in here so by another user so we can test the API using Postman so before we have to run the application so I run the application so there is a no error so I open the postman so then at the new collection call oh in root Grant so I create this springboard Crut collection and I add the add request I give a name as a at user so this is actually the use you can see in the controller layer we use post Mappy so in here so in Postman we used to post and into the past here foreign so then we have to enter the path user and it so you can see in the controller layer we use user and at so we have to add user after that add and so then we have to pass the required data using body so because we used request body annotation this is the entity so we have to add the all these attribute in the body name page and address so in the postman go to body and click the row and click the Json then we have to and name I will give a name as a question and h ank I will give a 28 because age is not spring descent integer so you can see the edge is integer and I will give a address goal so I I will I added the necessary field so I can test the API so you can see the success at user so you can go to the MySQL workbench and refresh so then go to the user BB schema and you can see the table so so you can see the table name as user so in here there is a icon so you can click the icon then you can see the all the data in the table id address is Name ID will be generated in automatically the first API is successfully implemented so then I can move to the next API so then I am going to implement the get the list of users so we have to implement the get request also I use get moving annotation for that public so in here I return the list of users so I have to return the type as a list get I give a name as a guest users and return to service good it uses so then we can generate boilerplate code using intelligent idea put the mouse on The get user then click the this URL then enter the ID will automatically create the interface for the application then click here go to service layer and Implement method so then delete your idea automatically generate the four wheeler plate code so we have to find all the users in database so we use user repository and in GPA there is a method method name is find all so you can see finder so the implementation part is done so we can test the API using Postmates so before the test the postman we have to restart the project so there is no error so then go to the postman and we have to add another request get user list and this also get method and we have to give the path in here I copy the path it will save my time we're going to copy the path and paste here and you can see the controller but this user and there is no specific field in here so actually we can delete it don't and so we can test the API so you can see the one record because previously I entered the one record so go to the add user API I add another record so then go to the get user list API so then you can see the list of users our next step is get user using user ID previously we get user as a user list so then this API I implement the API get specific user using user ID mapping regular path test get and public user it it user and here I I will give a ID as a path parameter so I have to use request for a rotation request Para representation the type of IDs integer variable name as ID and return the service dot git user I have to pass the ID and I will implement the code using intelligent idea click here so then delete ID automatically at the interface so then click in here so navigate to the service layer so then automatically generate the oil plate code so in here we have to implement the code user user user Repository there is a method named APA so I have to pass the ID to ID so then when you find the user in the user database client another ID so then we have to throw the exception if the relevant user cannot find in the database so then we have to implement OLS so you can see the OLS through new you can see there are a lot of exceptions so in here I use response status exception HTTP status code I give not found and I will give custom mirror message so then I can return user so I will explain once again the user device client and the ID then try to find using ID if cannot find the user using ID then we'll throw the exception the implementation part is done so we can test the API using post we have to start our project then go to the postman then I have to add request I will give name as a get user by ID so I have to put the path here so I simply copy the path and put here so you can see the controller path is user and then get user and then okay so then we have to pass the ID so you can hear that we we get the ID as a request pair up so the parameter name is ID during the postman go to the parameter section and here type the ID and we have to pass the necessary ID so give the ID as a one so then we then press the send button so then you can see user data related to the ID we can get so you can see so the the API is working correctly the next step is implement the update API so we have to use put mapping annotation and it has a path Flash update and uid this ID is it has a path variable public in here we don't response any kind of data it will be response in here only status code so we have to implement response is entity for it we have written a theme update user in here we can get the IDS path variable part variable integer the ID type PC integer and we have to pass the user data in the body of the API so we have to use a request body annotation we use use entity it has a variable as a user so then user service Dot update user so then we have to pass ID and user so after successfully updated we have to return status code return exposes response entity Dot no content actually we returned nothing the content is nothing no content the build using intelligent idea I am going to generate somebody paid code here generate this site and click here click here then click ok so then automatically generate this kind of where the plate code so first you have to check whether the user is in database or not use a repository Dot you can see the list of methods I use find by ID I have to pass the ID then if the user cannot find using ID so we have to through exception so in here lot of method I used to oils through actually previously also used this method then new response is status exception and STD status note found not found and add custom error message so I can explain once again the user user ID with user data first try to find the use data using ID find the user related user if they cannot find the user then throw the exception if successfully find the user so we have to set the user Dot it it ID that ID in first ID I will explain later shall we pass the user data and actually we don't pass the ID so we have to set the ID plane we have to save the user in database user Repository dot see you use implementation is completed so we can test API using Postman before we have to run the project so there is no error so then go to the boss man and I will let the another request this is a update user copy the path in here this is an actually it is a put so you can see the controller the party's user update and then ID user update then actually IDs then we have to pass the value click the row and Json so in the API so can you can see we passed the user model this is also used in at user so I simply copy the value and paste here so I update the position as a name and it is 25 so you can see the ID is one Roshan 28 cool Sri Lanka update user then press the send button so there is a nothing response but status code is 204 no contain so the API is working correctly so if try to put the ID note in the database so you can see the in ID is 1 and 2 only so I put here ID as a 10. so so then you can see there is a exception it's a four zero four node pound invalid juice ID is 10 for the reproject and so you can see if ID is not found in here then we'll draw this exception so next step is empty on the delete operation so we have to use station delete mapping we have to set path as a delete it has IDs path variable and public In Here Also I return only status code so I have to use response entity return nothing for it delete user we have to use annotation path variable type this integer variable name is ID so then user service note delete user I have to pass ID it is successfully deleted so we have to edit the status quote return response entity Dot dot content there is no content in the response and build so using IntelliJ IDEA I use generate boiler paid code the code is generated so this code also generated in here also we have to check whether the user is in database so previously also I implement the this kind of code so in here save the time copy the code so then using use ID find the relevant user then we can delete the relevant user user repository they submit that call delete so you can see here delete to Scientific you can ask user so then we'll delete user the implementation is done we have to run the project so there's a no error then go to the postman I give name as a delete user the method is delete path is same so if I copy the path go to the project the controller you can see the path user delete and then ID okay idea set to because we have id1 and id2 I try to delete this data press the send button so you can see 204 no content till it is successfully oh go to the guest user list API so you can see there is one record is deleted and also you can search in MySQL workbench this is a user schema in the table use a table so you can see one record bin database previously there is a two records but I deleted one record so there is a one record main so in here I will implement the extra API for the update actually previously you can see in the update user we have to pass the all the data all the record related to user for the update I will implement the actual I won't update only name so I will implement the API for the update only name so let's go to the repo and there is a annotation called touch passion Maybe so previously we use boot mapping now actually I want to update one attribute so I use a flash mapping so in mostly fetch mapping is tools for the partially updated purposes it has a path update name and I pass IDs path variable update name path variable integer ID so in here cache mapping we have to pass the ID and we have to updated name so in here so we have to add ETO for the that purposes so in detail package right click Java class give a name user dto you will let the notation data the data annotation comes from in lombok we previously been initiate the project we had the Chromebook annotation so actually we want to update only name so I put the only name so go to controller in here I have to annotate is a service name master update name so I have to pass ID and use a detail and I hit return response entity no content bit so I generate the boiler paid code using IntelliJ idea to generate this code line also generate it's called black also you have to check whether the user is in database or not actually the code is previously I written so I have to copy the code line I also previously I explained this code after that you can set the user Dot set name in here I actually I want to update only name so I set name using use a dto get name so then let's set the name to the user model and then save so implementation is done then from the project so you can see there is no error and then go to the postman at request your name update name is a method this patch I also I copy the URL you can see the path is user update name the 90 user update name then ID so in here you can see we pass the IDS path variable in here ID support variable then we have to pass the user DTU so when go to the user dto there is only one attribute is a name so in a postman go to the body go to the row Json your name Roshan I update as a name so you can get the user list using this API get user list API I previously implemented you can see the ID is one there is only one record in the database because I will delete some records the name is Roshan vikrama right so then I update the name as a Roshan vikrama press the send button so you can see two zero four no contact yes sir no here go to the choose a list API so you can see the Roshan Vikram the name is updated so and also bench press the button then you can see the name is the name is data so the API is working correctly I think you enjoy with this tutorial please Thumbs Up And subscribe to my channel and put the your idea in comment section also and we will meet again with next video bye
Info
Channel: lambdaCode
Views: 8,708
Rating: undefined out of 5
Keywords:
Id: 0oO2F2VCGdM
Channel Id: undefined
Length: 50min 34sec (3034 seconds)
Published: Mon Feb 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.