Spring Boot Restful + PostgreSQL + Spring JPA Hibernate - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to my channel and in this video we are going to see how to develop a simple springboard application and we will also see that how we can connect our application with database and we'll do some database operations to understand this video there are basically two basic prerequisites first one you must have the basic knowledge of java programming and the second one is the conceptual knowledge of object oriented programming because we are going to touch few of them but i'm not going to deep dive into these concept in this video so i'm going to use visual studio code you can use whatever id you prefer but if you are planning to code along with me then i would suggest you to use visual studio code to create java application or spring boot application using visual studio code i would recommend you to install at least these two extension packs first one is java extension pack this one is from microsoft and the second one is spring boot extension pack this one is from private okay these extension packs are basically provide some extension which enables you to like simplify your development process okay so because visual studio code is a lightweight ide and it doesn't come with like pre-installed bundles so we need to install and configure our visual studio environment according to our language that we are planning to use okay so this particular extension comes with spring initializer the same that we usually use to create a project here spring dot io okay here when we go to this section quick start section it like provides some tool to like create our project okay the same kind of thing we can do here with the support of this extension it enables us to create a project okay and this is for like debugging and it gives the all tooling that you can use to debug your project okay so let's create our project to create a springboot project in visual studio code you just have to press f1 or you can use this shortcut command control plus shift plus p okay so just press f1 and now you need to type spring init the moment you type spring in it it will come up with few options and from that we need to select this one create a maven project okay now we need to specify the version of springboot i'm going to use the latest stable version which is this one 2.4.2 okay now we need to specify the language java of course the group id for our project i'm going to say lsio and the artifact id is going to be springboot now we need to specify the packaging type this is required when you want to deploy your project on any like web server okay so i'm going to use tomcat and i prefer this one war okay now we need to specify the version of java i am going to use 1.8 you can use either 1.8 or any liter version we need to choose some dependencies for our project this project is going to be a spring boot restful application so for now i'm going to select spring web like we will need other extensions also to like make connectivity with database but that we will do later on okay so for now just this one press enter and i'm going to generate this project inside my f drive now i got the message click on open and this is our project okay say yes and we are good to go okay you can see in our palm.xml the initializer that we used to create our project it already like added this extension automatically and these two dependencies that you see here is this one is for test and this one is an embedded tomcat server to run our application okay so the beauty of spring boot initializer is the moment you generate your project it is always ready to run you can check this by navigating to this folder and this file and just go to run and say run without debugging wait for a few seconds and you will see that your server is running on port 8080 or whichever port is free on your system okay now we can check our application here and of course we are not going to get any output and we will get 404 instead because we haven't configured any route yet okay so let's do that first i'm going to create a folder inside spring boot okay and i'm going to name it controllers okay and inside this folder i'm going to create a file homecontroller.java okay for this tutorial i'm going to put all my endpoints in this class only and we need to tell springboard that we are going to use this class as a controller which is going to serve one or more endpoints okay to do that we need to annotate this class with rest controller because our application is a restful web application so the type of controller should be res controller okay now let's specify our first endpoint to do that just write a function let's say say hello and i'm going to return as simply hello say user okay now we need to annotate this function with some kind of http request okay like we have request get post or put or delete okay so i'm going to map it with get and for that we need to annotate this class with net mapping and open a parenthesis now inside this parenthesis we need to specify our endpoint so what endpoint this function is going to serve okay so whenever user type say hello in the url this function should trigger and should serve the request so let's check this run our project and our server is running now and let's say say hello okay so hello user so we are getting the message this is a really really simple implementation of a res controller actually we are doing nothing here just returning a plain string but in real world application we implement risk controllers in a such way that we receive the request from the user on any endpoint with maybe some data and delegate that request to microservices which may be running on the same server or maybe on external servers or we can delegate that request to business logic which is written within the application which actually handles the request processes the data and returns the response back to the rest controller and then way back to the user so this is the typical structure of a medium scale application and in our next chapter when we see that how we can connect our application with the database let's try to implement one of these to do that we need to do some configurations in our application so that we can access the database server the first step that we need to do is to add a dependency in a form.xml file i'm using postgresql version 13. so i need to add a driver dependency here which is suitable for my version of pg sql okay if you are using any different version you need to find out the correct version for your database server so now i'm going to edit manually so that we can learn how we can add dependencies manually in our palm.xml file let's go to maven repository google.com type maven postgresql.org and go to this link i found out that this one 42.2 is suitable for pg sql 13 so i'm going to use this one and add it here just save it now it is added in next step we need to add one more dependency in our application and for that go to form.xml file and here we need to add the dependency for spring data we can add it either manually or we can ask spring initializer to do that for us this time let's go to the spring initializer and add it from there just type add starters oh by the way press f1 to come to this window click on add starters and here type jpa this dependency is actually a bundle of java persistence api and the orm spring data and hypernate okay which enables our application to store and retrieve the data from the database right yes press enter proceed and dependency is added so now in next step the final step of configuration we need to go to this file application dot properties file which is under resources folder and here we need to write some configuration and rules that spring data will use to access the database and form the crud operations okay and first we need to specify the url to our database and the username and password for that just type datasource.url which is jdbc sorry post gray sql then localhost 5432 and the name of the database springboard now data source dot username postgres data source dot password now data source dot driver class name rc.postgresql driver now spring jpa.showsql say true we need to see whatever sql query is executing in a console and say test while idle so true and now validation query validation query just select one now we need to specify the naming strategy implicit naming strategy first it should be simply set naming [Music] touchy which one i oh this one legacy not this one this one right now the next one is physical naming strategy okay so here spring physical limit strategy this is it we are done with the configuration part now let's move to the next part which is creating a table in a database so let's move to our database and create a table i have already created this database springboard and inside this i'm going to create a table cities first column is going to be our primary key city id then it should be serial i want it to be generate automatically not null primary key then city name and here character varying should be under 30 characters not now save it now let's add some data and try to access this data from our application delhi new york this is it for now and let's move to our application and access this data from any of our endpoint to do that we need to create an entity bean for our city table with the same schema structure so that the spring data can make use of that bean to access and update the data in the city table right for that i'm going to create a folder inside springboard entities and inside this folder city.java like we did in our rest controller class like we annotated this class with at the rate res controller so that the spring will know that this particular class is actually a controller and going to hold some endpoints the same thing we need to do with our entity class as well but here we need to annotate this class with at the rate entity right now the next thing that we need to do is map the table with this class so we need to map the table with this particular city class and for that we need to annotate this class with at the date table and here we need to specify the name of the table which is cities right so whenever we access this class it is actually going to access our cities table now we need to specify the columns which is in our table like city id and city name the city id is auto generated one and it is a primary key okay and it is using this sequence so we need to specify this here in our entity class so how to do that the first thing we need to do is annotate this particular column with address id okay and which column we are going to target with this local variable okay so inside this we need to specify the name of the column since we have specified the name of the column here our local variable name could be anything okay and we can say like id so whenever we access this particular field id field it is going to refer to this one okay now this one is all good now we need to specify the generation type because whenever we insert the data this particular column should generate it automatically the value of this column okay so for that we need to specify the generation type for this column we are using pg sql so in pg sql we need to use the identity generation type so how to do that here we need to say generated value or inside this we need to use the strategy what strategy it should use to generate the next value in the sequence which is generation type dot identity right so this is done for our first field now our second field in our database is city name okay so let's say private string it is a character type character varying the data type of that field is so here we need to say string and city name and this field is going to refer to this column column name city name right okay now we need to generate the getter setter for these two variables and for that you need to go here source action generate character setters and select all okay right so this is it this is the schema for our table and we are going to use it to access and update the data but just hold on just writing this entity class doesn't mean that now we can access the data to do that we need to create few more files so let's do that inside springboot i'm going to create a folder repositories and inside this i'm going to create an interface class which is city repository.java now this is not a class this is going to be an interface and extends jpa repository okay and this is going to handle the city entity the primary key type is long right for now just leave it as it is and let's create a service class which has all the functions to access data from our city table we can do this directly from here also but like i said that we don't do all the heavy lifting in controller itself instead we just delegate the request to other business logic to perform that operation and to handle that request right so here just specify one more get mapping and this is going to be like get cities and public just going to return the city type object okay and the function name is get cities now written now we need to tell what to written here okay that we will do now so let's create one more folder again services here we we are going to put all our services here and city service dot java okay we need to annotate this class with service so that spring will know that this is actually a service class and it would always available for auto wire in the context okay and likewise we need to annotate this class as well at the rate repository the same thing the moment we annotate this class as repository it will available for auto wire in application context okay now city service here and home controller here like i said this auto wire or repository class auto wired city repository as city repository this one now create a constructor city service okay now let's create our function to get cities from the table public list of city list of city get cities and here we are going to return uh city repository dot fine all this is the default function that we have to access all data whatsoever in that particular table okay so this function is going to fetch all the data from the city table and return it as a list of city class right now here we just need to say enter it auto wired city service and from here we need to say city service dot get settings okay now let's access this one oops return and this should be list of cities because we are returning list of cities from there right so let's run our application okay our application is running let's go here and say get cities here so we have seen how we can access the data from the table right let's see how exactly things are going okay the user hits this endpoint first get cities and since we have auto wired this service and since we like annotated this class at the read service what spring does whenever we compile our application it creates an instance of the service class and make it available in the application context so that we can auto wire it right so this city service class is already instantiated by springboard and we are just using that instance to access this function of city service class so now what this function is exactly doing in this city service class we have did the same like we auto wired this time city repository from the context okay since we annotated this class with at the rate repository spring made it available for us okay now we don't need to initialize this so we can directly access whatever function this repository has by default the gpa repository has some inbuilt function and the find all function is one of them right what final function returns is it returns the list of city in city class the each instance of class holds a single record of our table right so by saying this find all it is going to simply written all the rows in our table in the form of list right so we are getting list of city by calling this function and we are returning this directly to our controller and from controller we are returning this to user like we have seen in this particular example here this one okay we are like sending requests to our business logic within application and we are getting the response and the same response we are forwarding to user right so this is how our program is working now now in the next section we will see that how we can insert a new data in our table so let's create one more endpoint which basically accepts the data in the form of the object of city class and we use that data to update our table our city table okay so let's create one more mapping post mapping this time because i'm going to submit some data endpoint is going to be add city public void save city okay let's say i'm going to return the same city object that user is supplying but this time with updated value okay we will see that how we can do that city and here we will use the same city service to save the data like safe city and we will pass the object which user has passed with the request okay this method is not there yet so let's create it this one and here now we can simply say city repository dot save okay and pass this object which user has passed right so this is also an inbuilt function okay in gpa repository what it does it just accepts the object of the bean and save the record in our database save or update it does both okay we don't need to like write different function for save and different function for update if the id field is greater than zero then it will perform update otherwise it will create a new row in our table okay let's say it is going to return the city and just return it and by default this function returns the object of the bean class and but this time with the updated value okay so this city this function is going to return the city object to the controller and the controller is going to return the same object back to the user okay so now let's check this this time i'm going to use the postman client this is a really useful application to check your endpoints okay so this is it and now we need to say i'm going to submit the post request to my endpoint localhost 8080 then the endpoint is at city but this time we need to supply the body parameter which is going to be a json data okay in json data we need to provide the key and value pair right and the key parameter should be the same name of this variable okay so i'm going to say id then we are going to create a new row in the table so we say id equal to zero and the second parameter is city name which is this one and here we say toronto okay now let's submit the data here it is let's check our database refresh it this is our data okay now let's fetch the data from the postman client using this endpoint get cities create a new request this time get request and this one gets it is see we got the data now we can update the data using this same endpoint and to do that let's say the id of toronto is three this time instead of zero we need to pass three right so we are going to update this entry so this time let's say toronto city and just send this the name is updated check this here here it is okay so this is how we access database using springboot application and so far we have seen that how we can fetch all records from our table and how we can insert a new record or update any existing record right now let's create one more endpoint which is for searching this time user is going to pass a city name along with the request and we are going to use the city name to search our table and by this mean we will make use of this repository as well let's see how to do that i'm going to use get mapping this time and getcity the singular one and public city i'm going to return the city found get city and expecting the user to pass the city name if found just return city service dot get city and we are going to pass the same city name that user has passed right now create this function and here we need to say city repository dot fine by city name you can use find by anything which is here if like there is one more property here like city code you can simply say find by city code but the first letter of the variable should be capitalized okay here city name c is small but here it is in capitals right now we need to define the same function in our repository okay so let's create this function okay we need to pass the city name as well okay and here we need to simply say return city if find by city name returns any data here city name okay now we have defined all is good let's check once user send the request with city name and here we are using city service get city function we are passing the city name then this function is calling this function it's basically implementing this function and passing this city name which has been passed by the user okay let's check it this time we are going to hit this endpoint with city name okay let's access this endpoint getcity and in request data we need to send a form data and city name should be like let's search this one toronto city set here it is so this is how we can perform the search operation using spring jpe repository so we have seen all three operations okay one more thing i want to tell you here that in terminal you can see that the spring data is showing all the sql that it is executing in background and if you want to debug if in case any query fails during execution you can directly copy this query and you can try executing this query in your database query console here now here you can remove the rear conditioner or you can just pass anything that you are trying like in our case we were trying toronto city and you can check your care query by this way okay so this is it for this video i'm going to come up with more advanced spring boot concepts in my future video and if you don't want to miss any notification regarding my new videos then hit that subscribe button and please hit that like button if you like this video okay thanks for watching bye bye and take care
Info
Channel: Let's Sort It Out
Views: 21,514
Rating: undefined out of 5
Keywords: spring boot, hibernate, postgresql, restful, api, vs code, visual studio code, jdbc
Id: n1iS8NpEP14
Channel Id: undefined
Length: 37min 27sec (2247 seconds)
Published: Mon Jan 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.