YTread Logo
YTread Logo

Spring Boot Tutorial | Full Course [2021] [NEW]

Jun 07, 2021
what's going on guys assalamualaikum welcome friends code in this

course

I'm going to teach you everything you need to know about

spring

boom

spring

bo is by far one of the most popular frameworks and I asked what people want to see and they told me they wanted to see a

course

specific about Spring Boot and also some other

tutorial

s, so this first course is basically the introduction on how this framework works and we are going to build an API where we will have three layers and also We will connect to a real database and you will see. You know the power of this amazing framework.
spring boot tutorial full course 2021 new
If you are new to my channel, go ahead and subscribe. Also give me a thumbs up so I can continue recording these courses. and um yeah so if you're not part of the buddy squad community go ahead and join because the community is growing and I would love for you to be there without further ado let's start this course right before we go on and I just want to say if you want Take this course at your own pace and receive a certificate, this course is available on my website for free. Yes, free, you can sign up and you will get a certificate.
spring boot tutorial full course 2021 new

More Interesting Facts About,

spring boot tutorial full course 2021 new...

You will also practice while I teach because that is the best way you are learning any new framework or programming language, so I don't want to, I don't want to waste your time, I want to add value, so practice while I teach and also join the private Facebook group and to Discord to interact with other students if you have any questions, you can ask questions there and participate. Yes, basically we are building an amazing community where people learn from each other and yes you should be a part of it and finally if you need to have access to the

full

source code.
spring boot tutorial full course 2021 new
At the end of this course, I will provide you the github link where you can find all the examples without further ado. Let's start this course. Springboo is an amazing framework for building applications, whether you want to build back-end applications. or

full

stack applications using java or kotlin Spring Boot is the framework to move to Springboard, it is by far the most popular framework at the moment and by learning Springbook you will be in demand for jobs. Everyone uses Spring Boot and you should too if you need it. my channel go ahead and subscribe give me a thumbs up and also go ahead and join the private facebook group and discord.
spring boot tutorial full course 2021 new
As I said, Springboard is an amazing framework that gives you everything you need to build applications if you need security. you can use the security modules available if you need to login you can use login integration to connect to databases whether you want to connect to mongodb, postgres, mysql they made it very easy for you as a developer connect to any database metric, to check how your application is doing. behave in production and the good thing is that it is very easy for you to learn as a beginner, as you will see in this course, plus it is production ready, you can create microservices, it has dependency injection, built-in configuration, great community and much more.
I am very excited about this course. Next, let's go ahead and learn how to get started with Springbook, so for this course we will create this entire application, excluding the front end where we will have the API here. So the API will receive the get request, it will be placed and removed, and then we will have a service layer, so this is mainly for business logic and then we will have a data access layer and this layer here is responsible to connect to any database. In this course we will use a real database and you will see how easy it is for you to implement all this with Spring Boot.
Now, so we can get started with Spring Boot, let's go ahead and navigate to the Spring initializer. So I'm going to leave the link in the description of this video where you can follow along, so on this website this is where we can launch any given springboard application, so here you can see we can choose between maven or gradle. I'm going to leave maven and in java languages ​​kotlin or groovy I'll stick with java and then choose any version, so literally any version I'll leave 2.4.1, this might be different when the time comes. Watch this video, but everything I'm going to show you today will work in later versions.
Here you can see that we can customize the project metadata, so I'm going to leave everything as default and for packaging, go ahead and select jar. So this is the most common type of package for Java applications these days and then for the Java version, I have version 15 installed, so I'm just going to select that one and now let me scroll up and here, let's go ahead and select dependencies. Here we can choose the dependencies that our project needs, so this list is quite large and of course feel free to explore this list, but for this course let's go ahead and select Spring Web to create a web that includes restful applications using Spring MVC, so right here.
I'm going to press command and then b because I want to select multiple dependencies right here, so I selected it and scroll down so you guys have a bunch of spring safety stuff if you want, so we'll leave it. come out for this video and here go ahead and select spring data jpa so we're going to connect to a database using jpa and hibernate and you'll see this in a second also scroll down and here we can use an in memory database if we want or if you want to connect to a real database like postgres you can also do that for this course we are going to connect to a postgres database so here let's select the postgresql driver so I just hit the command there again and you can see that you can connect or actually you can work with Spring's data reactive elastic search and basically the list goes on and on and on.
Rabbit mq web sockets kafka and Spring Batch Java Mail and you can see the list is crazy including Graphite and Prometheus so now that I've selected everything what I'm going to do is escape this so hit Escape here we go and also what I'm going to do is share this with you so that I can download the exact same dependencies from the link in this video now if I open up my desktop and there we go so you can see that we have the demo.zip and this has already been extracted . Next, let's go ahead and open this. project with intellij for this course I'm going to use intellij, which is by far my favorite ide and the one I recommend to most developers, so I have the definitive edition here, but if you have the community edition, this one will also work, the reason why i love the definitive edition is because it has a lot of great features when working with frameworks like springbook and also when working with databases so i highly recommend you give it a try so im going to open the definitive edition . editing and by the way I'm using the jetbrains toolbox so this tool here allows you to download and manage all the ides and there we go so now I'm going to click open and then navigate to the desktop and then to here. open the demo and open this folder so from this point you should pick up everything you need for this project including downloading all the dependencies just wait a second and voila you can see it was super fast.
Now to what I'm going. What you need to do is open the demo folder and then open palm.xml so here you can see that we have the project metadata here and then the dependencies, so we select Spring Boot Starter Data JPA and then we start a web as well. here and the dependency for postgres is here so this is what gives us the ability to connect and work with postgres databases and here we have this dependency given by default and this is for testing and to be honest this okay here, you can see the Java version 15 that I selected and this is more or less palm.xml and you can also see that we have the main dependency here, which is spring

