Coding

Spring Boot Tutorial for Beginners (Java Framework)

  • 00:00:00 hi welcome to amigos code my name is
  • 00:00:02 Nelson and in this crash course I'm
  • 00:00:04 gonna teach you everything you need to
  • 00:00:06 know about spring boom spring boot it's
  • 00:00:08 an amazing framework for building java
  • 00:00:10 applications very fast recently Netflix
  • 00:00:14 just announced that they'll be switching
  • 00:00:16 their entire back-end to spring boo
  • 00:00:19 and this is because spring boo is so
  • 00:00:23 solid that there's no need for you to
  • 00:00:25 reinvent the wheel so they provide
  • 00:00:28 things such as connectivity to databases
  • 00:00:31 Jetley servers metrics configuration and
  • 00:00:35 pretty much instead of you having to
  • 00:00:37 configure the low level code to get up
  • 00:00:40 and running you can simply use spring
  • 00:00:43 book and get off the ground very quick
  • 00:00:45 so first I'm going to show you exactly
  • 00:00:47 how to use sprinkle dependency injection
  • 00:00:50 and how to implement to an interface
  • 00:00:53 where we will have an in-memory database
  • 00:00:55 and then I'm going to show you exactly
  • 00:00:57 how to connect to a real database
  • 00:00:59 running on docker
  • 00:01:02 so without further ado let's get started
  • 00:01:06 alright amigos
  • 00:01:08 welcome to amigos code in this video I
  • 00:01:11 want to teach you exactly how to use
  • 00:01:12 spring boat 2.0 spring boat is by far
  • 00:01:15 one of the best frameworks for building
  • 00:01:18 applications in the Java world so if you
  • 00:01:22 want to build an enterprise application
  • 00:01:23 you can use bring pool or if you want to
  • 00:01:26 prototype an idea and see whether you
  • 00:01:30 know it works that you can use spring
  • 00:01:32 book because it makes it very easy to
  • 00:01:34 build applications and basically spring
  • 00:01:37 book provides you a platform for
  • 00:01:39 building back-end applications and only
  • 00:01:42 that you can also build web applications
  • 00:01:44 as well so in this video let's go ahead
  • 00:01:47 and build this awesome API where any
  • 00:01:51 client can submit a request so these
  • 00:01:55 requests could be a get request post
  • 00:01:58 delete and put and then we're gonna take
  • 00:02:01 that request and within the API layer or
  • 00:02:05 control layer so this is where we
  • 00:02:08 receive the requests
  • 00:02:10 we're gonna send that to the service
  • 00:02:11 layer and
  • 00:02:13 the service layer is responsible to
  • 00:02:15 handle all the business logic that you
  • 00:02:17 may have within your application after
  • 00:02:21 you perform some business logic then you
  • 00:02:23 might decide to use a database to
  • 00:02:26 perform some various credit operations
  • 00:02:29 and basically so this layer right here
  • 00:02:32 so this layer is responsible to connect
  • 00:02:35 to any database of your choice so the
  • 00:02:38 cool thing about spring pool and
  • 00:02:40 dependency injection is that you can
  • 00:02:42 start with an implementation and then if
  • 00:02:45 you want to switch to a different
  • 00:02:46 database you simply have to change one
  • 00:02:49 line of code and I'm going to show you
  • 00:02:50 exactly how to do that with dependency
  • 00:02:54 injection so after you submit the actual
  • 00:02:59 request from any client so the clients
  • 00:03:01 could be an iOS app a react or even an
  • 00:03:05 android app the request will go through
  • 00:03:08 all these layers and then go back with a
  • 00:03:12 response to the client so the response
  • 00:03:15 could actually be for example a JSON
  • 00:03:17 payload or an image or a status code of
  • 00:03:21 200 500 so on and so forth so if you
  • 00:03:25 wanna learn the best framework for
  • 00:03:27 building Java applications stick to this
  • 00:03:29 video because there is a lot to cover
  • 00:03:31 and I'm pretty sure that after this
  • 00:03:33 video you will be very comfortable
  • 00:03:35 building back-end applications with
  • 00:03:38 springle so let's go ahead and get
  • 00:03:41 started so the very first thing that I
  • 00:03:43 want you to do is to navigate to this
  • 00:03:45 website start spring doe il and
  • 00:03:49 basically this website you can see that
  • 00:03:51 it's it's titled spring initializer it
  • 00:03:54 basically allows you to bootstrap your
  • 00:03:57 application by picking the penances and
  • 00:03:59 configuring the actual project so go
  • 00:04:02 ahead and generate a project with maven
  • 00:04:05 you can see that you can pick Gradle and
  • 00:04:07 then you have the option to code with
  • 00:04:11 Java code in all groovy so I'm gonna
  • 00:04:13 stick with Java and then for the actual
  • 00:04:15 version make sure that it is above 2.0
  • 00:04:19 so I've got a video on 1.5 and basically
  • 00:04:24 I just want to make sure that you guys
  • 00:04:26 have
  • 00:04:26 latest version so that you can use it
  • 00:04:29 within your latest projects so then go
  • 00:04:34 ahead and click on this link right here
  • 00:04:37 so switch to the full version basically
  • 00:04:39 right here you can you know configure
  • 00:04:41 your project metadata so go ahead and
  • 00:04:44 change these according to your company
  • 00:04:47 or project name but for this tutorial
  • 00:04:49 I'm gonna leave everything as is apart
  • 00:04:52 from the Java version so I'm gonna pick
  • 00:04:54 Java 11 because I do have it installed
  • 00:04:57 but if you don't have to have 11 and go
  • 00:04:59 ahead and pick Java 8 because it will
  • 00:05:01 also work then if you scroll down you
  • 00:05:05 can see that right here we have a bunch
  • 00:05:07 of dependencies and this is why spring
  • 00:05:10 bow is pretty much one of the best Java
  • 00:05:13 frameworks for building applications so
  • 00:05:15 they give you a lot of flexibility with
  • 00:05:19 these dependencies which simply means
  • 00:05:22 that you can install one of these
  • 00:05:23 dependencies and start coding right away
  • 00:05:27 instead of you having to configuring and
  • 00:05:29 you know setting them things so
  • 00:05:31 basically you just have to install these
  • 00:05:33 dependencies and then start implementing
  • 00:05:35 whatever you want so you can see that
  • 00:05:39 they have a bunch of sections right here
  • 00:05:41 so webs or servlet web applications with
  • 00:05:44 spring MVC and Tomcat and Jersey
  • 00:05:47 WebSockets
  • 00:05:48 template engines security so they make
  • 00:05:51 it very easy for you to configure
  • 00:05:53 security within your applications so you
  • 00:05:56 know it saves you the hassle of having
  • 00:05:58 to implement security the right way so
  • 00:06:00 basically they're doing the difficult
  • 00:06:02 job which is implementing the security
  • 00:06:04 and and then giving you this nice
  • 00:06:07 package that you can just plug and play
  • 00:06:09 and then you have SQL so my sequel h2
  • 00:06:13 JDBC Postgres fly away for database
  • 00:06:17 migrations Mongo and then you can scroll
  • 00:06:19 down you can see that they have a lot of
  • 00:06:21 great things so for this video right
  • 00:06:24 here let's go ahead and focus on picking
  • 00:06:28 this dependency right here so web server
  • 00:06:32 web applications with spring MVC and
  • 00:06:34 Tomcat so I'm gonna take that and then
  • 00:06:37 generate a project
  • 00:06:39 so right here you see that I've got this
  • 00:06:41 demo zip which I'm going to open in my
  • 00:06:43 desktop there we go
  • 00:06:46 so now I'm going to click on this zip
  • 00:06:50 folder and if I delete the actual zip
  • 00:06:56 you can see that now I have this folder
  • 00:06:59 right here so for this video I'm going
  • 00:07:01 to be using IntelliJ which is by far the
  • 00:07:03 best IDE and if you want to learn about
  • 00:07:06 IntelliJ go ahead and check my video on
  • 00:07:09 YouTube which I show you how to download
  • 00:07:11 install and configure IntelliJ properly
  • 00:07:14 and if you are using NetBeans or Eclipse
  • 00:07:17 this will also work so inside of
  • 00:07:21 IntelliJ so I actually recommend you to
  • 00:07:23 download and install IntelliJ if you
  • 00:07:25 don't have it to follow along and I'm
  • 00:07:27 pretty sure that you're gonna learn
  • 00:07:28 great things with IntelliJ throughout
  • 00:07:30 this video so what I'm gonna do now is
  • 00:07:34 pretty much click on open and then
  • 00:07:38 navigate to desktop and then open up
  • 00:07:42 this demo folder and then open up the
  • 00:07:44 actual palm dot XML so I'm going to open
  • 00:07:47 that up and then open ass project so I'm
  • 00:07:52 gonna allow and just give a second maven
  • 00:07:57 is resolving the dependencies for this
  • 00:07:59 project there we go so that's nice and
  • 00:08:03 done so let's go ahead and explore this
  • 00:08:06 project so open up demo and inside demo
  • 00:08:10 you can see that I have a couple of
  • 00:08:11 folders but the ones that we really care
  • 00:08:13 about are the palm dot XML so if I open
  • 00:08:16 that up and see that this is what you
  • 00:08:20 saw when configuring the project with
  • 00:08:23 spring initializer so you saw the group
  • 00:08:26 ID artifact name and then version so
  • 00:08:32 this is a version right here and then
  • 00:08:34 this is Java 11 basically the actual
  • 00:08:38 dependency that we picked was this one
  • 00:08:40 right here so spring boat starts a web
  • 00:08:42 and by default
  • 00:08:44 springboard ships with spring ball
  • 00:08:46 started tests for writing unit and
  • 00:08:49 integration tests
  • 00:08:51 so go ahead and close this because we
  • 00:08:54 don't need it and open up the source
  • 00:08:58 folder so open up sauce and then main
  • 00:09:01 and you can see that you have Java and
  • 00:09:04 then inside you should have a class
  • 00:09:09 called demo application and inside of
  • 00:09:12 this class you have this public static
  • 00:09:16 void main method and this is how you
  • 00:09:18 pretty much just run your spring
  • 00:09:21 application and if I play so make sure
  • 00:09:24 you play this just give you a second you
  • 00:09:29 should see that I have spring up and
  • 00:09:34 running and you can see that it says
  • 00:09:36 that Tomcat starts it on port 8080 so
  • 00:09:39 right here we could change this port but
  • 00:09:40 this is the default port for Tomcat so
  • 00:09:44 I'm gonna stop this and you can see that
  • 00:09:46 everything is up and running fine so I'm
  • 00:09:49 gonna stop this and then collapse this
  • 00:09:52 and then inside of resources you have
  • 00:09:55 static templates and applications or
  • 00:09:58 properties so static is where all the
  • 00:10:02 web resources live for web applications
  • 00:10:06 and then you have templates and this is
  • 00:10:08 where you have all the templates for
  • 00:10:10 your web application so usually you
  • 00:10:13 would use something like most – or ejs
  • 00:10:15 and then you have this application that
  • 00:10:18 properties so this is where all the
  • 00:10:19 properties for your application do live
  • 00:10:22 and the cool thing about properties is
  • 00:10:25 that you can define properties for
  • 00:10:27 different environments so you could have
  • 00:10:29 a demo environment a test environment
  • 00:10:32 and a production environment with
  • 00:10:34 different values for the actual
  • 00:10:36 properties so it's pretty cool so I'm
  • 00:10:39 gonna cancel out of that and now that
  • 00:10:43 you know the actual structure and make
  • 00:10:46 sure that you can start the application
  • 00:10:48 you've successfully managed to start a
  • 00:10:51 tomcat server with spring book so now
  • 00:10:54 let's go ahead and implement our API
  • 00:10:58 with all of those resources controllers
  • 00:11:02 services and defining a date
  • 00:11:05 bass as well so if I go back to this
  • 00:11:09 diagram so remember that I said we had
  • 00:11:13 the API layer controller or controller
  • 00:11:18 layer and then we have a service layer
  • 00:11:21 for business logic and then database for
  • 00:11:23 data access so let's go ahead and start
  • 00:11:27 things right here so we're gonna start
  • 00:11:30 right here and in fact just let me put a
  • 00:11:33 dot right here so you know what we're
  • 00:11:35 doing so if I remove that basically
  • 00:11:39 let's put just a dot so we know where we
  • 00:11:42 are with things so right here and then
  • 00:11:48 if I collapse that so basically we're
  • 00:11:52 going to implement this section first
  • 00:11:54 and then move our way up so basically
  • 00:11:57 we're going to implement one step at a
  • 00:12:00 time and then see everything working
  • 00:12:04 fine and then implement some other
  • 00:12:06 functionality so let's go ahead and go
  • 00:12:10 back to IntelliJ and the first thing
  • 00:12:13 that I want you to do is to create a
  • 00:12:18 package so let's go ahead and create a
  • 00:12:20 package and right here simply call it
  • 00:12:23 API so this is where the API will live
  • 00:12:27 go ahead and create a second package and
  • 00:12:30 simply save the model go ahead and
  • 00:12:34 create a third package call it service
  • 00:12:37 go ahead and create a fifth package or
  • 00:12:41 actually a fourth package and call it so
  • 00:12:44 we need the actual Dao and I think this
  • 00:12:50 is it so we have API Dao model and
  • 00:12:55 service and basically these packages are
  • 00:12:58 representing this diagram so if I go
  • 00:13:03 back and perhaps you haven't seen me
  • 00:13:08 like right clicking so new and then a
  • 00:13:10 package so basically I tend to use a lot
  • 00:13:14 of keyboard shortcuts and this is how I
  • 00:13:16 am productive with IntelliJ
  • 00:13:19 so if I press ctrl and then enter you
  • 00:13:22 can see the keyboard shortcut down below
  • 00:13:24 and basically then that gives me the
  • 00:13:27 ability to create a new package so the
  • 00:13:30 same if I for example create a variable
  • 00:13:34 so if I say var and then ello equals to
  • 00:13:41 two and and then if I press command and
  • 00:13:46 then Y you can see the keyboard down
  • 00:13:49 below right so I've just deleted that
  • 00:13:51 line so also let me go ahead and say
  • 00:13:53 okay so now what we need to do is
  • 00:13:57 actually define our model so what is the
  • 00:14:00 actual model for this application so if
  • 00:14:03 I go back to this diagram so let's go
  • 00:14:06 ahead and define a model so I'm gonna
  • 00:14:08 copy this stereotype and you can grab
  • 00:14:11 this diagram you can find a link in the
  • 00:14:14 description below where you can download
  • 00:14:15 and see exactly you know everything that
  • 00:14:18 we're doing here so basically the actual
  • 00:14:21 model so loops so the model for this
  • 00:14:25 tutorial will be a person and basically
  • 00:14:28 a person will have an ID so an ID and
  • 00:14:32 let's keep things simple and have like
  • 00:14:35 let's say the full Ashley name just like
  • 00:14:40 that so obviously you could have more
  • 00:14:42 properties but I want to keep it simple
  • 00:14:44 so you see exactly what's happening and
  • 00:14:47 so things you know become really obvious
  • 00:14:50 to you so we have this model right here
  • 00:14:53 called person let's go ahead and define
  • 00:14:55 this in actual code so go back to
  • 00:14:58 IntelliJ and inside of the model package
  • 00:15:01 go ahead and create in your class and
  • 00:15:04 simply call this person right so the
  • 00:15:08 kind it's a class and then press Enter
  • 00:15:11 so now simply go ahead and say private
  • 00:15:14 final and then the ID will be of type
  • 00:15:17 you whit and simply call the ID go ahead
  • 00:15:22 and give us a private final and then
  • 00:15:25 string so this is the actual name
  • 00:15:28 then I'm gonna add these two
  • 00:15:30 constructors so you can see I'm using
  • 00:15:31 keyboard shortcuts again and then the
  • 00:15:35 getters so just like that and there you
  • 00:15:38 go so now we have a model that we can
  • 00:15:41 work with so if I go back so now we have
  • 00:15:46 to define the actual database section
  • 00:15:49 right here right so let's define the
  • 00:15:51 actual interface that will allow us to
  • 00:15:54 have any implementation for our database
  • 00:15:57 so inside of the DAO go ahead and create
  • 00:16:01 a class and then simply say person and
  • 00:16:05 then dao so instead of a class change
  • 00:16:09 this to an interface and this is the
  • 00:16:12 actual interface where we're going to
  • 00:16:14 define the operations allowed or the
  • 00:16:18 actual contract for anyone that wishes
  • 00:16:20 to implement this interface and the cool
  • 00:16:23 thing about it is that we can then use
  • 00:16:26 the penisy injection to switch between
  • 00:16:28 implementations with just one line of
  • 00:16:31 code so let's go ahead and define our
  • 00:16:34 very first method so for our database we
  • 00:16:37 want to insert a person into the actual
  • 00:16:40 database so we will simply return an
  • 00:16:44 integer and basically we're gonna mock a
  • 00:16:47 database using like a list basically we
  • 00:16:50 will return 0 or 1 depending whether the
  • 00:16:53 actual data is persisted to the actual
  • 00:16:58 database so let's simply go ahead and
  • 00:17:00 say insert and then person and this will
  • 00:17:05 take au it which is the actual ID and
  • 00:17:09 then a person just like that and then
  • 00:17:13 end up with semicolon now let's also go
  • 00:17:16 ahead and create a default method and
  • 00:17:20 basically this will be the same thing
  • 00:17:22 actually sorry this will be integer so
  • 00:17:25 it will be the same thing and let's
  • 00:17:28 simply call this ad and then person and
  • 00:17:31 right here we will simply take the
  • 00:17:33 person right and what this allows us to
  • 00:17:37 do is to actually generate the UITs
  • 00:17:40 ourself or there
  • 00:17:42 show ID so you it and then let's call
  • 00:17:45 the ID equals to and then you it dot and
  • 00:17:49 then run them you it and then simply say
  • 00:17:52 return and then insert person passing
  • 00:17:57 the ID and then the actual person yep so
  • 00:18:01 basically we have two methods one that
  • 00:18:04 allows us to insert a person with a
  • 00:18:06 given ID and the other one without an ID
  • 00:18:09 and the idea is simply randomly
  • 00:18:13 generated now let's go ahead and create
  • 00:18:16 a class that will implement this
  • 00:18:18 interface so open up the DAO package and
  • 00:18:22 then simply create a class and call it
  • 00:18:25 person and call it fake and then person
  • 00:18:29 data access and then service and then
  • 00:18:36 this guy implements and then person Dao
  • 00:18:40 just like that and then implement the
  • 00:18:43 methods and we need to implement insert
  • 00:18:46 person right so right here let's go
  • 00:18:52 ahead and define our list so private and
  • 00:18:56 then static and then and then list and
  • 00:19:00 this will be a list of person simply
  • 00:19:04 call it a be equals to a new and then a
  • 00:19:09 red list just like that or in fact we
  • 00:19:13 could even initialize this inside of a
  • 00:19:15 static block if we want it so now to
  • 00:19:19 answer a person all we have to do is
  • 00:19:22 simply add to this database right here
  • 00:19:26 so for the sake of this simply go ahead
  • 00:19:29 and say DB dot and then add and then new
  • 00:19:34 and then person and I simply take the
  • 00:19:37 actual ID and then simply say person
  • 00:19:40 don't and then get name right so and
  • 00:19:44 finally let's simply go ahead and return
  • 00:19:46 one so we know that it always works so
  • 00:19:49 the insertion always works so now let's
  • 00:19:53 go ahead and actually define the
  • 00:19:55 to service so inside of the service
  • 00:19:57 package go ahead and create a new class
  • 00:19:59 and then simply call it person and then
  • 00:20:02 service and right here inside of this
  • 00:20:05 class let's say we have a method to
  • 00:20:08 insert a new person so simply go ahead
  • 00:20:12 and say public and then int and then and
  • 00:20:16 and then person and basically this will
  • 00:20:19 take a person and what we want to do is
  • 00:20:24 simply say return and right here we need
  • 00:20:27 to actually get a reference of the
  • 00:20:30 actual person Dao so let's go ahead and
  • 00:20:33 simply say private and then final and
  • 00:20:36 then person Dao
  • 00:20:38 and make sure that you get the actual
  • 00:20:40 interface and not the concrete class so
  • 00:20:44 don't get the actual fake person data
  • 00:20:46 access service because I want to show
  • 00:20:48 you exactly how to use the penance
  • 00:20:50 injection so then what we need to do is
  • 00:20:54 add this to the constructor and now we
  • 00:20:56 can simply say person Dao don't and then
  • 00:21:00 insert and then person so remember we
  • 00:21:04 had two methods and if I go back to my
  • 00:21:06 interface so person ta oh and in fact I
  • 00:21:11 need to call this the same so let's be
  • 00:21:13 consistent so person so insert person
  • 00:21:15 actually so now you can see that the
  • 00:21:17 error went away so basically we have the
  • 00:21:19 option of providing the ID or not so in
  • 00:21:23 this case let's go ahead and have it you
  • 00:21:25 know randomly generated because we don't
  • 00:21:28 care but if you need to generate the ID
  • 00:21:30 your self you know that you can do it so
  • 00:21:34 now that we have the service done let's
  • 00:21:37 go ahead and implement the actual API so
  • 00:21:39 inside of the API package go ahead and
  • 00:21:42 create class and simply call it person
  • 00:21:45 and then resource oh actually let's go
  • 00:21:48 ahead and say controller so in Springbok
  • 00:21:51 world we tend to name these as
  • 00:21:53 controllers so and then simply say okay
  • 00:21:56 and then let's go ahead and also have a
  • 00:22:00 reference to the actual service so
  • 00:22:02 private final and in-person service
  • 00:22:06 and then right here we need to add this
  • 00:22:10 to the constructor and then let's simply
  • 00:22:13 go ahead and have a method that adds a
  • 00:22:15 person so public and then simply say
  • 00:22:19 void so we don't want to return an
  • 00:22:21 integer in this case so we're going to
  • 00:22:23 so we want to return a status code of
  • 00:22:26 200 or if anything fails we can pretty
  • 00:22:29 much throw an exception so or you could
  • 00:22:32 also have your custom exceptions if you
  • 00:22:35 wish so let's go ahead and simply say
  • 00:22:37 add and then person and this will take a
  • 00:22:41 person so a person just like that and
  • 00:22:44 then what we need to do is actually say
  • 00:22:46 person service dot and then add a person
  • 00:22:50 just like that so up to this point we
  • 00:22:54 simply have normal Java classes and to
  • 00:22:58 be honest we're not even using Spring
  • 00:23:00 Framework at this point so let's go
  • 00:23:04 ahead and configure everything to start
  • 00:23:05 using Spring Framework so let's go ahead
  • 00:23:09 and open up the actual Dao so fake
  • 00:23:13 person data access service and you see
  • 00:23:17 right here inside of the actual service
  • 00:23:20 we have a reference to it so person Dao
  • 00:23:23 and basically we are trying to use the
  • 00:23:26 penance injection so if I was to run
  • 00:23:28 this this would wouldn't work because
  • 00:23:32 this person do is is not instantiated
  • 00:23:35 and you know there's no way for this
  • 00:23:40 constructor to know about it right so
  • 00:23:42 this is where we use annotations to
  • 00:23:45 instantiate beans so open up the fake
  • 00:23:49 person data access service and the way
  • 00:23:52 that we tell spring that this class
  • 00:23:55 needs to be instantiated as a bean so
  • 00:23:59 that we can inject it in other classes
  • 00:24:01 it simply by saying act and then
  • 00:24:03 repository right so you could also say
  • 00:24:07 add and then component so this is the
  • 00:24:09 exact same thing but repository makes it
  • 00:24:13 obvious that this class is served as a
  • 00:24:16 repository right so now that we have a
  • 00:24:20 the tree let's go ahead and configure
  • 00:24:22 the actual service so for service you
  • 00:24:25 might take a guess so this will be act
  • 00:24:27 and then service right and you could
  • 00:24:30 also use at and then component if you
  • 00:24:32 wish but I usually prefer to be you know
  • 00:24:35 specific so anyone looking at my code
  • 00:24:37 know exactly that this is a service so
  • 00:24:41 now that we pretty much annotated the
  • 00:24:46 actual data access service and right
  • 00:24:49 here we are injecting right so we are
  • 00:24:52 injecting into the actual constructor so
  • 00:24:55 the way that we inject is simply saying
  • 00:24:58 at and then Auto wired so basically we
  • 00:25:02 are auto wiring into this interface
  • 00:25:06 right so now because we can have
  • 00:25:09 multiple implementations of this
  • 00:25:12 interface right here we must have a way
  • 00:25:14 to distinguish between them right and
  • 00:25:17 this is when you use at and then
  • 00:25:19 qualifier right so our qualifier and
  • 00:25:23 then right here you can give it a name
  • 00:25:25 so fake and then Dao and basically I
  • 00:25:29 need to go back to my fake person data
  • 00:25:31 access service and then right here I'm
  • 00:25:34 going to name this as fake and then Dao
  • 00:25:37 and what this allows me to do is to have
  • 00:25:40 multiple implementations and basically
  • 00:25:44 all I need to do is for example if I
  • 00:25:46 have for example a Postgres or MongoDB
  • 00:25:49 implementation I simply say Mongo
  • 00:25:53 just like that and then I don't have to
  • 00:25:55 change anything so let's go ahead and
  • 00:25:58 use the fake Dao and now this is why it
  • 00:26:02 correctly so finally let's go ahead and
  • 00:26:05 open up the actual API package and then
  • 00:26:08 person controller let's also go ahead
  • 00:26:11 and simply say at and then order wired
  • 00:26:14 and basically Springvale injects the
  • 00:26:16 actual service into this constructor and
  • 00:26:20 if you want to learn more about
  • 00:26:21 dependency injection go ahead and check
  • 00:26:23 my channel I made a video specific on
  • 00:26:26 the police injection and why it's
  • 00:26:28 important for you to know about it so
  • 00:26:31 now
  • 00:26:33 remember that I said that this class
  • 00:26:35 right here so the API if I go back to
  • 00:26:37 our diagram is this one right here
  • 00:26:41 and right here we can specify HTTP
  • 00:26:44 methods so get post delete and put so
  • 00:26:49 these clients right here right so they
  • 00:26:51 have to issue some requests to our
  • 00:26:54 back-end so right now there is no code
  • 00:26:58 that tells that this method right here
  • 00:27:00 will be served as a post or put or get
  • 00:27:05 or delete and also this class is not
  • 00:27:09 available as a rest controller so to do
  • 00:27:13 that we need to use a special annotation
  • 00:27:15 and simply say add and then rest
  • 00:27:17 controller right so this is a rest
  • 00:27:20 controller class that we can have some
  • 00:27:22 methods and then expose some endpoints
  • 00:27:25 that clients can consume so in our case
  • 00:27:28 what we want to do is have this method
  • 00:27:33 right here to be served as a post
  • 00:27:37 request so basically gets is for
  • 00:27:41 retrieving data from your server posts
  • 00:27:44 is to adding a resource to your server
  • 00:27:47 put modifying and then delete for
  • 00:27:50 deleting a resource a resource from your
  • 00:27:53 server so to tell spring that this
  • 00:27:58 method will be used as a post request we
  • 00:28:01 simply use the annotation at and then
  • 00:28:04 post and then mapping and this is all we
  • 00:28:08 have to do now so obviously we want to
  • 00:28:14 add a person from my client right so go
  • 00:28:18 ahead and download postman so postman
  • 00:28:20 allows you to pretty much just serve as
  • 00:28:24 a client because we don't have for
  • 00:28:26 example a react application or an
  • 00:28:28 Android app or you know you we don't
  • 00:28:31 have any any any client right so we can
  • 00:28:34 use postman as our client right so go
  • 00:28:40 ahead and download postman it's an
  • 00:28:41 amazing client and what we need to do is
  • 00:28:44 actually
  • 00:28:45 send a person to our server so before we
  • 00:28:50 do that so let me actually clear
  • 00:28:52 everything here and basically go ahead
  • 00:28:56 and say post and then this will be local
  • 00:29:00 host colum and then 8080 and then for
  • 00:29:04 slash and then simply say api ford slash
  • 00:29:07 and then v1 and then Patterson right so
  • 00:29:12 after you define the actual end point go
  • 00:29:15 ahead to the actual body and then simply
  • 00:29:19 say raw and then instead of text go
  • 00:29:23 ahead and send a JSON so we want to send
  • 00:29:26 a JSON payload to the actual server and
  • 00:29:29 remember we have name and let's say that
  • 00:29:33 this is James and then bond so basically
  • 00:29:36 we want to send James Bond to the server
  • 00:29:39 so in order for us to do that we have to
  • 00:29:43 do few things one is we have to define
  • 00:29:46 these properties in our Pocho so that
  • 00:29:51 when we send this JSON Spring knows to
  • 00:29:55 take this property and then convert that
  • 00:29:58 into a Java class right so in order for
  • 00:30:02 us to do that what we need to do is open
  • 00:30:04 up person so inside of the model package
  • 00:30:07 open a person and then you see right
  • 00:30:11 here in the constructor let's go ahead
  • 00:30:13 and define some properties so I JSON
  • 00:30:17 property and this will be the ID and
  • 00:30:20 then right here so if I put this on the
  • 00:30:23 line at and then Jason property and this
  • 00:30:27 will be the actual name right so you
  • 00:30:30 could also define more properties but as
  • 00:30:32 I said let's keep this simple so that
  • 00:30:35 you understand exactly what's going on
  • 00:30:37 so if I open a postman again so right
  • 00:30:42 here you see that name so this name
  • 00:30:45 right here matches the property that we
  • 00:30:49 have in our class person and if we
  • 00:30:52 wanted to send the ID so we will simply
  • 00:30:54 say ID
  • 00:30:57 and then pass you know a an ID but we
  • 00:31:01 are generating the idea from the server
  • 00:31:03 therefore we don't need to send it now
  • 00:31:06 what I need to do is actually tell that
  • 00:31:11 we are receiving this JSON payload from
  • 00:31:16 the actual body so inside of person
  • 00:31:19 controller go ahead and you see right
  • 00:31:22 here where you add a person so right
  • 00:31:24 here simply annotate this with at and
  • 00:31:27 then request and then body right so we
  • 00:31:32 are simply saying that we want to take
  • 00:31:34 the request body and then shovel that
  • 00:31:37 inside of this person and then because
  • 00:31:40 we have so right here so because we have
  • 00:31:44 the this property right here we will
  • 00:31:47 simply turn this JSON object into a
  • 00:31:50 person that's what we are saying and the
  • 00:31:53 final thing that we have to do is
  • 00:31:54 actually define this path right here
  • 00:31:57 right so right here before rest
  • 00:32:01 controller simply say at and then
  • 00:32:03 request and then mapping and the actual
  • 00:32:09 mapping will be API v1 and then person
  • 00:32:13 so this is all you need to do in order
  • 00:32:16 to create your very first endpoint wait
  • 00:32:19 Springville now what we need to do is
  • 00:32:21 actually test it so let's go ahead and
  • 00:32:24 simply play the application so as you
  • 00:32:31 can see Tomcat started on port 8080 now
  • 00:32:35 if I open up postman and then simply
  • 00:32:40 send this request right here you can see
  • 00:32:45 that we have a 200 status code and this
  • 00:32:49 means that the request went through fine
  • 00:32:53 now obviously we don't have a way of
  • 00:32:57 testing this right because we don't have
  • 00:33:00 an endpoint to retrieve people from our
  • 00:33:02 database so let's go ahead and define
  • 00:33:04 the functionality to get people from our
  • 00:33:08 database so let me
  • 00:33:10 go ahead and open it IntelliJ and if I
  • 00:33:14 collapse that so go ahead and open up
  • 00:33:17 the person Dao and let's go ahead and
  • 00:33:21 say that this will return a list of
  • 00:33:24 person and say select all people and
  • 00:33:29 then let's go ahead and open up the
  • 00:33:33 person data access service so we need to
  • 00:33:37 implement that and then right here what
  • 00:33:40 we need to do is very simple return the
  • 00:33:42 actual database and open up service so
  • 00:33:47 right here simply create a method public
  • 00:33:50 and then list all person get and then
  • 00:33:58 all people and then this will return
  • 00:34:05 person Dao dot and then select all
  • 00:34:08 people so now we need to define the
  • 00:34:11 actual resource or the actual method
  • 00:34:15 that will serve as the get request so
  • 00:34:18 simply say public and then a list of an
  • 00:34:24 in person and then get all people and
  • 00:34:30 then what we're going to return is the
  • 00:34:32 actual person service dot and then get
  • 00:34:37 all people right so now we need to tell
  • 00:34:40 that this method will be served as a get
  • 00:34:43 request and the way that you do it is
  • 00:34:46 simply by saying act and then get and
  • 00:34:48 then mapping so you see that everything
  • 00:34:51 has the same pattern at post mapping at
  • 00:34:54 get mapping now let me go ahead and
  • 00:34:57 pretty much just restart the server
  • 00:35:04 there we go
  • 00:35:06 so the actual server started on port
  • 00:35:10 8080 so open a postman
  • 00:35:15 and then let's actually try and send the
  • 00:35:18 actual same request so we're gonna send
  • 00:35:21 James Bond you can see that worked and
  • 00:35:24 now let's go ahead and try and perform a
  • 00:35:28 get request to our server so the exact
  • 00:35:31 same URL send and there we go so you can
  • 00:35:35 see that we have an array with one
  • 00:35:37 person that comes from our database so
  • 00:35:40 this is awesome
  • 00:35:42 now let's go ahead and perform another
  • 00:35:44 post so so right here so change this to
  • 00:35:49 Nelson and then Mandela you can change
  • 00:35:53 this to whatever name you want to be
  • 00:35:54 honest I'm gonna send that you can see
  • 00:35:57 that it's 200 and then simply change
  • 00:36:01 this to Anna and then Jones send and now
  • 00:36:07 if I perform a get request to our server
  • 00:36:10 you can see that we have three people so
  • 00:36:14 James Bond Nelson and Anna Jones so as
  • 00:36:18 you see Springville is just incredible
  • 00:36:21 it's super fun and as you saw like it
  • 00:36:25 didn't take us long to implement this
  • 00:36:28 functionality right we're just using
  • 00:36:30 annotations to drive our implementation
  • 00:36:33 which is awesome and we can pretty much
  • 00:36:35 deliver results very fast using spring
  • 00:36:39 boom now let's go ahead and implement
  • 00:36:41 the other two HTTP methods the ability
  • 00:36:45 to update a person as well as deleting a
  • 00:36:48 person so open up IntelliJ if I collapse
  • 00:36:52 this and then open up the actual Dao and
  • 00:36:56 let's define the actual interface
  • 00:36:58 marshal method
  • 00:37:00 so right here simply say int and delete
  • 00:37:04 and then person by ID and right here we
  • 00:37:08 need to pass the actual ID so you it and
  • 00:37:11 an ID and let's also define a couple
  • 00:37:15 things so let's also define that we want
  • 00:37:17 to update a person so this will return
  • 00:37:21 an integer and then simply say update
  • 00:37:25 and then person by ID so right here
  • 00:37:29 we're going to pass the actual ID and
  • 00:37:33 the actual new person that we want to
  • 00:37:36 update right
  • 00:37:37 so person and also let's define one more
  • 00:37:43 method and this will be an optional of
  • 00:37:46 and then person and then simply say
  • 00:37:51 select person by and then ID and then
  • 00:37:56 simply pass the actual ID inside and
  • 00:37:59 there we go so now let's go ahead and
  • 00:38:02 open up the fake person data access
  • 00:38:05 service and we have to implement all of
  • 00:38:07 those methods so I'm pressing alt and
  • 00:38:09 then enter and then implement methods
  • 00:38:12 and you can see that we have these three
  • 00:38:14 methods that we need to implement okay
  • 00:38:17 and errors gone so in fact let's go
  • 00:38:20 ahead and implement select person by ID
  • 00:38:23 first so to implement this method what
  • 00:38:27 we need to do is actually search within
  • 00:38:30 our database and see whether we have a
  • 00:38:32 person with the given ID so this one
  • 00:38:35 right here so the way we do it is simply
  • 00:38:37 by streaming our database so let's go
  • 00:38:40 ahead and use Java streams dot and then
  • 00:38:43 stream and if we learn more of our
  • 00:38:46 streams go ahead and check my video on
  • 00:38:48 Java streams where I cover this so let's
  • 00:38:53 go ahead and simply say filter and right
  • 00:38:55 here we get a person and then let's
  • 00:38:58 simply say that person dot and then get
  • 00:39:01 ID dot equals to ID right and then what
  • 00:39:06 we want is dot and then find and then
  • 00:39:10 first and there we go so now let's go
  • 00:39:14 ahead and open up the actual service and
  • 00:39:16 then simply implement the same method
  • 00:39:19 here so public and then this will be an
  • 00:39:23 optional and then person and then get
  • 00:39:28 person by ID
  • 00:39:32 and then simply pass the on show ID and
  • 00:39:36 you can see that it's almost the same
  • 00:39:37 thing that we're doing right here return
  • 00:39:40 and then person Dao don't and then get
  • 00:39:44 or actually select person by ID and then
  • 00:39:49 pass the ID and then open up the
  • 00:39:52 controller so person controller and this
  • 00:39:56 will be a yet so basically we want to
  • 00:40:01 public so this will be a person and then
  • 00:40:06 get and then person by ID and then we're
  • 00:40:10 going to pass the ID and I'm going to
  • 00:40:12 show you exactly how do we get that
  • 00:40:14 so ID and then what we want to do is
  • 00:40:17 simply say return and then person
  • 00:40:20 service dot get person by ID lowercase
  • 00:40:26 ID and then dot or else and then no so
  • 00:40:31 right here you could actually have a
  • 00:40:33 custom message or a custom exception or
  • 00:40:37 a custom code thrown to the actual
  • 00:40:39 client so for example if for for we the
  • 00:40:42 message saying users are not found but
  • 00:40:45 for this video let's go ahead and keep
  • 00:40:47 it simple and simply return no right so
  • 00:40:51 now what we want to do is if I open a
  • 00:40:53 postman is you see this ID right here
  • 00:40:56 right so I want to be able to copy the
  • 00:41:00 ID and then simply say fort slash and
  • 00:41:03 then pass the ID and then when I send
  • 00:41:07 the request I should get that person
  • 00:41:09 back right so the way that we get the ID
  • 00:41:13 from the path is by simply using an
  • 00:41:17 annotation and the annotation is called
  • 00:41:20 so this one is at and then path and then
  • 00:41:24 variable and a path variable that we
  • 00:41:28 want is an ID so ID just like that and
  • 00:41:33 basically this is a type right here so
  • 00:41:37 now let's go ahead and define the actual
  • 00:41:39 mapping so this will be act and then get
  • 00:41:43 and then mapping and now because we want
  • 00:41:47 to have the actual ID in the path
  • 00:41:50 so inside simply say path and then what
  • 00:41:54 you want is double quotes and then curly
  • 00:41:58 brackets and then pass ID so basically
  • 00:42:01 this will allow you to have four slash
  • 00:42:03 and then some ID and then right here
  • 00:42:07 you're simply grabbing the ID and then
  • 00:42:10 turning that into a u it right so let's
  • 00:42:14 go ahead and test this so if I pretty
  • 00:42:17 much just restart the actual server
  • 00:42:24 there we go I've been running so if I
  • 00:42:27 open up postman
  • 00:42:29 now we need to issue some requests so
  • 00:42:34 you can see that you know every time
  • 00:42:35 that we will start the server we lose
  • 00:42:37 everything from our list that's a
  • 00:42:40 disadvantage of having an in-memory
  • 00:42:41 database but for this video you can see
  • 00:42:44 exactly how everything works so let's go
  • 00:42:47 ahead and send a post request there and
  • 00:42:50 then let's get everyone so remove the
  • 00:42:55 actual u it there so send so we have
  • 00:43:00 Nelson right there and basically now we
  • 00:43:03 can grab Nelson's ID and then do a
  • 00:43:09 forward slash and then paste that right
  • 00:43:12 so now if I send you can see that we
  • 00:43:16 have only one object right it so it's
  • 00:43:20 not an array but it's an object and if I
  • 00:43:23 also change the ID for something that
  • 00:43:27 doesn't exist send you can see that
  • 00:43:30 actually it's a bad request because the
  • 00:43:33 you wait it's completely wrong so it's
  • 00:43:36 not valid you with that I've just sent
  • 00:43:38 to the server but you get the idea so if
  • 00:43:41 I go ahead and let's go ahead and add
  • 00:43:43 and a Jones so send and then let's get
  • 00:43:49 everyone and let's grab Anna Jones ID
  • 00:43:52 right there
  • 00:43:54 and then forth / send that you can see
  • 00:44:00 that works right and in fact let me go
  • 00:44:03 ahead and say you with generator and let
  • 00:44:08 me generate you it so I want to one you
  • 00:44:11 with here and if I grab this and then go
  • 00:44:15 back to postman and then perform a get
  • 00:44:18 request wait that you were there so if I
  • 00:44:23 delete everything and then paste I do it
  • 00:44:27 and then send you see that it's a 200
  • 00:44:35 but we have nothing in our database
  • 00:44:37 without you it right so as I said so you
  • 00:44:41 should be thrown in a 404 with a custom
  • 00:44:44 exception but in this video you can see
  • 00:44:47 exactly that we are returning nothing
  • 00:44:49 because there there is no one without
  • 00:44:51 you it against our database so let's go
  • 00:44:54 ahead and implement the remaining
  • 00:44:56 methods so if I collapse this so let's
  • 00:45:00 go ahead and implement everything in one
  • 00:45:02 go now so open up the actual fake person
  • 00:45:05 that access service and to delete a
  • 00:45:08 person what we need to do is simply get
  • 00:45:13 the person if the person is there the
  • 00:45:16 leader otherwise return 0 so let's go
  • 00:45:20 ahead and simply say select and then
  • 00:45:24 person by ID
  • 00:45:25 we're gonna grab the ID and remember
  • 00:45:29 this returns an optional so person and
  • 00:45:31 then maybe and right here I'll simply
  • 00:45:35 say if and then person maybe dot and
  • 00:45:39 then is empty so if it is empty we want
  • 00:45:44 to return 0 otherwise
  • 00:45:49 what we want to do is actually remove
  • 00:45:52 from the database right so if it's there
  • 00:45:54 we want to remove so DB dot and then
  • 00:46:00 remove and then person maybe don't and
  • 00:46:04 then get just like that
  • 00:46:07 and then we turn one right so we know
  • 00:46:10 that we did delete this person now let's
  • 00:46:13 go ahead and implement update person so
  • 00:46:16 what we're going to do is simply say
  • 00:46:18 return and then get or actually select
  • 00:46:22 person by ID we're going to pass the ID
  • 00:46:26 and then what we want to do is say dot
  • 00:46:28 and then map
  • 00:46:30 so right here we get a person let's call
  • 00:46:33 it P so P and as I said if you want to
  • 00:46:37 learn about you know Java streams Maps
  • 00:46:41 operations filtering go ahead and check
  • 00:46:44 my YouTube video where I teach all of
  • 00:46:46 this so now let's go ahead and try and
  • 00:46:49 get the actual index of this person so
  • 00:46:52 int and then index of and then person to
  • 00:46:57 delete equals two and then DB dot and
  • 00:47:01 then index of and then pass the actual
  • 00:47:04 person right so now we have an index so
  • 00:47:08 I'm gonna say if the actual index to
  • 00:47:11 delete and I'm missing an e right there
  • 00:47:14 so if the index to delete is greater or
  • 00:47:18 equal to zero what we want to do is
  • 00:47:21 actually set the actual person so it
  • 00:47:27 means that we found someone right so
  • 00:47:29 let's go ahead and say DB
  • 00:47:30 dot and then set and then index of the
  • 00:47:34 person to delete and then simply pass
  • 00:47:37 the actual person right and now what we
  • 00:47:41 can do is simply return one right here
  • 00:47:44 so otherwise so else or we can simply
  • 00:47:49 say return and then zero and then right
  • 00:47:53 here so because we need to return an
  • 00:47:55 integer simply say don't and then or
  • 00:47:58 else and then zero right
  • 00:48:02 so basically if I format this so
  • 00:48:05 basically I'm saying select the person
  • 00:48:07 and then map that person to something
  • 00:48:11 else right and if the actual index of
  • 00:48:16 that person is greater or equal to zero
  • 00:48:19 we know that
  • 00:48:20 we found that person and then set the
  • 00:48:23 contents of that person to the new
  • 00:48:25 person that we've just received from the
  • 00:48:28 client and then returned one everything
  • 00:48:30 is fine otherwise return zero or if
  • 00:48:34 select person body is not present we
  • 00:48:37 don't do anything and simply return zero
  • 00:48:40 so this is what it means
  • 00:48:43 so now let's go ahead and simply you
  • 00:48:46 know feed these through the service as
  • 00:48:47 well as controller and then test it so
  • 00:48:50 let's go ahead and simply open up person
  • 00:48:53 service so let's go ahead and say public
  • 00:48:55 and then int delete and then person and
  • 00:49:02 then right here we're going to take the
  • 00:49:04 actual ID so ID and then this will be
  • 00:49:08 very simple all we need to do is simply
  • 00:49:11 say return and then person Dao dot and
  • 00:49:14 then delete by ID pass the actual ID and
  • 00:49:18 we're done to update person sorry
  • 00:49:21 so if I close that so to update a person
  • 00:49:25 what we need to do is simply say public
  • 00:49:27 and then end update and in person so
  • 00:49:32 we're going to take the actual ID so you
  • 00:49:35 it and then ID and then this person is
  • 00:49:41 the new person so Neal and then person
  • 00:49:44 right and then what we need to do is
  • 00:49:48 simply say return person the oh don't
  • 00:49:51 and an update and then ID right so you
  • 00:49:56 can see that I don't have much logic
  • 00:49:59 going on here and this is because you
  • 00:50:01 know I'm just teaching you exactly how
  • 00:50:03 to use you know sprinkle to to create an
  • 00:50:07 API but essentially you know inside of
  • 00:50:09 this service you have whatever logic
  • 00:50:12 your application requires right so I'm
  • 00:50:16 just making it simple for you to
  • 00:50:17 understand exactly so you have a lot of
  • 00:50:20 code going on basically you see the
  • 00:50:22 entire process of what we are doing so
  • 00:50:25 now let me go back to the actual person
  • 00:50:28 controller and simply expose those
  • 00:50:32 to a REST API so the first one is update
  • 00:50:37 or actually delete so let's go ahead and
  • 00:50:39 say delete so public and then void and
  • 00:50:43 then delete and then person by ID we
  • 00:50:49 simply pass the ID and pretty much the
  • 00:50:52 same thing at and then path variable and
  • 00:50:56 then this will be the ID and then we
  • 00:51:03 want that to be a you it ID and then
  • 00:51:07 return or actually there's no need to
  • 00:51:10 return here because this is void
  • 00:51:12 so simply say person service dot and
  • 00:51:15 then the lead person party ID now this
  • 00:51:20 has to be guess what at and then
  • 00:51:23 remember we are deleting a resource from
  • 00:51:26 the server so delete and then mapping so
  • 00:51:30 now inside we have to have the path and
  • 00:51:34 then the actual ID right so this ID is
  • 00:51:39 this one right here which we can pass
  • 00:51:42 down to the service so let's also go
  • 00:51:45 ahead and implement the actual update so
  • 00:51:48 the update will be at and then put and
  • 00:51:52 then mapping and public and then void
  • 00:51:58 and then update person and then this
  • 00:52:06 will take a person from the request body
  • 00:52:09 so at and then request body will take a
  • 00:52:14 person to update there we go and now
  • 00:52:20 simply say person service dot and then
  • 00:52:24 update and in fact we also need the
  • 00:52:27 actual path so the path will include the
  • 00:52:31 actual ID so ID and let's go ahead and
  • 00:52:36 say that at and then path and then
  • 00:52:39 variable and that will contain
  • 00:52:44 di D and this will be a youíd and then
  • 00:52:52 call the ID and there we go so now we
  • 00:52:57 can pass ID and then person to update
  • 00:53:00 right and to be honest this is all we
  • 00:53:04 have to do to implement our REST API so
  • 00:53:08 let's go ahead and test it so I'm gonna
  • 00:53:11 pretty much just restart the server so
  • 00:53:15 let me go ahead and open a postman so
  • 00:53:19 let's start with one person so let me
  • 00:53:23 send this request and then let me get
  • 00:53:27 everyone so send you see that we have
  • 00:53:31 Nelson Mandela so let's go ahead and
  • 00:53:34 change Nelson's name to something else
  • 00:53:37 so go ahead and simply create a new
  • 00:53:44 request so go ahead and change this to a
  • 00:53:47 put and the body so instead of saying
  • 00:53:52 and Nelson Mandela let's simply say that
  • 00:53:55 this is Alex and then Colin right and
  • 00:54:00 now let's go ahead and simply pass
  • 00:54:03 Nelson Mandela right here and basically
  • 00:54:07 we just changing his name so if I go
  • 00:54:10 ahead and send you can see that the
  • 00:54:14 request didn't work and now if I get
  • 00:54:16 everyone from our database so if I
  • 00:54:20 perform a get request and then send you
  • 00:54:24 can see that that didn't work and let's
  • 00:54:27 go ahead and debug this in a second but
  • 00:54:29 for now let's go ahead and pretty much
  • 00:54:32 try and delete this person so now some
  • 00:54:35 Mandela so if I put forth slash and then
  • 00:54:39 the actual ID and then delete and then
  • 00:54:43 send we got 200 if we get everyone you
  • 00:54:49 see that Nelson is not there and it's
  • 00:54:52 been deleted so we had a bug so
  • 00:54:57 if I post let's go ahead and post Anna
  • 00:55:01 Jones and basically we couldn't update
  • 00:55:05 the actual name so if I get Anna Jones
  • 00:55:09 get the actual ID and then perform a put
  • 00:55:15 and this will be an adjourns ID just
  • 00:55:25 like that and then body and then let's
  • 00:55:30 simply say update and then update send
  • 00:55:36 still a two hundred but if we get
  • 00:55:39 everyone you'll see that still Anna Joan
  • 00:55:42 so we have a bank so let's go ahead and
  • 00:55:44 try and find out why exactly this is
  • 00:55:47 happening so let me go back to IntelliJ
  • 00:55:51 and then close this and then open up the
  • 00:55:55 actual data access service and now this
  • 00:56:01 is the actual method so we are selecting
  • 00:56:05 the person mapping and then so basically
  • 00:56:09 so right here we have index of person to
  • 00:56:12 delete so this should be actual update
  • 00:56:15 so to update and DB index of person so
  • 00:56:24 basically yes so this is the error so so
  • 00:56:30 right here so you see that we have so
  • 00:56:33 this person right here so this is what
  • 00:56:35 we want to update so right so let me
  • 00:56:37 rename this to update right so the index
  • 00:56:41 of person to update you know it's minus
  • 00:56:45 one so this will always return zero so
  • 00:56:48 what we want is actual P so P is the
  • 00:56:52 actual person and in fact just let me
  • 00:56:53 rename this to person so we want this
  • 00:56:57 person that we found
  • 00:56:59 so person and then this should return
  • 00:57:02 the actual index and then if it's bigger
  • 00:57:05 or equal to zero
  • 00:57:07 what we want is new so because this
  • 00:57:11 update doesn't contain the actual ID so
  • 00:57:14 we need new person and then pass the ID
  • 00:57:18 and then simply say update dot and then
  • 00:57:21 get name so yeah I think this makes more
  • 00:57:26 sense so let's go ahead and restart the
  • 00:57:29 server there we go so now if I open a
  • 00:57:37 postman and then pretty much just yet so
  • 00:57:44 let's start from scratch so no one in
  • 00:57:46 the database let's go ahead and post
  • 00:57:49 anna jones and now we have to get let's
  • 00:57:56 go ahead and get so we get the actual
  • 00:57:59 youíd of anna jones
  • 00:58:00 and then right here let's go ahead and
  • 00:58:03 perform a put to this endpoint and
  • 00:58:09 simply pass anna jones
  • 00:58:11 you with or ID and then in the actual
  • 00:58:14 body let's also change this to Anna and
  • 00:58:18 then Montana just like that and then if
  • 00:58:22 we send so we go 200 and now this should
  • 00:58:27 work so go ahead and send and that uh so
  • 00:58:31 you can see that this now works so you
  • 00:58:35 see that we pretty much just managed to
  • 00:58:37 implement all the methods defined in our
  • 00:58:41 API so it is nice to have like tests for
  • 00:58:45 everything right here and basically I
  • 00:58:48 wrote tests for this class so go ahead
  • 00:58:52 and download the repo down below and you
  • 00:58:55 can run all the tests and see that this
  • 00:58:57 works as expected so I've got unit tests
  • 00:59:00 around all of this so one last thing
  • 00:59:04 that I want to mention is that you see
  • 00:59:07 inside of this person controller right
  • 00:59:10 here so
  • 00:59:12 this person controlled we can have you
  • 00:59:15 know some annotations that enforce null
  • 00:59:19 ability in our application so we can
  • 00:59:22 pretty much just use one some
  • 00:59:25 annotations and enforce that this object
  • 00:59:28 right here is always present and also
  • 00:59:31 valid so go ahead and open a person
  • 00:59:35 model and then right here what we can do
  • 00:59:39 is actually say that this field right
  • 00:59:42 here so the actual name this cannot be
  • 00:59:45 blank so at and then not and then blank
  • 00:59:50 right so the reason why I'm not saying
  • 00:59:52 no is because if you have an empty
  • 00:59:54 string it's no no but if you have a an
  • 00:59:58 empty string it's actually blank so
  • 01:00:00 basically we don't want that to be blank
  • 01:00:02 right so then go back to the actual
  • 01:00:05 controller and now you can say that the
  • 01:00:08 request body so this has to be so at and
  • 01:00:13 then valid right and then we can also
  • 01:00:16 say that this must not be no so we could
  • 01:00:20 also do the same here so we can enforce
  • 01:00:22 that this is valid and also at and then
  • 01:00:26 not know right
  • 01:00:27 so now if I pretty much restart the
  • 01:00:30 server so you see exactly what we're
  • 01:00:33 doing so basically we want to fail as
  • 01:00:36 soon as possible instead of having to
  • 01:00:38 deal with a null ability inside of our
  • 01:00:41 code so now let's go ahead and open up
  • 01:00:44 postman and then let's go ahead and
  • 01:00:48 pretty much try and post someone to our
  • 01:00:52 database so right here so you see that
  • 01:00:55 we have an account but if we send for
  • 01:00:58 example blank so send you see that we
  • 01:01:03 get an error and basically this is
  • 01:01:05 working as expanding as expected so you
  • 01:01:07 see that it's a bad request and it's
  • 01:01:10 pretty much saying that validation fail
  • 01:01:13 for object person right here so this is
  • 01:01:16 awesome and you can see the actual
  • 01:01:18 default message which says that the
  • 01:01:21 field name must not be blank so now if I
  • 01:01:25 go ahead and
  • 01:01:26 add the correct payload so right here so
  • 01:01:31 let's go ahead and say John and then
  • 01:01:34 Jones and then send you can see that we
  • 01:01:39 have way too and your status code and if
  • 01:01:42 I go ahead and get everyone you can see
  • 01:01:45 that we have John Jones so this is it to
  • 01:01:50 be honest so now you should be familiar
  • 01:01:52 with spring boot and writing api's for
  • 01:01:57 your applications so now the cool thing
  • 01:02:00 about it is that you can have a client
  • 01:02:02 API and then do all kinds of things
  • 01:02:04 depending on your application so one
  • 01:02:07 more thing actually that I forgot to
  • 01:02:09 show you is the pendency injection right
  • 01:02:12 so let's say that we want to have a
  • 01:02:15 Postgres implementation right another
  • 01:02:18 database or connecting to a different
  • 01:02:21 database so how do we change that
  • 01:02:23 without changing pretty much anything
  • 01:02:25 apart from one line of code so to do
  • 01:02:29 that what we need to do is open up the
  • 01:02:32 actual Dao package and then simply
  • 01:02:35 create in your class and right here
  • 01:02:37 simply call it for example person let's
  • 01:02:43 call it person and then data access and
  • 01:02:49 then service right let's simply call it
  • 01:02:52 that so this is the real thing now so ok
  • 01:02:56 implements and then person Dao and then
  • 01:03:02 implement all the methods and then right
  • 01:03:06 here so when we select everyone I simply
  • 01:03:09 return list dot and then off and then
  • 01:03:16 new and then person you wait don't run
  • 01:03:21 the MU it and then let's simply say from
  • 01:03:25 Postgres DB right so now what we need to
  • 01:03:29 do is annotate this with at and then
  • 01:03:33 repository and then simply say that this
  • 01:03:36 is a Postgres
  • 01:03:39 implementation right and then to change
  • 01:03:43 our implementation right all we have to
  • 01:03:46 do is grab this name right here and then
  • 01:03:49 go where you inject the actual interface
  • 01:03:51 which is inside of the service right
  • 01:03:54 it's a person service so now instead of
  • 01:03:57 fake Dao
  • 01:03:58 simply use Postgres like that now if I
  • 01:04:03 restart the actual server and then open
  • 01:04:08 a postman now if I perform I get you can
  • 01:04:16 see that we pretty much switched our
  • 01:04:19 database with just one line of code and
  • 01:04:21 you can see that this now is coming from
  • 01:04:23 a Postgres database so I'm actually
  • 01:04:26 trying to do a video on you know
  • 01:04:30 connecting to a real Postgres database
  • 01:04:32 but go ahead and check my channel
  • 01:04:35 because I've got a video on how to spin
  • 01:04:37 up a Postgres container using docker and
  • 01:04:39 also an example with spring boots so go
  • 01:04:42 ahead and check my video on that but I'm
  • 01:04:45 gonna make a second part on this video
  • 01:04:47 connecting to a real database so the
  • 01:04:52 last thing that I'm going to show you
  • 01:04:54 exactly how to spin up everything so go
  • 01:04:57 ahead and stop your server now what I
  • 01:05:00 want to show you is actually how to spin
  • 01:05:02 up the actual jar so this is the the the
  • 01:05:05 jar that you can deploy to a server and
  • 01:05:08 have your application
  • 01:05:10 I've been running so I'm gonna simply
  • 01:05:12 collapse that and then inside of maven
  • 01:05:16 so go ahead and open up this top right
  • 01:05:18 here so maven and you should see a
  • 01:05:21 target folder so if you don't see that
  • 01:05:24 so if I delete that simply go ahead and
  • 01:05:27 open up my cycle and then simply say
  • 01:05:30 install
  • 01:05:38 there we go so this is actually running
  • 01:05:40 some tests and there we go you see that
  • 01:05:43 we have a target folder right here so
  • 01:05:45 inside of the target folder go ahead and
  • 01:05:48 simply right click and then open in
  • 01:05:52 terminal so you should see that we have
  • 01:05:55 this demo
  • 01:05:57 0:01 snapshot jar the jar and this is
  • 01:06:01 what we can pretty much just run on the
  • 01:06:05 actual server right so when you deploy
  • 01:06:07 this to any environment so just to show
  • 01:06:11 you so if I open a post man so if I send
  • 01:06:15 a request you see that the server is not
  • 01:06:17 up and running so to kick off this jar
  • 01:06:20 all you have to do is simply say Java
  • 01:06:22 and right here so if I scroll up you can
  • 01:06:27 see that so if I scroll up you can see
  • 01:06:30 that we can say Java and then – jar and
  • 01:06:33 then pass the actual jar so go ahead and
  • 01:06:36 simply say Java
  • 01:06:37 – and then jar and then the actual jar
  • 01:06:41 so the jar name is demo and then if I
  • 01:06:45 press tab you can see that we get auto
  • 01:06:48 completion and then all I have to do is
  • 01:06:51 press ENTER and you can see that now we
  • 01:06:55 are starting our server so right here
  • 01:06:58 Tomcat started in port 8080 and what I'm
  • 01:07:02 gonna do now simply open up my web
  • 01:07:04 browser and this is the actual API that
  • 01:07:07 you would use so localhost and then 8080
  • 01:07:11 and then API and then V 1 and then
  • 01:07:16 person and then enter and there we go so
  • 01:07:20 you see that we have a JSON that we can
  • 01:07:23 consume and basically you can distribute
  • 01:07:25 this and have thousands of clients
  • 01:07:28 consume your API so I hope you had fun
  • 01:07:32 implementing this application or this
  • 01:07:36 API with Springwood and basically we
  • 01:07:39 achieved everything so we have you know
  • 01:07:42 client so in our case was postman we
  • 01:07:45 implemented get post delete and pull
  • 01:07:47 requests
  • 01:07:49 through the actual controller layer we
  • 01:07:52 had a service layer data access layer
  • 01:07:54 for a database you saw how to implement
  • 01:07:58 an in-memory database and then you saw
  • 01:08:00 how easy it was to switch from databases
  • 01:08:04 and then we returned the actual response
  • 01:08:06 to the actual client which is what you
  • 01:08:10 see right here so as I said I'm going to
  • 01:08:13 do a second part on this video I really
  • 01:08:17 want to add value by connecting to a
  • 01:08:18 real Postgres instance which to be
  • 01:08:22 honest is not going to be a lot of work
  • 01:08:23 as you saw it's just switching one line
  • 01:08:26 of code and then implementing the actual
  • 01:08:29 connectivity to Postgres
  • 01:08:40 so now let's actually go ahead and
  • 01:08:43 pretty much just use actually connect to
  • 01:08:49 a real database the database engine that
  • 01:08:52 we're gonna be using for this second
  • 01:08:54 part of this crash course is this
  • 01:08:56 amazing database engine called pulse
  • 01:09:00 grass so if you don't learn more about
  • 01:09:02 post branches you can go ahead and check
  • 01:09:03 my website where I've got this
  • 01:09:06 PostgreSQL crash course or actually it's
  • 01:09:09 a course about four hours long and it's
  • 01:09:12 absolutely free so you can see the
  • 01:09:14 roadmap right here and I cover the
  • 01:09:16 essentials that you need to know in
  • 01:09:18 order to be familiar with pulse waves
  • 01:09:21 and by far actually by far post price is
  • 01:09:25 the best database out there so go ahead
  • 01:09:28 and check it out go ahead and enroll and
  • 01:09:30 it's absolutely free so if I go back to
  • 01:09:33 the diagram so you can see that right
  • 01:09:35 here we have this data access layer and
  • 01:09:37 instead of talking to a fake database
  • 01:09:41 we're going to connect to a real
  • 01:09:42 database which will be Postgres so for
  • 01:09:46 this database what I'm going to be using
  • 01:09:48 is docker so I'm gonna use docker and
  • 01:09:51 simply say Postgres and go ahead and
  • 01:09:56 click on this very first link and
  • 01:09:59 basically if you don't know about
  • 01:10:01 Postgres and docker or docker and go
  • 01:10:05 ahead and check my video so I've got
  • 01:10:07 this video right here where I cover
  • 01:10:09 about where I cover it docker and
  • 01:10:13 Postgres in about 10 minutes so
  • 01:10:15 everything I'm going to teach you you
  • 01:10:16 can find on this video so if I close
  • 01:10:20 that and basically it's really
  • 01:10:23 straightforward so you can see that they
  • 01:10:25 have some description on what is
  • 01:10:27 Postgres and basically this is how you
  • 01:10:31 start an instance once you have Postgres
  • 01:10:34 installed so let me go ahead and simply
  • 01:10:37 grab this command right here so let me
  • 01:10:40 just grab this command and then go ahead
  • 01:10:44 and open up your terminal I term or
  • 01:10:47 command line so go ahead and simply
  • 01:10:52 open that up and what I'm gonna do is
  • 01:10:54 simply paste that in so actually I
  • 01:10:56 didn't copy that so let me just grab
  • 01:10:58 this and then place that in basically
  • 01:11:04 what we're doing here so just let me
  • 01:11:05 explain so so let's start from the very
  • 01:11:08 first command so docker run so this is
  • 01:11:11 the darker command and then I'm gonna
  • 01:11:14 name this container as so let me simply
  • 01:11:18 rename this to let's say post aggress
  • 01:11:23 and then – the spring and then right
  • 01:11:26 here so – E is the environment variable
  • 01:11:29 so we need that to set the actual
  • 01:11:31 password
  • 01:11:32 so let's simply keep things simple and
  • 01:11:34 simply have a very weak password so
  • 01:11:37 password and then – D Postgres and this
  • 01:11:42 is the actual image and go ahead and
  • 01:11:45 simply change this to post grass and
  • 01:11:47 then alpine so this is the smallest
  • 01:11:50 version and then what we need to do is
  • 01:11:53 also expose a port so we're gonna take
  • 01:11:56 the port that is running inside of that
  • 01:11:58 docker container so 5 4 3 2 and then
  • 01:12:02 expose that to the outside world as 5 4
  • 01:12:06 3 2 so basically what we're doing so
  • 01:12:11 basically this – D is in D touch mode so
  • 01:12:15 as soon as I press ENTER then we can
  • 01:12:17 carry on doing all the things so go
  • 01:12:20 ahead and check my video if you don't
  • 01:12:21 quite understand this because I teach
  • 01:12:23 you all of this so go ahead and simply
  • 01:12:26 press ENTER and there we go so now if I
  • 01:12:29 go ahead and simply say docker and then
  • 01:12:32 PS you can see that we have this
  • 01:12:35 container right here and the name is
  • 01:12:37 post quest spring so go ahead and simply
  • 01:12:40 say docker and then port and then post
  • 01:12:44 an ingress – spring and you can see that
  • 01:12:49 we are exposing the container port to
  • 01:12:53 the outside world on localhost and then
  • 01:12:56 5 4 3 2 and by the way 5 4 3 2 this is a
  • 01:13:00 default port for Postgres so now that we
  • 01:13:04 have
  • 01:13:05 a database so we can log into it and
  • 01:13:08 then perform various things so I'm gonna
  • 01:13:10 show you exactly how to SSH into this
  • 01:13:13 box in a second but for now you have a
  • 01:13:15 container running and exposing a port on
  • 01:13:18 five four three two
  • 01:13:19 so now what we need to do is actually go
  • 01:13:23 and open up IntelliJ and let's actually
  • 01:13:26 connect to this database so what I want
  • 01:13:28 to do is first refer this because I want
  • 01:13:31 to connect to Postgres and what we need
  • 01:13:35 to do is if I actually stop that and
  • 01:13:38 then close that so open up the resources
  • 01:13:42 and by the way I didn't mention that we
  • 01:13:44 have some tests right here so if I open
  • 01:13:47 up tests so you can see that I even took
  • 01:13:51 some time to write some tests to make
  • 01:13:53 sure that everything works so let me go
  • 01:13:56 ahead and close this and then what we're
  • 01:13:59 going to do is open up this application
  • 01:14:01 door properties so application
  • 01:14:04 application doc properties this is where
  • 01:14:07 you define the connection details to
  • 01:14:10 your database so go ahead and pretty
  • 01:14:14 much just delete the application doc
  • 01:14:16 properties so go ahead and delete
  • 01:14:19 applications or properties and inside
  • 01:14:23 what I want really is a file and simply
  • 01:14:27 call it application don't and then
  • 01:14:31 yellow why should I could have simply
  • 01:14:33 rename that to llamo but it's fine so
  • 01:14:36 okay and not for now I'm gonna commit
  • 01:14:40 this in a second so you can have
  • 01:14:41 everything on a branch
  • 01:14:43 so now inside of this application though
  • 01:14:46 ya know this is where we define all the
  • 01:14:49 connection details to our database but
  • 01:14:53 before we do that what we need to do is
  • 01:14:55 actually add some dependencies that will
  • 01:14:57 allow us to connect to databases have
  • 01:15:01 the drivers and then also give us the
  • 01:15:05 ability to write SQL commands against
  • 01:15:08 our database so for that what we need to
  • 01:15:11 do is open up palm dot XML and just let
  • 01:15:16 me collapse this for a second
  • 01:15:19 and you can see some information about
  • 01:15:23 this project I'm using you have 11 the
  • 01:15:26 version of spring boo is 2 point 1 3
  • 01:15:29 release and then you can see that we
  • 01:15:32 have some dependencies right here so
  • 01:15:35 this is the actual dependency for spring
  • 01:15:37 boot start to whip so this gives you
  • 01:15:39 ability to have a tomcat server up in
  • 01:15:41 running and write some API is using
  • 01:15:44 spring boo MVC so on and so forth and
  • 01:15:48 then we have this one right here so
  • 01:15:50 spring will start to test for writing
  • 01:15:53 tests and then we have this except for J
  • 01:15:56 which is pretty much an assertion
  • 01:15:58 library for writing unit tests so the
  • 01:16:01 dependencies that we need are the
  • 01:16:02 following so go ahead and simply add a
  • 01:16:05 new dependency so I'm gonna press ENTER
  • 01:16:08 and you can see that now we can add a
  • 01:16:11 new dependency so the first one that I
  • 01:16:13 need is from all dot Spring Framework
  • 01:16:17 top boot so you see me grabs that and
  • 01:16:19 then paste that in and then the artifact
  • 01:16:22 ID that we need will be spring and then
  • 01:16:27 – and then boot – and then starter and
  • 01:16:34 then – JDBC so let's actually duplicate
  • 01:16:39 this and then what we need is org dot
  • 01:16:44 and then post and then press key well
  • 01:16:48 and then the artifact ID is simply
  • 01:16:52 PostgreSQL so just let me grab that so
  • 01:16:56 that will be that and then what we need
  • 01:16:59 is simply have a scope and then this
  • 01:17:03 will be only at run time and finally we
  • 01:17:06 need another dependency and this time
  • 01:17:10 this will be org dot and then fly away
  • 01:17:14 and I'm gonna explain this in a second
  • 01:17:17 and then DB and then what we need is
  • 01:17:21 simply fly and then way and then core
  • 01:17:28 oh actually – and then core just like
  • 01:17:31 that so all of these dependencies will
  • 01:17:34 allow us to connect to a database and
  • 01:17:37 basically this is what allows us to
  • 01:17:41 write or actually it gives us an awesome
  • 01:17:44 API that we can execute some statements
  • 01:17:47 against our database this is for the
  • 01:17:50 actual driver so PostgreSQL and then
  • 01:17:53 this fly away DB is for database
  • 01:17:56 migrations and you'll see this in a
  • 01:17:58 second so now let me simply go ahead and
  • 01:18:01 open up the project tab and then if I
  • 01:18:04 open up the demo application and then
  • 01:18:07 press play
  • 01:18:15 you should see that we have an error and
  • 01:18:18 the error simply says that failed to
  • 01:18:22 configure data source URL attribute is
  • 01:18:24 not specified and no embedded data
  • 01:18:27 source could be configured so basically
  • 01:18:30 what we need to do now is simply to
  • 01:18:32 create a data source and inject the URL
  • 01:18:36 username and password and then pretty
  • 01:18:39 much allow us to and and pretty much
  • 01:18:41 that will allow us to connect to our
  • 01:18:43 database so let me go ahead and simply
  • 01:18:47 collapse this and what we need to do
  • 01:18:50 first is let's simply go ahead and
  • 01:18:52 create a new package so package and then
  • 01:18:55 simply call it as data and then source
  • 01:19:00 and then go ahead and press ok and
  • 01:19:08 inside of this package let's go ahead
  • 01:19:10 and simply put in your class and then
  • 01:19:13 simply say post grass and then data and
  • 01:19:17 then source press ok and don't ask me
  • 01:19:23 again I will add all of this later on so
  • 01:19:26 this class will be served as a
  • 01:19:28 configuration so at configuration and
  • 01:19:32 what we're gonna do inside of this class
  • 01:19:35 is simply return a data source so public
  • 01:19:40 and then it carry the data source and
  • 01:19:44 let's simply go ahead in any azúcar data
  • 01:19:47 source and what we're gonna do here is
  • 01:19:50 simply return a data source of builder
  • 01:19:54 and inside let's simply go ahead and say
  • 01:19:57 dot and then create and then write here
  • 01:20:00 simply go ahead and say and then simply
  • 01:20:03 go ahead and say dot and then type and
  • 01:20:05 the type for this will be e carry data
  • 01:20:11 source dot and then class and then dot
  • 01:20:14 and then build so you can see that now
  • 01:20:19 we have this data source and what we
  • 01:20:21 need to do is simply say add and then
  • 01:20:23 bean and what we need to do also is say
  • 01:20:27 configuration and then
  • 01:20:28 properties and then what we're gonna do
  • 01:20:31 is simply have app dot and then data and
  • 01:20:37 then source so basically we are
  • 01:20:41 instantiating this Akari data source as
  • 01:20:44 they been and and then what we're going
  • 01:20:46 to do is create some configuration file
  • 01:20:49 that will contain the user name URL
  • 01:20:51 password so on and so forth
  • 01:20:53 so let's go ahead and simply open up the
  • 01:20:55 application dot properties and inside of
  • 01:20:59 application log properties what we need
  • 01:21:01 to do is simply do the following
  • 01:21:03 so let's go ahead and have an app and
  • 01:21:07 property and inside of this app property
  • 01:21:10 we have data and then source and then
  • 01:21:14 inside of data source
  • 01:21:15 what we need is JDBC – URL and this will
  • 01:21:21 be JDBC colon host grass Kewell colon
  • 01:21:28 forward slash localhost and then the
  • 01:21:34 port will be five four three two and
  • 01:21:38 then four slash and then we need to
  • 01:21:42 connect to a database so we're going to
  • 01:21:44 create database in a second but for now
  • 01:21:46 let's simply name this as spring and
  • 01:21:51 then boot and then – and then post Grass
  • 01:21:57 dashdb it's a long name but you get the
  • 01:22:00 point so now we need a username so the
  • 01:22:03 default username that ships with
  • 01:22:05 Postgres it's called Postgres we need
  • 01:22:09 the password so password and remember
  • 01:22:12 the password that we chose was password
  • 01:22:14 and we can also define a pool size so
  • 01:22:20 basically that this is a number of
  • 01:22:22 connections that we can have actively
  • 01:22:25 instead of having to create a new
  • 01:22:28 connection to the database every time
  • 01:22:30 that we want to execute a query every
  • 01:22:33 time we want to execute a query against
  • 01:22:36 our database so I simply go ahead and I
  • 01:22:38 thirty
  • 01:22:40 and that should be it so you can see
  • 01:22:43 that we have app data source and inside
  • 01:22:46 of this data source package we have this
  • 01:22:49 post quest data source and basically
  • 01:22:52 it's just grabbing all the dependencies
  • 01:22:54 from this app and then data source so
  • 01:22:58 just let me go ahead and start this
  • 01:23:01 because I want you to see the errors and
  • 01:23:02 you see exactly what is going on so if I
  • 01:23:06 start the application there we go so now
  • 01:23:13 it says that cannot find migrations in
  • 01:23:17 location class path DB and then
  • 01:23:20 migration so what we need to do is to
  • 01:23:23 create a folder called DB and then
  • 01:23:28 another one called migrations and then
  • 01:23:30 have our database migrations inside it
  • 01:23:33 so what fly and this is basically fly
  • 01:23:36 away right so fly away allows us to
  • 01:23:39 manage our database schema as we evolve
  • 01:23:43 so go ahead and pretty much just learn
  • 01:23:46 about fly way right here so Flyway and
  • 01:23:52 basically you can read through the
  • 01:23:54 documentation but it is straight forward
  • 01:23:56 so just follow along and you'll
  • 01:23:58 understand exactly what I'm doing so if
  • 01:24:02 I close actually I just let me keep that
  • 01:24:03 open so if I go ahead and simply and
  • 01:24:08 close this or actually clear that and
  • 01:24:11 then I've got this time you know right
  • 01:24:14 here so I'm going to open this time you
  • 01:24:15 know and if I do PWD
  • 01:24:18 you can see that inside of desktop
  • 01:24:21 spring boot YouTube crash course so what
  • 01:24:24 I'm gonna do is simply CD into SRC and
  • 01:24:29 then main and then inside of core
  • 01:24:32 resources so inside let me go ahead and
  • 01:24:35 make a folder so make and then there and
  • 01:24:39 what I need is DB CD into DB and then
  • 01:24:46 make another folder so make
  • 01:24:52 dir and then migrations there we go
  • 01:25:00 anta and basically you could have simply
  • 01:25:03 come here and pretty much just right
  • 01:25:05 click and a new and then directory and
  • 01:25:09 then DB dot migrations but I prefer to
  • 01:25:12 use terminal and you should also get
  • 01:25:14 used to it so inside of DB migrations
  • 01:25:17 let's go ahead and create a file and
  • 01:25:21 this file will be as follows so version
  • 01:25:24 1 and the score and the score and then
  • 01:25:28 person table dot SQL so make sure that
  • 01:25:34 this is as follows because otherwise it
  • 01:25:37 won't work so version 1 and the score
  • 01:25:40 and the score person table dot SQL
  • 01:25:43 enter now let me ignore this extension
  • 01:25:47 so this is where we can write some SQL
  • 01:25:49 so let's go ahead and simply create
  • 01:25:51 table person and then inside of this
  • 01:25:56 person we have an ID so ID and this is
  • 01:26:00 of type you it and then this will be not
  • 01:26:04 and then no primary and then key and
  • 01:26:07 then we also have so if I open up the
  • 01:26:10 person class we have the actual name so
  • 01:26:14 if I go back so this will be name and
  • 01:26:18 then this is of our char so let's go
  • 01:26:24 ahead and simply add 100 not know so
  • 01:26:29 this is enough so now we have a table
  • 01:26:32 called person with the following columns
  • 01:26:35 ID and name so what I need to do now is
  • 01:26:39 actually go ahead and create our
  • 01:26:41 database so if I open up the
  • 01:26:44 applications or llamó so remember so
  • 01:26:47 right here we have this database called
  • 01:26:50 spring book post grants dashdb so you
  • 01:26:53 can give it a better name but you know
  • 01:26:56 i'm just giving this name so that you
  • 01:26:57 know exactly what we're doing so what we
  • 01:27:00 need to do now is actually so if i open
  • 01:27:03 up
  • 01:27:03 the terminal and this is where I've got
  • 01:27:07 you know of time some doctor commands
  • 01:27:10 and you saw the how we pretty much just
  • 01:27:13 launched this container which is running
  • 01:27:15 and also exposing this sport so five
  • 01:27:17 four three two so this is the port that
  • 01:27:20 we are connecting right so localhost
  • 01:27:23 five four three two so let's actually go
  • 01:27:26 ahead and bash into a disc on Taylor and
  • 01:27:29 create a database so simply go ahead and
  • 01:27:33 say docker and then exec and then – and
  • 01:27:38 then PI T for interactive and then what
  • 01:27:42 we want is actually if I grab this
  • 01:27:44 container ID and then simply say bin
  • 01:27:49 forward slash and then bash enter and
  • 01:27:53 you can see that now we are inside of
  • 01:27:55 that box or actually and you can see
  • 01:27:59 that now we are inside of this container
  • 01:28:02 right here which we can simply now run
  • 01:28:05 some commands so go ahead and simply say
  • 01:28:07 psql and you can see that we have P SQL
  • 01:28:13 working and now we can write some P SQL
  • 01:28:16 commands again if you've missed my
  • 01:28:20 course on P SQL or actually Postgres so
  • 01:28:24 on my website so this is where I teach
  • 01:28:27 everything that you see me typing and to
  • 01:28:31 be honest you should learn Postgres it's
  • 01:28:33 amazing and a lot of companies are using
  • 01:28:36 it and you can also use it for your
  • 01:28:38 personal project so let me go ahead and
  • 01:28:41 simply go back to my terminal and simply
  • 01:28:44 go ahead and say P SQL – and then copy
  • 01:28:47 to you for post grass so remember so
  • 01:28:52 this is the actual user name and then
  • 01:28:57 enter and there we go so you can see
  • 01:29:01 that now we are inside so if I pretty
  • 01:29:05 much type backslash
  • 01:29:06 and then L so if I clear the screen
  • 01:29:10 backslash L you can see that I do have
  • 01:29:15 three databases so you can see
  • 01:29:17 list of databases we have Postgres
  • 01:29:20 template zero and template one so let's
  • 01:29:23 go ahead and create a new database with
  • 01:29:26 this main so let me grab this and then
  • 01:29:30 if I type three eight
  • 01:29:32 so perhaps so create table actually
  • 01:29:36 database and then paste the name and I
  • 01:29:42 would semicolon enter
  • 01:29:44 oh okay so we can now have – so let me
  • 01:29:52 simply pretty much rename this to demo
  • 01:29:57 DB right so demo and then DB enter and
  • 01:30:04 you can see that worked back slash L and
  • 01:30:07 we have demo DB right here so let's go
  • 01:30:11 back to IntelliJ and let's simply rename
  • 01:30:16 this to demo DB and now what we need to
  • 01:30:22 do is actually connect to this database
  • 01:30:25 so let me connect inside so backslash c
  • 01:30:28 and then demo DB enter and you can see
  • 01:30:32 them connected to demo DB so if I do
  • 01:30:35 backslash and then D so to describe what
  • 01:30:39 we have in here so backslash T we
  • 01:30:43 haven't got anything back /dt you can
  • 01:30:46 see that there is no relations
  • 01:30:48 so these for everything and then DT is
  • 01:30:51 for tables only so now what I'm gonna do
  • 01:30:55 is go back to IntelliJ and let's go
  • 01:30:59 ahead and simply start our application
  • 01:31:01 so then actually go ahead and click on
  • 01:31:05 this icon right here edit configurations
  • 01:31:08 and let's go ahead and simply say that
  • 01:31:10 we only want one sink single instance
  • 01:31:13 running at a time so if you press play
  • 01:31:17 multiple times it means that you will
  • 01:31:20 only get the same process and not get
  • 01:31:24 two processes and then you you get like
  • 01:31:26 when error saying that the pour is
  • 01:31:28 already in you so we don't want that
  • 01:31:30 so go ahead and simply now play and we
  • 01:31:38 have an error and it says cannot find
  • 01:31:39 migrations in classpath DB migration and
  • 01:31:43 that's because I've misspelled migration
  • 01:31:48 have typed migrations instead of
  • 01:31:50 migration so let me rename that so
  • 01:31:54 migration and then we factor there we go
  • 01:31:57 and then start the application and there
  • 01:32:03 we go so you can see that everything
  • 01:32:05 started file and basically right here
  • 01:32:08 you can see that we have Akari pool
  • 01:32:11 started and then completed and then we
  • 01:32:15 have a database demo database right here
  • 01:32:19 and you can see that we have migrating
  • 01:32:24 schema so person table so basically that
  • 01:32:27 was executed so our command was executed
  • 01:32:29 correctly and you can see that the
  • 01:32:32 migration was applied and now we have
  • 01:32:35 the server running on port 8080 which is
  • 01:32:39 awesome so now so if I pretty much go
  • 01:32:44 back to my terminal so I just want to
  • 01:32:47 show you something first
  • 01:32:48 so remember previously we type backslash
  • 01:32:50 D and backslash DT and that wasn't
  • 01:32:53 working actually he was working but we
  • 01:32:56 didn't have any relations so if I type
  • 01:32:59 the same command so backslash D T you
  • 01:33:03 can see that now we have two tables so
  • 01:33:07 this one is for flowery specific which
  • 01:33:10 you shouldn't care at this point but you
  • 01:33:12 can see that we have this table right
  • 01:33:15 here called person and if I go ahead and
  • 01:33:18 describe person so backslash T and then
  • 01:33:21 person you can see that we have this
  • 01:33:27 table called person inside of the public
  • 01:33:29 schema and then the ID is of type u it
  • 01:33:32 not know and then we also have name and
  • 01:33:35 then it's a character varying and then
  • 01:33:38 this is 100 so this is the length and
  • 01:33:40 it's also not know and
  • 01:33:44 this is awesome so if I go ahead and
  • 01:33:46 simply say select and then start from
  • 01:33:50 and then person and that would say my
  • 01:33:53 column and you can see that we have zero
  • 01:33:57 rows so let's go ahead and simply insert
  • 01:34:00 someone so because I know I need to
  • 01:34:04 install an extension to be able to to
  • 01:34:10 generate into it and also I cover this
  • 01:34:12 on my free course I need to install an
  • 01:34:14 extension so what I want to do basically
  • 01:34:17 is this
  • 01:34:18 so select and then you it and then
  • 01:34:22 generate and then v4 as a function and
  • 01:34:27 you can see that no function matches so
  • 01:34:32 what we need to do is create extension
  • 01:34:36 and then this will be you it – OH ssp so
  • 01:34:43 W ID – Oh s SP and now it's my column
  • 01:34:49 enter you can see that worked now if I
  • 01:34:53 pretty much select you with January
  • 01:34:55 version four and you can see that now we
  • 01:34:57 can generate UITs so if i go ahead and
  • 01:35:00 simply say insert into and then person
  • 01:35:06 and then we have ID and the name and
  • 01:35:12 then values and then we want to generate
  • 01:35:17 you it so generate and then v4 and then
  • 01:35:23 the actual name so let's simply say that
  • 01:35:25 this is maria and then jones and then
  • 01:35:33 end out with parenthesis and enter you
  • 01:35:40 can see that marie jones does not exist
  • 01:35:43 and that's because that has to be single
  • 01:35:46 quotes and not double quotes so Marie
  • 01:35:49 Jones like that enter and you can see
  • 01:35:52 that worked so let me go ahead and add
  • 01:35:55 and Tom and then Smith enter now if I
  • 01:36:01 select star from person you can see that
  • 01:36:05 now we have two people inside of our
  • 01:36:08 database and this is awesome so we've
  • 01:36:12 managed to connect to our database using
  • 01:36:14 spring bool and we have a database up
  • 01:36:19 and running with a table and now what we
  • 01:36:23 need to do really is just write some
  • 01:36:26 code to retrieve data from our database
  • 01:36:30 using spring book actually I'm actually
  • 01:36:33 using Java and Springville and their API
  • 01:36:35 basically so let me go ahead and
  • 01:36:38 collapse this and inside of the person
  • 01:36:42 data access service so let's go ahead
  • 01:36:44 and write some code that will retrieve
  • 01:36:46 as everyone from our database so what we
  • 01:36:51 need to do is to use this class right
  • 01:36:54 here so private final and then JDBC
  • 01:36:59 template so what we're gonna do is add
  • 01:37:02 this to constructor and then also wire
  • 01:37:06 so this is dependency injection we don't
  • 01:37:08 have to do anything
  • 01:37:09 spring spring boot knows how to
  • 01:37:12 initialize correctly this class for us
  • 01:37:15 so that we can write some code against
  • 01:37:19 our database so now instead of saying
  • 01:37:23 select all people and then return a list
  • 01:37:27 so basically this fake list what we're
  • 01:37:30 gonna do is use JDBC template API so
  • 01:37:33 simply go ahead and say JDBC template
  • 01:37:36 dot and you can see that they have
  • 01:37:39 various methods so the one that we want
  • 01:37:42 is query so we want to query and the
  • 01:37:47 query takes two things so we can pass s
  • 01:37:50 so we can pass an SQL statement and then
  • 01:37:55 we have to pass a row mapper so
  • 01:37:58 basically the block mapper is how we
  • 01:38:00 take the values that we retrieve from
  • 01:38:02 our database and then transform that
  • 01:38:05 into a Java object
  • 01:38:08 so let me go ahead and simply simply
  • 01:38:12 define a string so string and let's go
  • 01:38:16 ahead and make this final not needed but
  • 01:38:19 point O so SQL equals to select so
  • 01:38:24 basically the same thing that we had so
  • 01:38:26 select name Oh actually this is ID name
  • 01:38:31 and then from and then person not the
  • 01:38:36 same so previously we simply said select
  • 01:38:38 star so right here so select star so
  • 01:38:41 select and then ID name from person and
  • 01:38:49 this is the same thing so I I prefer to
  • 01:38:52 use the actual column names instead of
  • 01:38:54 the instead of star so now let's go
  • 01:38:59 ahead and simply pass the SQL query so
  • 01:39:03 xql and now we have to pass this row
  • 01:39:07 mapper so this bro mapper is a lambda
  • 01:39:10 that has access to the result set to the
  • 01:39:13 result set as well as the index so what
  • 01:39:17 we're gonna do inside here so let's
  • 01:39:20 simply go ahead and say return and then
  • 01:39:25 we need to return a person object so
  • 01:39:28 person so this is our model and inside
  • 01:39:32 of our person we need to pass what we
  • 01:39:35 need to pass the youíd so this should be
  • 01:39:39 new person just like that so inside you
  • 01:39:42 can see that we have to pass a u it and
  • 01:39:44 also a name so inside of the results
  • 01:39:48 sent we can get the string so get string
  • 01:39:52 and then you can see that we can pass
  • 01:39:55 the column label so I know the column
  • 01:39:57 label is so basically it's ID and also
  • 01:40:02 we can get the actual name so resolve
  • 01:40:05 set dot and then get string and then
  • 01:40:10 this will be named so if I put this this
  • 01:40:12 on a new line and you can see exactly
  • 01:40:14 what we're doing so right here you can
  • 01:40:17 see that we have an error and this is
  • 01:40:19 because this result said I'll get
  • 01:40:22 string is a string basically and what we
  • 01:40:25 need is a you wit type so I simply go
  • 01:40:27 ahead do it and then from string and
  • 01:40:31 then we need that so that will work so
  • 01:40:36 you can see that now this works let's
  • 01:40:40 end up with semicolon and what I like to
  • 01:40:43 do is actually to extract this way
  • 01:40:44 variable so all of that actually and
  • 01:40:48 then this will be ID and then this will
  • 01:40:52 be name so you see exactly what's
  • 01:40:55 happening and we can remove that and
  • 01:40:59 also that and then that and then format
  • 01:41:04 everything so now I don't need to return
  • 01:41:07 the list of pretty much just fake people
  • 01:41:11 so this from Postgres DB is not name and
  • 01:41:15 we are retrieving from a database so you
  • 01:41:18 can see that I still have an error and
  • 01:41:21 this returns a list of people right so
  • 01:41:26 you can see that's a list of people so I
  • 01:41:29 can either say return and then people
  • 01:41:34 oops so people just like that or I can
  • 01:41:41 simply return that in line so we factor
  • 01:41:43 and there we go so you can see that I'm
  • 01:41:46 returning that in line which is awesome
  • 01:41:49 so this is it so if I now restart the
  • 01:41:54 application so let me go ahead and
  • 01:41:57 simply say stop and rerun there we go
  • 01:42:03 it's up and running and if I open up
  • 01:42:07 Chrome and then remember so right here
  • 01:42:11 we previously add an empty list and this
  • 01:42:15 is because he was coming from our fake
  • 01:42:18 database so now if i refresh this there
  • 01:42:24 we go
  • 01:42:24 so you can see that now this is coming
  • 01:42:26 from our database and this is awesome
  • 01:42:30 guys so you can see the power of Spring
  • 01:42:33 boo basically you can just write code
  • 01:42:35 you know very fast and they give you
  • 01:42:38 these tools so that you can focus on
  • 01:42:41 writing what matters instead of you
  • 01:42:43 having to configure things so there we
  • 01:42:46 go so let me go ahead and simply add
  • 01:42:48 someone else into this table so let's go
  • 01:42:52 ahead and simply say Tomcat actually no
  • 01:42:58 it's okay let's simply say Ana and then
  • 01:43:04 Jamil and an enter so if i refresh this
  • 01:43:11 you can see that now we have a ninja mil
  • 01:43:14 so there we go
  • 01:43:18 if I collapse this you can see that this
  • 01:43:21 is only one implementation of our API
  • 01:43:25 right so we have a bunch of other
  • 01:43:28 methods
  • 01:43:28 so select person by ID actually let's go
  • 01:43:31 ahead and implement this one quickly so
  • 01:43:33 you see exactly how this is done so what
  • 01:43:37 you want is to pass and Ashley select ID
  • 01:43:42 name from person and then here you can
  • 01:43:44 say we're in an ID equals two question
  • 01:43:49 mark
  • 01:43:49 and then what you want is to take the
  • 01:43:52 exact same thing here and then simply
  • 01:43:56 return that but instead of query because
  • 01:43:59 query returns you a list what you want
  • 01:44:03 is query for and an object and query for
  • 01:44:07 object takes the SQL but we also need to
  • 01:44:11 pass the arguments so new object so this
  • 01:44:16 must be an array so just like that and
  • 01:44:21 you can see that so currently this is
  • 01:44:25 empty right but I need to pass this ID
  • 01:44:27 that comes from the client so ID and
  • 01:44:31 then this let's rename this because it's
  • 01:44:34 already in scope so person and then ID
  • 01:44:37 and let's rename that to person ID as
  • 01:44:40 well just like that and let's actually
  • 01:44:45 change this so this will be
  • 01:44:49 a person right but what we want really
  • 01:44:52 is to return so return optional dot of
  • 01:44:57 nullable because that can be no and then
  • 01:45:00 past person just like that so basically
  • 01:45:04 it's the same thing but we have this
  • 01:45:06 question mark and then query for object
  • 01:45:10 we pass the ID right here so if I put
  • 01:45:13 this on a new line so you can see
  • 01:45:14 exactly what we're doing
  • 01:45:17 so in than that so we pass the SQL the
  • 01:45:22 ID and basically if you had more
  • 01:45:24 argument arguments you'd simply pass
  • 01:45:26 them right here so we have the ID and
  • 01:45:29 then this is the role mapper as you saw
  • 01:45:31 before so right here and then we simply
  • 01:45:34 return the optional of nullable of
  • 01:45:36 person because you know we might not
  • 01:45:38 find a student with a given ID so if I
  • 01:45:42 restart this and then open up Chrome so
  • 01:45:51 if i refresh you can see that this still
  • 01:45:55 works but if I now take this ID and then
  • 01:45:59 pretty much to four slash and then ID
  • 01:46:01 remove the quotes there enter and you
  • 01:46:07 can see that now we only get one person
  • 01:46:10 and this is awesome so there we go so
  • 01:46:14 you can see that we managed to connect
  • 01:46:16 to a database using docker Postgres
  • 01:46:21 flyweight spring JDBC and you can see
  • 01:46:26 that this was pretty much you know very
  • 01:46:28 straightforward and we even managed to
  • 01:46:29 use Akari so I didn't even explain what
  • 01:46:32 Kari was but Kari is the actual
  • 01:46:35 recommended data source are you should
  • 01:46:38 be using for spring book because it's
  • 01:46:41 very fast reliable and yeah it's just
  • 01:46:44 one of the best ones out there so there
  • 01:46:48 we go guys if you have any questions on
  • 01:46:50 this and basically I'm gonna let you
  • 01:46:53 implement the rest of the logic so
  • 01:46:57 insert person
  • 01:46:59 the same with delete and then update but
  • 01:47:03 basically it is straightforward using
  • 01:47:05 some SQL so go ahead and pretty much
  • 01:47:10 just subscribe to my channel to get more
  • 01:47:11 videos like this if you want to learn
  • 01:47:14 more about all of this and more so I've
  • 01:47:17 got a spring book full stack web
  • 01:47:20 development course where I teach all of
  • 01:47:23 this and a lot more and also creating
  • 01:47:27 user interface for this using react GX
  • 01:47:30 and hooks and pretty much it's a lot of
  • 01:47:34 content packed into one course where I'm
  • 01:47:37 pretty sure that once you learned that
  • 01:47:39 you will be able to build applications
  • 01:47:41 on your own and also apply for jobs
  • 01:47:44 because a lot of companies are looking
  • 01:47:47 for a springboard developers or you know
  • 01:47:50 Java developers but spring bow is used
  • 01:47:53 quite a lot in the industry and once you
  • 01:47:56 know all of these skills so dependency
  • 01:47:58 injection connecting to databases
  • 01:48:00 writing tests writing integration tests
  • 01:48:04 writing user interface for it I'm pretty
  • 01:48:07 sure that you will be able to get a job
  • 01:48:10 okie-dokie
  • 01:48:12 as you saw throughout this crash course
  • 01:48:14 spring book is amazing so we really just
  • 01:48:18 scratched the surface and the next step
  • 01:48:21 for you is to check out my full stack
  • 01:48:24 web development spring boot and react
  • 01:48:27 and in that course I'm going to teach
  • 01:48:30 you everything you need to know about
  • 01:48:32 spring boots so we're going to cover
  • 01:48:34 essential topics that you need to know
  • 01:48:37 as a software developer and the cool
  • 01:48:40 thing about this course is that whatever
  • 01:48:43 you learn you will be deploying it into
  • 01:48:46 a real server using Amazon Elastic
  • 01:48:48 Beanstalk and upon completion the skills
  • 01:48:52 are quiet in this course it will help
  • 01:48:55 you apply for jobs because it will help
  • 01:48:57 you with a lot of great important
  • 01:48:59 concepts that a software engineer should
  • 01:49:02 know also you can start your own
  • 01:49:04 projects University projects and pretty
  • 01:49:07 much if you are a developer you should
  • 01:49:10 be aware of this awesome
  • 01:49:13 and how to use it inside out this is all
  • 01:49:17 for now go ahead and check out my
  • 01:49:19 youtube channel where I've got more
  • 01:49:21 content similar to this it was a
  • 01:49:24 pleasure teaching you and I'll catch you
  • 01:49:26 in the next one see ya