Java Spring Boot with MySQL using Spring Data JPA Full Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
self spring data jp or youtube so intra [Music] hello everyone very warm welcome to thin constructive i am isha in this session i will be talking about data persistence for spring boot applications using the spring data jpa and mysql databases in this session i will be giving you quick overview for all these three technologies and then i will be telling you all the required dependencies for connecting spring boot application to the databases and we'll be testing it using mysql workbench also so stay connected watch the session till the end so that you don't miss upon anything all right so let's have a quick overview of a spring boot first springboard spring boot is a framework which is very helpful to quickly build any java application and make it production ready you can easily create any standalone production grade spring based applications here because it comes already embedded with tomcat jetty and under two servers it also provides the starter dependencies so you can easily configure the dependencies and make your system ready you can easily configure third party libraries in any spring boot application and make it ready to work it also supports a lot of production ready features such as health checks or matrix etc which will make your application self-sufficient all right now let's talk about a spring data jpl so what is a spring data jpa it is a part of a spring data family which makes it easy to implement jpa based repositories that means earlier when we used to do this process that means to connect application with databases the process was little longer and cumbersome but after the spring data gpa the developers life has become really very very easy what developer need to do they just need to do the configuration that means include dependencies and write repository interfaces and if they are supposed to write any custom finder methods that they need to write that's all the spring will provide the implementation automatically they need not to write those detailed implementations so things have become very very easy for developer to do after the spring data jpa that is where i'm using spring data jpa in today's demonstration all right now let's have a quick look on spring boot and spring data jpa important annotations which we will be using in today's demonstration so what we will be using definitely the first annotation spring boot application which is a combination of enable auto configuration component scan and configuration because spring boot application is the kickstarter for any spring boot application so this is the point from where any spring the spring boot application gets started all right another annotation is at entity annotation this will tell that this class this particular class wherever this annotation is being used is the entity class and this thing will be stored in the database all right at auto wired annotation this wherever auto wired annotation will be used this will inject beam dependencies in that particular class at overwrite to override any method at id to indicate that this particular field is the id field add generated value to generate values automatically all right so all these things will be seeing in a while so stay tuned watch the session till the end so you don't miss upon anything now let's have a quick look on mysql overview so mysql is open source rdbms that is very it is very very popular because mysql provides a very rich set of features and variety of features which are required to purchase data and database and play around with that data all right and it is open source that means it is free to use it is easy to use and it is very very reliable and scalable and mysql since it is free so any small project can pick up this and use it and also any large enterprise level application so it's not like that it is free so only small project should be using it no it's very very feature rich and support is very good that is where any large enterprise level applications also uses it for example facebook twitter youtube so those are big names big companies using data day in and day out are using mysql so now you can understand how important and how good mysql is so this is the database we'll be using in our demonstration all right now let's see what we'll be building in this session so we will be building online store information in this session so for this online store we will be building an entity class and what will be the properties of those of that online store entity class it will contain a store id store name store detail and store phone number and here is the data set which we will be entering via spring boot application in the database all right so i will start building the spring boot application as i said right from the scratch so i will be using spring initializer to build up the application or rather a spring boot application from the beginning i'll show you the dependencies which you should be selecting in order to build this kind of application all right and then we will be evolving that spring application to store the data in the database followed by certain operations i'll switch to the browser window now and will open a spring initializer from there all right so here is a browser window i'll just open a spring initializer all right so i'm on a spring initializer site i will take this project as a maven project if you wish to use gradle you can use that also language as java default spring boot version i'll change the group id to let's say com dot thing constructive okay artifact let's say i am building a database application so i wish to give the name as db demo okay demo project or rather i will say db demo project for a spring boot application okay packaging let's have it as char java let's say the default 17 i'll go ahead with now the important part is i will start adding dependencies okay so what i'm doing i'm going to use a spring data jps so i should be using jp jpa dependency okay i'll select that then i would add another dependency let's say i'm using my database as mysql so i will just say mysql driver so these two important dependencies i should have in order to build the spring boot application with the spring data gpa and mysql all right so these are the must have dependency after doing so i will say generate it has generated the db demo dot zip you can see it here okay i'll quickly extract this zip and load it in the intellij editor all right you can use any java editor of your choice so here my project is loaded in intellij editor here we can see src folder with main java resources okay with application.properties and db demo application is annotated with the spring boot application so this is my spring boot application here is application dot properties which is by default empty and let's have a quick look on pom.xml also because that is also very important for us to see so here we can see springboot starter parent dependency which will tell this is the springboot application followed by the group id artifact id whatever we have given okay the description java version you can notice we had selected 17 so same is given here now the two important dependencies we should look at is the spring boot starter data gpa this is what we wanted to use spring data jps so this is the dependency for that and another important dependency is mysql connected dot sorry mysql connector java because we are using mysql database to persist data so this is another dependency which is the must-have dependency in pom.xml all right so now this is a basic spring boot application with all the required dependencies okay now the next step is to give the mysql specific configuration to the spring application so that it should know where to look for database that means the database connection string database username password all those important stuffs we should be configuring in the springboot application where should we configure all that the place is this application.properties so this is the important place where the entire important key configuration of a spring boot application should be given okay so this is the bare minimum basic configuration which you should be giving for configuring the mysql database in a spring boot application all right how you should be doing you should be configuring the propertyspring.datasource.url okay this is the one is equal to the connection string so connection string will start with jdbc colon mysql because this is the db we are using then colon double forward slash then the ip where the database exists in our case it is localhost because mydb is running on my same laptop same machine which i am using to build this application so i am giving as localhost you can give any ip where the db exists then the port so 3306 is the default port of mysql so the same port i have used so i have given 3306 port number here you can give the port number wherever your db is actually listening all right then forward slash then schema name so this is something very important you should know that a schema should have been created okay this schema should be existing otherwise this won't be able to establish the connection you can have a already created schema name here and then use ssl i have given as false if you are using ssl certificate you can give here true and then down the line you should be giving the certificate path also then after that you should be giving spring data source username so username you should be writing in my case i have taken it as root you can have any user name which is already created in the mysql database here and can give the username here then a spring data source password and you can give the password whichever is configured for you all right after that this is a spring jpa hibernate ddl auto i have given it as create so what this create will do every time whenever this spring application will come up if the table is already existing it will drop and recreate a new table with new set of records for you if you don't want to drop the table every time you can replace this create with update also okay so i am proceeding with create you if you in your case depending upon your project requirement you can have update also all right this is the bare minimum configuration you should be doing to connect your spring boot application to the database all right and now switch to workbench and we'll show you this test users schema all right so here is a mysql workbench and you can notice this test users schema is existing here okay and this same schema i'm using for this demo if you don't have any schema or if you want to use different schema for this demo you can create one all right and can show you some ah tables are existing here but our store table is not existing that should be created with my spring boot application all right [Music] let's first create the entity which is my store info okay store information entity all right okay since um since we know this is my entity class so i'll first put the annotation entity here okay that's how the spring will know this is the entity class okay you can notice java x persistence package is imported here all right so let us start creating the properties of this store information so first one is ids so let me just take it as integer store id okay and this is the id field so i want to put the annotation as ids so that spring application will know this is the id field and i want id to be auto generated so what do i do i will just say generated value and i will put strategy is equal to generation type auto all right so this is my first property i will just create another property which is my store name so i will just put it as a string store name okay all the time then another one is store details that should again be string store detail and then store phone number again phone number i am taking as a string all right so these are the four properties which we are using let us create the constructors i'll just go to generate i will say constructor first i will create empty constructor i will correct its positioning okay then we'll create parameterized constructor i'll select all four properties and we'll get the constructor for this we'll again sort out the positioning stuff there you go okay now let us create getter and setter for all these four properties i'll just say get an setter we'll select all properties okay all right we'll again fix the positioning stuff some positionings come here okay so i've got my entity class created with the id field annotated and given its generated value also which will create id numbers automatically okay i'll show you that i am not giving any id numbers but it is getting registered in the database okay so my entity class is ready next step i will be creating the repository interface all right how do i do that i will just say repository dot store information repository all right i will just change this class to interface okay so here is my repository interface what it should do it should extend crud repository okay it should extend thread repository and here i should be giving the parameter so crud repository here we'll be taking two data types first one should be my entity class for which i'm extending it so what is my entity class name that is store information okay and the second parameter is the data type of id which is my integer okay so here for this store id i have marked it as id this id parameter data type will go here all right here i can write any custom method which i want to implement let's say for fetching any details so let us say i want to return a list of store information okay when i will say find by name find by rather i should say store name okay because this name mapping should happen and the camel casing must also be followed okay and what should it take it should take string name let's say okay next method i would want to have is let's say find by store phone number okay and here the string of phone number should be passed okay let me just match this name store phone number store phone number perfect okay so now you get the point this this should match with the property name all right okay so these two extra methods i would want to have i have given although this repository already provides us a lot i'll show you in a while how now we will go to db demo application in this db demo application we will do some changes okay so first thing we will implement command line runner here implements command line runner okay why do we do that because i want to override the run method public void run string okay i want to overwrite this method that is where i have implemented command line runner because i would want to call the data saving methods or fetching methods right from this run method so i should annotate it with override okay and my data persistence or fetching data code will go here all right before that what i need to do i need to auto wire repositories here which repository which we have just now created my store information repository so i'll just copy this name and we'll create the instance of this okay i should put auto add annotation here then only spring will know that this bean already exist all right so i have put the auto wired here and the store information repository instance is created now with the help of this instance we can save data in the database in this case mysql database okay how do we do that i'll just show you this is my repository instance dot for saving or for putting the data in the database i will call save method okay and inside the save method the complete entity will go okay that means the record which i would want to save should go in here okay so what do i do i'll first create the record here what is my entity store information okay just say store information one is equal to new store information and its parameters okay okay just one change i should be doing in the entity i i don't need this store id here in the constructor why because we are using auto generation mechanism for this store id so i am just removing it all right so in the constructor we should be passing three parameters store name store detail and store phone number okay so we'll come back here and just say store name amazon all types of bags are equal here all right let me just shift it to next line so that it is more readable and then the next parameter is phone number my phone number let's say xx xxx okay so this is for store information and this record i will pass here okay so first store information will be saved after execution of this line okay now what do i do i would want to query this okay how do i query that i will just say my store information repository dot let's say i want to find by store name i will just pass the store name here which is this one okay what it will return it will return the list of store it can be one it can be many in this case it will be one only but it can be many also so i need to iterate it in order to get it printed okay so i'll make use of for each in this forage i will be let's say passing the value and i want to get it printed so i will just say system dot out dot println and here i will print the value okay done all right so now let us execute this and see the results currently i will just show you mysql workbench once again and you can notice there is no table as of now called as store information so this is my schema which we are using i will refresh you can see only two tables user and hibernate sequence okay let us now execute this springboot application and see what happens let's spend some time here we can see what happens my jp entity manager is initialized dbw application is started all right i have got this entity information printed here now that we have executed this application let us go and check the database what happens here in the database when i will refresh now i have got a store underscore information table and then i will say select rows from this table what am i getting i am getting a store id as 1 store detail has all types of bags are available here store name amazon and the store phone number as xx here that means the data which i wanted to store in mysql database is stored here all right we'll now do some more stuffs here first let us fix this what i am getting here something like this but what i want is a completely readable record which we have just now persisted okay so let us go back to the entity class and do a bit change there what did we miss here we missed to create its two string method so i'll select these attributes and we'll say generate two string okay i'll generate its tostring method okay again we'll fix up its positioning let's say i'll place it here okay so now when i will query this i should be getting information something like this okay so now let's again execute this application and see whether this issue is fixed or not notice some change see store information store id one store name amazon store detail this and store phone number xxx this is what we try to persist so it is persisted and we are able to fetch this record these values from the database and print it properly here all right so don't miss overriding two string method that will be very useful okay so now moving forward since we targeted to persist four records just now we have persisted only one so i'll now create four records and we'll save them okay i'll just paste and we'll do the required changes so i'll just make it as 2 and what is the second value i wanted to insert flip card and all books are are available here so i'll just change the store name to flip card will say all types of books are available here and we'll change the phone number to pi by y all right the next value is may show all fashion items are available here okay so third record items are available here and what is the phone number all right and then the fourth one so it will be four and mintra all fashion items are again available here so i will just say store name as mantra all description for these two are same so i will just paste it here okay and its phone number is okay so here are four records we have created and we will save these four records in the database one by one okay so store information one two three four so this will let us change it to three that is better okay so all four information we have stored in the database okay we have done find by store name for amazon okay let us do a find by store detail that is that will be our next query okay how do we do that i will just do changes here i will just say find by store what is my repository method okay find by store detail we haven't yet created so let us create one that will be good so we will do three find queries okay so store detail okay and here again detail okay find by store detail so this is the query method we have and what do we do we will put it here find by store detail and let us copy this if you notice the same store detail exists for two stores so ideally in this iteration we should be able to print two records okay okay and the iteration will remain same and now we will put a query for store information repository find by store phone number okay and we will give the phone number let's say 5y by 5 times 5 all right and then we'll do for each and we'll do iteration for printing system dot out draw println okay and we'll say so these are the different methods for finding so these are the different methods for finding let us do one more that is find by id okay so i'll just copy and do the changes that will be better so i'll just say find by id okay and id is my integer value so i should be giving some int let's say 2. and not necessarily this value might be present so i will just say instead of 4 h i will just say if present then you print this value okay so these many find operations we are performing here okay so let's now um first execute check the database and check the the results of these find queries then we will proceed towards counting operations all right so let us now execute our spring boot application and see what happens let's see the logs so what first thing i am getting amazon is store details what was my first find query find by store name which is amazon right second what i am doing i am doing find by store detail and store detail is this which is present in exactly same store detail in two stores okay which is store three and store four misho and mantra all five fashion items are available here all right my next find by query is with store phone number and number is y y by y all right so what is my uh why phone number store is flip card that is a store number two and eventually when i am doing find by id which is two that is again the same query okay so all results i am getting beautifully here everything is printed properly here now let us check our database what changes have happened there so i am back to the database earlier when we executed the query we got only one record because we created only one at that time but now we have already created four records now let us execute this query again select star from test users dot store information and see what happens ok i will re-execute the query and you can notice i am getting all four results here one two three four amazon flipkart micho and myntra all right so data is persisted in the database and i am able to query the data properly okay we'll have uh we'll see one more scenario now so i'm back to editor and i will now put accounting for this how can i count all the records existing in the database so i will just again call my repository instance so store information repository it is actually giving a lot of methods count i'll just call count and i would want to print it so i will enclose this thing inside system dot out dot okay and we'll close it here okay so i should get a number 4 from this so let's see what do i get let me again go ahead and execute my application and here is the log details all the fetching information would remain same and after that i am getting number four so in database there are total number of records four okay one more thing i would like you to notice find by id and count these two methods i have never written inside my store information repository so these are the default methods given by crud repository and i can use it directly no change but whatever methods are given and also one important method which is save so if you notice in my store information repository see here i have never written save count or find by id what have i written is only these three because these three are specific to stor store information entity and these specific methods will not be provided by crud repository so i have written the signatures here and the implementation is automatically taken by springboard okay so it's that magical you just need to write your custom find methods here and those implementation you need not to worry it will be taken care by string and you can directly use them like this all right so it's that simple i hope this part is also very clear to all of you okay if you would like to delete a particular record from the database let us say i want to say delete by id how would you do that i will just again take a store information repository and we'll call delete see so many deletes are available there i can pass the entire entity which is candidate for deletion or i can say delete by id other versions or of delete are also provided so let's say for this demonstration i will call delete by id and i will pass the id number 2 okay and after that again when i will call count let's see what changes okay so what did i do here i will i have created four records called its find and then count and after that i have deleted id number two record and then again i am counting the record let's see what happens and let's do one more call here find by id 2. okay let us now again execute our application and see what changes let's go back to log details what happens initially same find queries everything is good my account is 4 after that if you notice count is changed to 3 and after this count also i have called find by id 2 but now there is no id with 2 exist so the count is decreased to 3 and no more count 2 record is printed whereas if i will go up here find by id 2 was getting printed with flipkart okay let us verify this information from mysql workbench also so this is my select query select a star from test users dot store information let me just go ahead and execute it what am i getting now this time store id 1 3 and 4 amazon misho mintra flipkart is gone so no flipkart is here all right so i hope this demonstration and the usage of repository pattern spring data jpa with the spring boot and mysql all these things all the configuration and all the code changes are very very clear to all of you and if you have any question please ask the questions in the comment section i will try to get back to you as soon as possible all right thank you everyone for watching this session i hope you found it useful if you really like the session hit the like button subscribe to the channel to remain updated with more sessions and also i would like to request you to share it with more and more people so that everyone else will be benefited share this session with more and more people so that we grow together more stronger as a community and also i would like to mention if you like any specific part of the session annotate that part in the comment section so that everyone else will be benefited from that thank you once again see you in the next session bye for now [Music] hey
Info
Channel: Think Constructive
Views: 13,808
Rating: undefined out of 5
Keywords: spring boot with mysql, jpa, mysql, spring boot, spring boot crud example with mysql, spring boot data jpa, spring boot hibernate, spring boot jpa, spring boot jpa mysql, spring boot jpa mysql crud example, spring boot mysql, spring boot tutorial, spring data jpa, spring data jpa mysql, spring data jpa tutorial, spring framework, spring jpa, spring mvc, java, spring initializer, esha puri, findbyid(), save(), deletebyid(), count(), mysql workbench, spring, spring boot jpa tutorial
Id: xl5xxbp5vcE
Channel Id: undefined
Length: 40min 37sec (2437 seconds)
Published: Mon Jul 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.