boot

starterparent, there you go, you have created it successfully. an empty springboard app, then let's go ahead and start the app and watch it run to start the app, let's go ahead and close this palm.xml and here in this demo folder right here so we have the source and inside the source we have main and then test, this is where all the test code goes, so let's open the application tests so you can see we have an empty test here, so this is where you put all the test code. test if you want to learn more about testing.
Go ahead and check out my website where I have a complete course covering unit testing, integration testing, mocking and tdd. Now let me close this and if I open the Java folder right here, this is where we put our code, but the most important thing is that we have this demo application, so if I open it, this is a basic springboard application and you can see the annotation here now inside Java, so inside main we have resources and static template and application.properties, so this application.properties this is where we set all the properties for our application as well as the environment specific properties and you will see this in a second when we connect to a real database, then we have static, so static and templates.
This is when you are doing web development like working with html css javascript and all that good stuff now open the demo app so let's run the app and here you should see this play button or you can right click and then run the app or you can run the application from here. so let me run the application here and give you a second to let you know that this should fail because it will try to connect to a database right here, you will see that the proper driver cannot be determined here, so what do I do? what to do here is the following, I'm going to open pom.xml and where we have the spring

boot

jpa startup data, let's go ahead and comment on this dependency for now, there we go and what I'm going to do is right click on palm.xml and then maven and then reload the project, so sometimes it may not reload automatically, but this ensures that you actually remove this dependency from here.
Now let's go ahead and launch the app and there we go, you can see there are no errors so be patient. Note that we will be placing this dependency bank once we are ready to connect to our database, so here you can see that we have the Tomcat logs started at 48080 right here, which means that we have a web server up and running and If we try to access our web server on this port, we will get nothing because we have not implemented any endpoint. Next, let me go ahead and show you how easy it is for you to implement a simple Restful API.
So far, we have the server up and running at 48080. So what I want you to do is navigate to your web browser and then type the localhost column and then enter 8080 and you can see a white label error page appears and here the status is 404, which means it was not found. Let's go ahead and quickly implement an endpoint that will return us a json, so let me go back to intellij, let me stop the server and here I'm going to hide this. I'm going to close palm.xml and remember this dependency. right here we're going to add it back in once we're ready to connect to our database, so close that and application.properties as well, so what I'm going to do is inside this demo application class, go ahead and type add and then rest and then controller now let's go ahead and have a class here public or actually a method so public and then say string and then hello and here what we're going to do is we're just going to return hello and then a world like this now for So that this method serve as a quiet endpoint, we have to write it down in and then we get the mapping, we put the mapping after the mapping, etc., and you'll learn all this in a second, for now we want to get something. off our server so here in get and then mapping and this annotation makes this class restful makes this class serve rest endpoints and the only rest endpoint we have here is this string hello and it returns hello world now let's go ahead and start the server, there we go, the server is up, let me open up my web browser and before we had a 404.
Now if I refresh, you can see that we have Hello world here and what's really interesting here is that if I change this. to list, so I'm going to change this to a list of and then I'm going to chain here and here I'm going to say list point and then of and then I'm going to pass hello as one element and then world as another element. like this and if I restart the server, stop it and run it again, there we go. If I open my web browser again and then reload the page, you can see we have a json back, so hello world, this is really good. go so you can see we have hello world and we basically did nothing and we get a json array out of the box so this is really awesome so obviously we don't want this simple API where we have hello world but we want to build all this so So what we will do next is the following: we will create aclass right here, we'll model it as a student and then give it some attributes and behaviors and then ultimately the students will end up in a database, but for now let's start with a simple class that will represent a student and then implement the student based on everything in this diagram, so let's take this student here to represent him in a class. that we can start building this entire API, so let me go ahead and open up intellij and the first thing I'm going to do here is create a package, so here I'm going to create a package and I'm going to name it. as a student you will also learn how to structure your applications using springbook so inside this package now we have to extract all the codes related to students so here we have to have the student class so this will be our model here like this than the public student class and now let's go ahead and define private and then we want long for the id, we also want private and then string name, so here name, age, date of birth and h. so here private and then integer h and actually we need private and then local and then date and this will be dob for date of birth and finally I almost forgot so let's have an email so let's chain and then send an email like this and let's find this diagram should be email actually let's change this to email right here and we're ready to go so now let's generate the getters and setters and constructors so let's go ahead and generate the constructor so constructor and I'm going to have three builders. so I'm going to have the no r constructor, I'm also going to have another one with everything, there we go and let me put this on a new line like this, like this, the constructor with everything, let me also have another one without the id. and you'll see why in a second because the database will generate the id for us, there we go and a new line like this and finally let's generate the getters and setters and obviously we could use something like lombok to eliminate all this boilerplate code. there we go and finally tostring to string and then ok and there you have it so now we have a class called student so what I'm going to do now is open the demo app and instead of a list of strings I'm going to say list of and then student and then here what I'm going to do is on a new line, I'm going to delete that and then I'm going to say new and then student and let's pass the id of 1 and then let's name mariam and this has to be an l because it's long here we go and here let's have mariam dot and then jamal at gmail.com comma and we also need to pass the local date of birth and then date and then dot and after and if I put this full screen there we go and here let's pass the first wall here so let's say that she was born in 2000 and then the month, so let's say it's January and then the day, let's say January 5th just like that and then the comma and then here let's just say 21.
Actually, we could subtract this, so subtract now minus date of birth, but we'll do that in a second, so now let's go ahead and start the server and by the way, you'll see that we have id. column name column email column don't add them because intellij adds them again don't add them so if I open my web browser from time to time here remember we had this list before if I reload the page you can see it now we have an array of and then objects, so our class became a json object right here and we have an array surrounded by it, so this is really awesome.
You can see how easy it is for us to start building APIs with Springboot. We have this class here and we also expose an endpoint that returns a series of students. Now is the perfect time for us to do things the right way, so we want to structure our application on this whole architecture where we have the API layer service. layer and then data access, so we already have the student class, let's go ahead and create a class that will serve as the API layer, so open intellij and right here I'm going to collapse this project expanded and inside student so that everything is inside of student, so create a new class and call it student and then controller, here we go, now we have this class that will have all the resources for our API, so remember that before, if I open the demo application, we had this annotation correct. here, so rest the controller, so what we're going to do is remove it from here and let's also take this mapping, so I'm going to cut this so this class here should now be empty here, so empty class and let me. delete all those imports just like that and you can see the keyboard shortcut.
Now let's go back to our student controller here and here we're going to annotate with add and then rest controller and then what we also want to do is say this we'll have a request and then we'll map here and we can pass in a couple of things, one is the route here and the path will be api then v1 so version one of our API slash then student so instead of going to localhost 8080 now I want localhost 8080 API to slash v1 then student let's paste the mapping too of get that we had before here, so put this full screen, but now instead of hello, I'm going to say get and then students, so now we have a get mapping for our student controller, so let's start the application, here we go if I open my web browser and here I remember that before we had localhost 8080, so if I try to get to this endpoint, this should not work, but now we have changed it to slash API. slash v1 and then slash student come in and look at this so you can see that we have the API working, but now things are much more organized.
Next, let's go ahead and create a class that serves as business logic to properly manage students, so you have our API layer here now, the API layer should talk to the service layer to get some data and that Service layer should also communicate with the data access layer to get the data so it does a round trip like this directly from the client API. service data and then all the way back, so now let's move this method here to the service layer so that the service layer is mainly responsible for the business logic, so here let's open the project tab and then here we go to create a new class and then call it student and then service, there we go and what we're going to do is take this method, so let's take this method and then let's put it inside this service, just like that and now we can continue.
Back to the controller and here, this will no longer be the case and instead we have to use the method inside our student service, so here we have a private reference and then the final student and then the student service like this and let's add it. to the constructor, so let me put this full screen, there we go and now I can say return and then student service, get students, so this is now a much better approach and we are using the previous design pattern, so if I go back to the project o Actually, open a project and then the student service.
You can see that it is now being used, but ideally it should currently be a static list. We also want this to come from a database, but we'll worry about that in a second. Let me go. Go ahead and teach you about some annotations and dependency injection within Spring and Springbook. Okay, so far we have three classes and inside the student controller here, so let me put this like this, you can see we have a reference to the student service and then here we pass it inside the controller. Now this isn't going to work because we don't have a student service instance.
Now the way this would work is if I said equal to new and then student service like this. and now this will work fine, but when writing code we should avoid things like this and use dependency injection as much as possible. So how do we say that this student service should be injected into this constructor right here or whatever we pass into this constructor? be injected, well, we have this annotation called at and then auto and then wired. Now we are saying that this student service should automatically connect for us, so a magic instance will be created for us and then it will be injected into this constructor and then all of this will work. but now we also have to say that this student service is a class that must be instantiated, that is, it has to be a spring bean, so to know that it is a bean, we can use this class or in fact, we can use this annotation called add and then component now, if I go back to the student controller, you can see that this red error is gone because now you know where to find it since you're here, so if I go back one more time you'll see that I'm using add component, but with Spring we have annotations that allow us to be more specific, so here we don't want this to be just a normal component, we want it to be a service, so in the service in the component they are exactly the same, but this is more for semantics.
It's more for ease of reading. By looking at this you know that this class is meant to be a service class, the same way you saw here for this controller we use the controller add and then rest for the service we use and then service. restart the server and then go back to my web browser and here I'm going to refresh this and you can see it still works so now we're using dependency injection and we've actually layered things up so that we have the API layer talking. successfully to the service layer and the service layer is returning some data to the API layer.
Next, let's focus on data access by connecting to a real database and then store the student inside the database and then remove it if you have any questions, please let me know; otherwise, let's go ahead now and connect to a real database so that we have the service layer of the API layer. Now let's focus on this data access layer for this course. Let me go ahead and steal some code from my spring data jpa course and in this course you will learn everything you need to know about spring data jpa so we cover what is bringing data into apa how to connect to real databases how assign classes to features entity lifecycle queries, page salted one-to-one relationships, one-to-many, many-to-many transactions, and much more.
Here what I'm going to do is go into src and then resources and then application.properties and I'm going to grab all of this so here again I'm going to leave this in the description of this video so you can follow along now I'm going to go back to intellij and I'm going to to open applications.property or properties and then paste it in there, so this is the configuration that we need to connect to a database, this is the URL, this is the port and here that and then here this is the database, so let's call it student code instead of friends code and then we have the username and password because I'm on a Mac and I'm using the Postgres app so this for local development is empty and then I have jpa create drop, which means we have a clean state every time we run the application and then display the sql.
We also want the dialet to be postgres and then format the sql so you can see all of this in action in a second. Now, the easiest way to download and install Postgres on a Mac is by installing this app called Postgres App, so it's really simple and allows you to connect and experiment with different versions of Postgres. The default port is so here it is 5432 if you are on Windows go ahead and download it from Enterprise db.com and I will leave the link in the description of this video and I will download it for Windows version right here and you can download the latest version 13.1 as I speak.
I'm also going to leave a link to videos I've made in the past on how to install the database if you have any problems, but if you want to learn more about databases, check out Amigosco.com and if I go to courses, here you will find spring data jpa. So this course covers everything you need to know to work with databases, so I'll leave a link for this course as well. Once you have installed Postgres, we need to create a database and that is what we are going to do. Do the following to connect from our application to our database we have to do the following so here remember we have changed this to students so this is the name of the database here so I already have a database of data in operation and again if desired. to learn how to work with databases visit my website so this is spring data jpa but i also have a free course just in postgres that includes advanced databases and database design and implementation where you will master everything there is to know about databases.
So here what I'm going to do is open my shell and type psql. If I do a backslash, you can see that we have a pair of bases ofdata here, but what What I really want is if I press Ctrl l to create a completely new database, so create a database and then call it student and then semicolon and that's it, now you can see we have a database of data that we can use now if I type backslash. ndu you can see I have two roles so friends code and postgres so here what I'm going to do is say grant and then all privileges and then the database that I just created as a student and then two friends code in there. let's go semicolon and this should be the database, so on and then the database there, we enter and we also hit postgres, so now if I clear the screen and then I type backslash and then I can see that we have this correct database. here it's called student so if I connect with backslash c and then student and then backslash d and no relations so now let's open intellij and I'm going to open palm.xml before remember we commented out this dependency so I'm just I'm going to uncomment like this and then I'm going to reload the project, so palm click, right click and then Maven and then reload the project, there we go and now I can start the application and there we go, so we actually connect to our database here you can see the records so we start ikari and then we complete it using postgres here and everything looks fine but we still don't have any tables in our database and that's what you guys I'll show you how to do it.
Next, okay, we successfully connect to our database. Now what we want to do is take this student here and use Spring Data JPA to create a table within our database that we can then add, delete and basically do all the credit operations. our database, so doing that is very easy with Spring Boot and Spring Data JPA, so here this dependency here that we have simply uncomment gives us this capability and if you want to learn about Spring Data JPA, go ahead and check out my course on Spring Daily JPA where I teach all of this, so now let me come back here and open up the student class and map this student. to our database just type en and then entity here and then say en and then table so this one is for hibernate and this one is for the table in our database and we also need to say add and then id and then here we are.
I'm going to generate a sequence then sequence and then generator and here I'm going to say name equals and then student and then sequence and you'll see this in a second, we also need to have the name of the sequence, which will be the same and also the allocation size , so our location size will be one and then here it will be a sequence and then a name like this and now that we have a sequence generator here I'm also going to say add and then generate value and then the default or recommended value for postgres is a sequence, so I'm going to say strategy equals two and then the sequence, there we go and the generator is the sequence that we just created, the student underscore and then the sequence, and there you have it literally.
This is all we need to do now that we have assigned this student class to a table in our database. Next, let's go ahead and launch the app and see what's happening under the hood. Okay, let's go ahead and test these changes I'm done using Spring Data JPA using these annotations on the entity in the table and then the id with a stream as well, so let's go ahead and restart the application and here let me put this a little bit more big so you can see we have some records being made and there we go so take a look here we have some records and we have created a student sequence starting with an increment one so basically this sequence is this so the sequence generator and allocation size are here, so increment one. that's what it means and then we have a create table statement here take a look at create table and then student and then all these fields and then the primary key is id so this id here and you can see this was super easy, right?
We just added some annotations and now we have a table in our database, so if I collapse this and here, what I'm going to do is connect from intellij to my database, so I'll have a new data source. We just hit here database and a new data source and then postgresql and localhost good database. This will be a student test for the connection, you can see it worked, apply fine and now we have a database inside, we have the public schema and inside we have the table. right here student and you can see the columns and then we have the sequence student, so if I open my shell, here remember that before when I typed backslash d, we had no relations now, if I type backslash d again, here we go.
You can see we have this table student and the sequence, so now let me describe the student and you can see we get exactly the same thing here, so we have the index for the primary key and all of these columns, so this is pretty cool. So what we're missing now is this part here, so the data access layer, we have the database set up and running with a table called student. Next, let's go ahead and implement this layer here that will interact with our database, so you saw the power of how we take a class and then assign that class to a table in our database, so if you want to learn more about databases of data and Spring Daily JPA, I have a series of courses that focus only on databases starting from scratch. um sql and postgresql so um those are the fundamentals those are the fundamentals that you should know and then I will explain to you how to design databases how to use erd diagram relationships and constraint transactions and all that and then I have the advanced database course and basically you know it brings everything together and then the fourth course is on Spring Data JPA so you can see it's basically a series of courses that focus just on databases and if you know Spring Data JPA well. so you can build, you know, complex applications with complex relationships, you know, understand, for example, indexes, unique constraints, one-to-one relationships, so basically everything is covered in my daily spring jpa course, so I I will go. a link in the description of this video so you can go and check out the course for yourself and yeah I would love to have you there so yeah I just wanted to say this and continue with this course okay let's go ahead and implement. the data access layer so open up intellij and right here I'm going to create a class inside the student package and I'm going to call this class student and then repository so this is the naming convention for anything that accesses your database of data. and specifically when you're working with jpa, which is what we do, now this has to be an interface and you'll see the power of this in a second, go in there, come on, so we have the interface there and let me collapse that now let me also. hit command n1 just like that now this interface here I'm going to say extends and then jpa and then repository right there now you saw this is actually using some generics and we have to specify a couple of things so first we have to specify So , the type of object that we want this repository to work with and also the id of the type that we want, so let's go ahead and say student, so this is the type that we want to work with. comma and let me put this just like this so we can see everything and here remember our student ID, so the type is long.
If I open student, you can see that the type is long here, so he is long and this is the type we want. repository to work on now we're going to annotate this with add ups, not backslash two, but add and then repository, the same way we add the service in the resting controller, this is in the repository because this interface is responsible for the access to data and to be honest, this is it. for this layer now let's connect things using dependency injection so here we open a project and we want to use this interface inside our service so here let me collapse this so instead of having a list like this, so static list, we want say private. final and then the student repository, here we go, then we want to add this to the constructor and then we will also annotate the constructor with add and then we will automatically connect it like this and now let's check this instead of saying we want to return this list.
What I'm going to say is let me copy this student because I'm going to need it in a second, but now I'm going to say in the repository here or actually go back and then point the repository and then check this so we can have a bunch of methods here, we have found everything, we can sort if you want by ID, we can save and we can flash, we can count exists, find one, save all, delete all and you can see we have a bunch of methods and To be fair, we haven't implemented any of this correctly and this is the magic of Spring Data JPA so here I'm going to say search and then everything so that this returns us a list and to be honest this is it so this interface We just implement this in the student repository, so if I navigate to this file, you can see that it is an interface that has all of these methods here available to us.
We haven't written a single line of SQL or code and we can search for students. from our database then let's go ahead and add some students to our database using this repository and then see everything will work for the API to provide access to the data from the service, get from the database and then return the response to our client. Okay, so let's go ahead and start our server there, let's get the server up and running and if I open up my web browser, remember that here before we had a student, Miriam, and now if I refresh this, you can see that we have an array empty and this is because there is not one in our database and in fact, to demonstrate to you, so here what I'm going to do is say: select, go, select star and then from and then the semicolon of the student come in and take a look at 0 rows, so let's go.
Let's go ahead and add some students to our database, so let's go back to intellij and here what I'm going to do is create a new class and I'm going to name it student and then I'm going to configure and annotate. this with add and then configuration like this, so this will be a configuration file and here we want to have a bean, so we want to have this bean right here, so let's say command line runner and this guy will not carry anything inside there, but here we want to return arguments and for now we are not going to do anything with arguments and here we are going to annotate this with add and then bean so that it is executed and what we are going to do is have access to our repository so that here we can inject the repository of students just like that and I'm going to call it repository and from now on what we can do is remember that we had a student before, so if I press the v command, this is the student that we had before and let me add. semicolon there and then import this and the local data as well and here what I'm going to do is add a static import like this so now this is our student I'm going to press the command v or actually the command shift v and this is mariam , let's also create another student, so here and this time what I want to do is remove the correct id because the database will generate it for us and this will be alex, so alex and this will be alex gmail. com, for example, and alex was born in 2004, for example, right, and don't worry about the age, we're going to sort this in a second and now we have two students and we want to save it in our database, so this is how we do. saving to our database is next, we invoke our repository point and then we can say save and take a look, we can save a student or we can pass a list of students, so here we want to save all and this takes a list like this . and I'm going to move to miriam and alex like this and then here and this with a semicolon and there we go now let's restart the server and check this so you can see that now we have a little bit more record and here we have insert in student these columns and then these values ​​and then we have another insert, so here in student and then these values, we can basically see the SQL running in hibernate when we invoke save all, so now let me go back to my web browser and remember. before we had an empty list, so if I update this, check this so that we have two students coming from our database and here, if I select start from student, come on, this is from our database and even inside intellij, so here I can open the The console says select basically the exact same thing, so let me be a little lazy and copy this command here, we go back, we paste that in a semicolon and then we run and you can see it's a little bit slower, but you can see that the results are also here and the nice thing is that I can see them and then transpose them and check them tolet us have the results here, so this is Mariam and this is Alex, so to see the power of Sprinkle and Spring, the apa data already allows it. build software very quickly and so far we have everything working, so we have the get request which gives us a list of students and when a client sends a request, we have the service layer for the business logic, access to the data, we have the database.
I've been running and everything is set up beautifully now all we have to do is implement these three other http methods so post to save new resources intended to update the lead for the leader so if you have any questions please message me ; otherwise please contact me. the next one just before implementing the post method here which will allow us to save new students to our system. What I want to do is take care of h. Basically I don't want the age to be stored in our database because we can calculate. that based on date of birth so to do that, let's go ahead and open up intellij and here what I'm going to do is this, let me collapse this and this and then open up the student class and this field right here, so private and a whole age I'm going to annotate this with and then transient, so one thing is also to make sure that any package you import always uses java.persistence because that way if we switch from hibernate to another provider behind the scenes, everything will still work because will follow this implementation here, so from persistence, what this transient does is basically just say this field right here, it doesn't need to be a column in our database, which means the age will be calculated for us, so let's go ahead as well and remove the age from the constructors, so here the age disappeared, let's remove the age from here so that it disappears and from here too and we also forget about here, so here we go and here goes h. away too and now if I go back to our student configuration, here this doesn't work anymore, come on, so that's gone and also here that disappears and now we basically want to calculate this correctly, so we basically want to calculate the h for us and the way we do it is inside student so here where we say get and then age so what we're going to do is say period from time point java and then go in and I want to get the date so the start date , so this will be the date of birth or date of birth and then the local date and then the dot from time to time here I want to get and then the years, come on and now we have the number of years and also let me say this.dob, wow this. .dob and there you have it, so now let's go ahead and start the server, so let's get the server up and running and here remember that before we had two students and then the age was 2121 because this was actually coded with art, like this that now this should be real.
So if I refresh and you can see that now the age is 21 and fair is 17 and also in our database, if I describe the database you can see that age is no longer a column in our database and this is because a that here, if I collapse this, remember that we added add and then transient. Next, let's go ahead and implement our API's ability to save our client's students for our post request. We want to send this payload right here. to our server and we want to check if the email exists, if not we save the student in our database.
If the email is taken, we want to raise an exception, so let's go ahead and open up intellij and here inside the student. controller, you have seen this class and remember that we have implemented get mapping. Now let's implement the API which will take a payload and then store it in our system, so post is used when you want to add new resources to your system, in our case we want to add a new student, so let's go ahead and say public and then void and then I'm going to say register and a new and then student and this will take a student like this and now what we want to do is invoke the service point and then add new and then shouldn't and then pass the student inside now from here what I'm going to do is press option and then enter and I'm going to create this method inside the service class and there we go So at this point I'm not going to do anything because I want to go back here to the controller and for this to work we need do the following, so here this will be in and then post and then map and in.
We're not going to configure anything because this is more than enough and we want to take the student that comes from the client, so this json blob here we want to map it to this student taking it from the response or the request. request and then body so this is what it means so we take the request body and then we assign it to a student right here so now we are going to add the student so let's open the Add New Student option and for now let's just say south and then student so let's run the application again and inside intellij here you can see we can test so here we can test our API so I'm going to say open in the client here we go so this is will oppose localhost and then API view 8080 in students Oops, here we go and I really need an example.
Oh, actually, yeah, here we go, so this is an example with the payload, so here we want that, but instead what I want is this payload right here. that and this will make this request here, but for now we're not implemented, we haven't implemented this logic or this one, we're just logging in to see if we can send a request to our server, so here I'm going to run this request, so run , there we go, it seems to be done and let me open the console, run and check this, here we have the student id, the null name under the email bilal ahmed and then the date of birth and then the age equals no.
So this is awesome so we managed to take this payload and then send it to our server so here you can see I'm using intellij and you can also use for example postman or any risk client to perform this operation here. but the reason I like intellij is that everything is integrated, especially if you are using the definitive edition. Next, let's go ahead and grab this student from our service class and then check to see if the email exists; If not, we keep it, if not, we throw it away. an exception so now that we have the student here we can do some business logic and what we want first is to open the repository so here we have our student repository and we want to have a custom function here that will find a user by email , so doing it is very simple and here we can do this in two ways we can have an optional and this will be student and then we can build so let me import that first we can build the query based on method name so search and you can see I'm getting student auto completion and then send email and here I'm going to say string and then email so this is a form and this here will be transformed into a SQL like select and then start from and then student where email is equal to whatever we pass in so this is one way of doing things or we can annotate this with add and then query here and this we can say select and then lowercase s so this is an alias of and then student and then s where and then s dot email is equal to a question mark and then one, so I'm going to leave this here because you already know what's going on with this name, so again I can comment on this or I can be. more specific and say this is the query that I'm actually running and note that this is j, this is jpql and not a straight sql and this student here this is our class, our student class, so this name here , so when? we said in entity and then student so this is this and then we can access all the properties so age email etc so now let's go back to the service class and here we can do the following we can say repository . our student repository dot and then search by email and then student dot receive email and again we can do a lot of validation here, but I'm going to keep it super simple and here I'm going to say that we're going to try to find a student by that given email , then email student and let me put this on a new line like this and now I can tell if the email student is and then it is present, so if this is the case I want to cast, so for now let's just say cast a new constant exception and then illegal and I'm going to say email taken for example right so obviously there are better ways to handle exceptions and you should have your custom exceptions but this will work. and let me also rename this and then student and then optional, here we go, if the student is not present, we want to save our student and again you can check if the email is valid or not.
You can do a lot more complex validation on this, but I'm keeping things simple, so here now I'm going to say repository, then point student repository and then save and then we want to save student like this and there we go, so to Be honest, this is all you can do. Let's see that we have added a new method to our repository and we have some business logic next, let's go ahead and test this and see how it works next, let's go ahead and test this, so let me stop the server. Oh, actually, I'm here. this is the server and it's not running so give it a second we're going to be up and running now let me go back to this request here that we had before and what I'm going to do is I'm going to run this request so that this is to student and then with the same payload and then run you can see this post here was a status code 200 and here let's go ahead and try to find all the students you can see we have bilal so now it's in our database and id 3 here , so this is working, so if I try to send the exact same request, I'm going to send the exact same request, you can see we get a 500 status code here and the message is empty, so this is working. let's fix this message here to get the message from our exception that we just passed, we open application.properties and here at the bottom let's go ahead and say server and then dot error and we've included the message so this guy is right and we say always, so restart the server, there we go, so if I send the request again, remember that every time we restart the application we lose everything, so the first time we send this request, this will work, there we go.
Status code 200 so here 200 and if I send the request again you can see we now get a status code 500 but the message here is the one we added in our exception and it's a received email so there you have it . Let's see that we implemented the post functionality here very well, so we implement get and post here. Next, let's go ahead and implement the ability to delete students from our database, so let's implement the delete method here where we want to delete. a student determined by an id so open intellij and here let's open the controller to be the student controller and what we're going to do here is this: we're going to have another public method and then we're going to override delete and then student and right here this will be in and then it will remove the mapping and to remove what we want is inside, I'm going to say this has a path equal and then inside I want to pass the student and then the id, there we go like this, now inside we can take this student id by saying add and then path and then variable and reference it so student and then id like this and this will be long, so this is the type and oops, this will be an id like this and we can enforce that it's not null and whatnot, but for now let's keep it nice and simple, now let's go ahead and say student service point and then delete and then student and here we're going to pass the id, so whoops. id like this and then we're going to create that inside our student service and this will be the student id, in fact, let me come back here and this will be the student id like this so you can see how easy it is for us to take something out of the way, so this is one way. variable and you'll see this in a second, so now let's go back to the service class and here I want to say student repository dot and then check this, we have a bunch of methods, so here I want to find and then buy the id. pass through an id and then here I'm going to say dot or let me see if it exists, then the student repositorylet's say here the student repository point exists yes, we have an exists that returns a boolean value, so let's use this instead, so there we go, the student id and I'm going to say it exists, so if it doesn't exist, I want throw a new and then illegal state waiver student weight ID and then more ID, wow, I'll really shoot an ID and let me put this in. like so and then plus does not exist otherwise if this is the case I want to say student repository point and then delete and then here I want to delete by id and then by student quick id and to be honest this is it for this API let's call so let's restart the server here we go so here we're going to have basically we want to delete so we were placed so here we want to have a delete method so here let's take all of this and actually . we just need that and then here I guess we need to add the pound sign three times as well as paste that to change this from post to delete and then student and here I'm going to delete student one so if I update this we should have two students , maryam and alex, so I'm going to delete mariam and here if I run this and by the way, this is the path variable, right slash and then one and this here is what we capture inside this controller, so this go here now if I run this you can see we have a 200 status code which means the request went well and if I refresh this you can see mariam is gone so if I run the exact same request here we go y We receive an exception and the message is that the student with id1 does not exist.
There you have it. We have implemented this method right here and now we can remove students from our system. Finally, let's go ahead and implement the placement functionality. What I'm going to do is try to implement the functionality to be able to update both the name and the email, so put is used when you want to update a resource in your system. In our case, we want to update the name and email. For this exercise, what I'm going to do is have a method like this here, so update the student in your service class and then use this annotation on transactional, which you haven't learned, but using this annotation here means you don't have than implement no jpql query so you can use the setters of your returning entity to check whether or not you can update and then you can use the setters to automatically update the entity in your database again use the setters to update the entity when possible , so go ahead and try to write some business logic for this exercise and I will give you the solution in the next video.
Ok, let me go over the solution for location mapping where we want. to update the students so remember I asked you to update the name and email so I went ahead and added this method right here and here we have a path variable for the student id and you've already seen this but then we have a request parameter in here for name and email where both are not needed and then I pass it to the service with update student name and inside this student update I told it to use this annotation in transactional like this that here I am just doing some simple business logic, I am checking if the student exists with that id; otherwise we throw an exception and then if the name is not equal to null and the length of the name is greater than zero and the given name is not the same as the current one, then If that is not the case, we want to set the name like that and we do the same for the email, so it's exactly the same logic here, but we also check that the email here hasn't been received.
If received, we throw an exception. otherwise we update the student by just saying that you shouldn't set up email, so here you will see that we are not using any queries from our repository here, we are not using any queries and this is because of this annotation here in transactional, which when we have this annotation, the entity goes into a managed state and I cover all of this in my spring data jpa course so it is very important that you understand exactly how this annotation works but in a nutshell this is what is happening Now, let me go ahead and show you. you here we have mariam and alex right here and what I'm going to do is have a new request and this will be a request to v1 and then to student and then one that we want to update to mariam and we want to assign a new name called maria now if I send this request you can see it's 200 and now here it should be maria instead of merriam so if I refresh you can Look here we go so maria instead of mariam let's also refresh the email so here I'm just going to say than the new email now, so here I'm going to say and then email equals and let's say this time. it's just mary and then send it, let's go to status code 200 and if I refresh, you can see the email was updated, so there you have it like I said, if you want to learn more about Spring Data JPA, go ahead and check out my course, where I teach all this but basically the life cycle of the entity here where I mentioned that the entity went into a managed state so here the managed state is because of this so it is very important that you understand this to understand completely the concept and get the most out of spring daily jpi, so far you have seen the power of Spring Pool, it's amazing, so I just want to talk about testing because testing is something that you as a software engineer should know and when it comes to spraying. everything you know about external tdd mock testing services and basically putting all those things together, you can take all those skills and use them to basically test your applications properly, so in this course we haven't done any testing, but Testing is something you should do.
I know it inside out so I have a course for you on testing and I definitely recommend it because that will set you apart from other developers so I remember you know back in the day when I used to work with people who didn't do testing. your code and then deploy to production without any testing, but you know, if you want, if you are serious about it, then you have to know testing, so in my testing course you will learn what testing is. about junior framework assertions uh mocking you know why you should use dependency injection and you'll see you know when it comes to testing why it's so important so you'll learn about mocking like I said and I'm also going to show you the testing of integration correctly, so this is when you start your full application and then fire requests just to make sure everything works, so unit testing, integration testing, test-driven development, you'll learn about that in the course and then the most important thing is also how external services are tested, for example stripe.
I'm going to show you a use case for stripe where we don't want to connect it to your real systems for integration testing, so I'll show you how. to test against external services, so yeah, like I said, Springboard doesn't give you much in terms of testing, so if you want to do contract testing, they have spring contract testing, which is, I haven't actually used it. . but it is there but this is more for when you work with microservices but other than that you know testing is something that if you know it you can use it and test your applications, if you don't know then I highly recommend it.
I recommend you to enroll in my course where I teach you about testing. Now let's go ahead and learn how to take our API and produce a jar that we can then run multiple instances of our application, so let me go back to intellij and here what I want. What you need to do is the following, so if you see a target folder here, go ahead and delete it and once you delete it, open the maven tab and then here you can clean the project first to delete the target folder and actually, let me show you, so if I run this project there we go, you can see it, so let me stop it so you can see we have a destination folder.
Now this destination folder contains a couple of things, so it contains the classes. here I generate sources and basically that has not ended well so what I want to do is the following so let's stop the application from running and open maven and then do a cleanup so run the cleanup job to delete the destination you'll see in a second and there we go, that's done and you can see the destination is gone. Now let's install the application and make sure that your database is up and running, so install it and this will clean, validate, build the test package, verify and then inside we will have the jar file which we can then run manually, so give it a second so you can see that it is now running the tests.
To be honest, we don't have any proof, so there's nothing we can do. I'm done with testing and again if you want to learn about testing go ahead and check out my website where I have a complete course on testing and there we go so you can see we have a green check here and then we have the destination folder now inside from this destination folder so here if I expand this you can see we have a demo and then basically a snapshot and then a jar so this is what we're interested in now let me open up the terminal from here so open the terminal. and then you can see I'm inside the demo, so this folder here is this one and now I can navigate to the destination, then cd and then the destination and now we can run our application from here, so here I'm going to say java dash. and then jar and then the name of the jar, so demo and then I hit tab and you can see we have auto completion there.
Now I'm going to press Enter, I'll just give you a second and basically the records are too big. Here, let me see if I can zoom out, so I can't really zoom out, we can see we have a couple of things and basically the application is now working properly, which means I can navigate to localhost, so if I open my web browser and here this is the previous example, but now if I refresh, you can see that the app still works and we can try all the um post put delete, basically everything will work, so we've basically managed to get this right.
We have packaged our application in hr and now we have run an instance. If you want to run a different instance, it is completely up to you and you can run as many as you want, but you must specify the port here. because the port, so if I open it here, if I press Ctrl C to the right to close the application and if I refresh, you will see that now it doesn't work, but if you want to run, for example, the application on a different port. just run the same command and then you can say dash dash server dot and then port is equal to and then let's say 80 81 for example enter there we boot and run and then update this doesn't work but now here if I say 8081 and look this, you can see the app is now working, so there you have it.
Now you know how to package your application and then, from a jar, create an instance containing your application. Basically, from now on you can basically take whatever jar you want. You can deploy it on a server or you can dock it. You can do pretty much anything you want with your jar file to deploy it to real users. To use your app, if you have any questions, go ahead and leave my message; Otherwise, let's move on. True, it was a pleasure to teach you about Spring Boot. Let me know what you thought of this course and what you want to see next, so in the next few videos I plan to give you more

tutorial

s on Spring and the Springboard Framework, so probably the next video will be something along the lines of creating a complete logging service, where maybe we send an email and then you have to log into their system and then we'll also use some spring security stuff along the way, so, yeah, that's how it was. it's my pleasure to show you the spring book let me know what you think about this video and you know this is just the beginning and on my website I have courses on Spring Daily JPA Spring Full Stack um tests like I said so go ahead and check out those courses .
I'm going to leave a link in the description of this video because that way you will improve your skills and become a better developer, so knowing Spring Boot and Java quite well, there are many. There are jobs available and I can guarantee that with the courses I have for you you will not be disappointed and in fact I have many students who have benefited from these courses, some of them got jobs, others finished university. and I also got jobs, so yeah, go ahead and check out those courses, also join the community. I would love for you to join the community. um and yeah, that's all for now, thanks for watching assalamualaikum and I'll see you in the next one. see you

If you have any copyright issue, please Contact