Spring Boot Tutorial using JDBC + Connection Pool + Flyway + JDBC Template + SQL and Docker 😲

Spring Boot Tutorial using JDBC + Connection Pool + Flyway + JDBC Template + SQL and Docker 😲


Spring Boot Tutorial using JDBC + Connection Pool + Flyway + JDBC Template + SQL and Docker 😲

In this video you learn how to work with databases using JDBC, Creating Datasources, Database Connection pool, Flyway for Database Migrations and JDBC Template for executing sql queries against any relational database of choice.

#jdbc #jdbctemplate #amigoscode

Github Repo: https://github.com/amigoscode/jdbc

Don’t Forget to 😲
===========================================
πŸ’― Subscribe to Amigoscode - http://bit.ly/2HpF5V8
πŸ’― Courses Available for free here - https://amigoscode.com/courses
πŸ’― Join Private Facebook Group and Discord - https://amigoscode.com/p/join-community

⭐ Table Of Contents ⭐
===========================================
00:00 - Intro
01:54 - Requirements
02:32 - Architecture Overview
07:02 - Github Repo
08:28 - Backend Overview
14:28 - Docker and PostgreSQL
20:28 - Hikari Datasource
25:01 - Database Migration with Flyway
30:04 - Running app
35:51 - JDBC Template
39:46 - Other JDBC Wrappers
40:26 - JDBC Template and Insert
46:52 - JDBC Template Select and RowMappers
55:07 - JDBC Template Select by id and RowMappers
01:00:17 - JDBC Template Delete
01:04:31 - Exercise
01:06:09 - Outro

πŸ™Š Here are the goods for all my videos video πŸ™Š
===========================================

β–Ί Recommended Books
===========================================
- Clean Code - https://amzn.to/2UGDPlX
- HTTP: The Definitive Guide - https://amzn.to/2JDVi8s
- Clean Architecture - https://amzn.to/2xOBNXW

β–Ί Computer and Monitor
===========================================
- New Apple MacBook Pro - https://amzn.to/3464Mmn
- Dell 27 INCH Ultrasharp U2719D Monitor - https://amzn.to/2xM3nW1
- Double Arm Stand Desk Mount - https://amzn.to/3aYKKfs
- USB C Hub Multiport Adapter - https://amzn.to/2Jz7NlL

β–Ί Camera Gear
=============================================
- Sony ILCE7M3B Full Frame Mirrorless Camera - https://amzn.to/346QIJn
- Sigma 16 mm F1.4 DC DN - https://amzn.to/2wbic3Q
- Sigma 33B965 30 mm F1.4 DC DC - https://amzn.to/39G37Fd

β–Ί IDE \u0026 Tools I use for coding πŸ’» πŸŽ’
===========================================
- ITerm
- VsCode
- GoLand
- IntelliJ Ultimate
- Sublime

P.S
===========================================
πŸ’― Don’t forget to subscribe | http://bit.ly/2HpF5V8
πŸ’― Join Private Facebook Group and Discord - https://amigoscode.com/p/join-community
πŸ’― Follow me on Instagram | http://bit.ly/2TSkA9w

❀️ Thanks for watching


Content

0 -> What's going on, guys, Assalamualaikum. Welcome to Amigoscode in this
3.61 -> video. I'm going to teach you everything you need to know in order to start
7.2 -> working with databases. In this video, I'm going
10.9 -> to teach you about JDBC connection pools, how to
14.5 -> set up your data sources, flyaway for
17.85 -> database migration, as well as how to use JDBC template,
21.81 -> which is a JDBC wrapper for interacting with your
25.98 -> database. So I know a lot of you guys have issues when it comes
29.65 -> to this kind of stuff, and also because
34.1 -> I don't think the right way for you to learn how
37.44 -> to store data and manipulate it through,
40.97 -> for example, JPA and Hibernate, I don't think
44.34 -> that's the best way for you to learn, and instead you should really know exactly
48.47 -> how to do things from the ground up and understand exactly,
52.72 -> for example, how road mappers works, connection pooling transactions
57.17 -> and all of that good stuff. If you need to my channel, go ahead
60.38 -> and give me a thumbs up. So literally take 2 seconds.
63.59 -> D smash that like button, literally just take 2
66.947 -> seconds and smash that like button because it helps me to
70.14 -> keep on recording videos like this. I had a comment of a
73.41 -> guy saying, if you don't, if I
77.07 -> don't smash the like button, then that means I can't basically
81.46 -> record these videos and think of it as a
85.38 -> battery. Right? So if your laptop runs out of battery, then you
88.9 -> can't really turn it on. So the same thing with me. So if you show
92.26 -> that you like this video by giving me a thumbs up, that gives me a
96.02 -> boost to keep on producing content like this. If you're not
99.4 -> part of the Amigo Score community, go ahead and join both the private Facebook group
103.36 -> as well as Discord. The community is growing over 30K people
107.05 -> combined, which is amazing. And I would love you to see you there without further
111.1 -> Ado. Let's take off this video.
119.12 -> So for this video, the requirements that
123.12 -> I needed to have is the basics of Java.
127.34 -> I also needed to have a basic knowledge of SQL Docker,
132.65 -> how to use IntelliJ and how to use postman.
136.64 -> So these are the essentials and really basic. But if you're not
140.37 -> sure about these technologies, don't worry. I'm gonna go step by step and it should
144.21 -> be really easy to follow. And also you can grab the source
147.39 -> code for the entire project on my GitHub.
155.52 -> So before we kick
159.07 -> off coding and diving into IntelliJ, let me
162.28 -> go ahead and give you the diagram which you can find in the
166.09 -> repo. And I'm going to show you this in a second, but basically here I
169.06 -> just want to explain exactly what we're going to be coding today.
172.8 -> So in here the purpose of this video is
176.32 -> really to show you how to connect to databases and how
179.86 -> things work. So I want to understand, for example,
183.06 -> data source. So what a data source is.
185.7 -> Well, a data source provides a way for you to obtain
190.48 -> a DBMS connection. Right. And this
194.29 -> entry points to a connection pool. Now,
197.39 -> when it comes to the Java world, you've got two options,
200.04 -> mainly. So you've got a carp and Tomcat CP,
203.85 -> but a car CP is one of the most popular ones due to its performance.
208.55 -> So in here you can see that you have a data source in
211.92 -> here. You can have one or more data sources because you
215.54 -> might want to connect to multiple databases from your application.
219.86 -> Now the data source points to a connection pool right here.
223.64 -> And because obtaining a connection can
227.01 -> be a really expensive operation. So what the connection pool gives
231.03 -> you is a list of connections that you
234.48 -> can pick from. So instead of the client each time that wants
238.11 -> to perform, let's say, a query against your database, it doesn't
241.74 -> have to open the connection, close the connection and all that
245.31 -> stuff that goes in between. Right. Because it's very expensive.
248.72 -> So instead it actually picks a free connection from the
252.24 -> connection pool. And in here you can see that all of these on connection.
255.51 -> So here I've got six connections that point to the database. And then I've got
259.37 -> three right here which are free and three. So this is just
262.95 -> an example. But you see that here, for example, three clients using
267.07 -> the connection, and as soon as they are done, the connection becomes
270.65 -> free. So here if I delete this, this then becomes free,
274.49 -> just like this. So this is the power of connection
279.01 -> pools. And then if you have another client,
282.06 -> then it uses any of these free connections. So also
288.2 -> what I forgot to mention is within the data source. Actually,
291.35 -> you need to define the properties that allows you to connect to your database.
295.02 -> So usually the URL, the username, the password and the
299.39 -> driver class name and all the parameters that you need.
304.35 -> Then basically you have the data source,
307.58 -> you have the connection pool, then you have your database. So for this,
310.79 -> we're going to use Docker to spin up a Postgres DB,
314.23 -> and I already have the Yamo file for you. So you don't have to worry
317.48 -> about none of this. Whether you are in Windows or Mac, this should
321.01 -> work. Then in here what we're going to be using it fly away.
324.72 -> So flyway. It's a database migration and more on it
328.03 -> later. But basically have Flyway or like base. These are the
331.48 -> most popular ones. But just imagine the scenario
335.49 -> where you actually need to make changes on your database schema.
338.55 -> Right. So instead of you having a large file and
342.67 -> basically apply that yourself
346.73 -> and manage that yourself. That could be quite challenging. So you have flyway,
350.77 -> which pretty much takes care of it. Then on this side right
354.84 -> here we have JDBC. So JDBC, it's the
358.66 -> Java connectivity interface,
362.4 -> which basically defines our client may access a database.
366.93 -> So again, more on this later. I'm going to show you exactly what it is.
370.53 -> But back in the day, people used
373.84 -> to use raw JDBC to connect to databases. But these days we have
377.77 -> a bunch of wrappers that basically facilitate the way
381.16 -> that we connect to databases using JDBC.
384.84 -> So for this video, we're gonna be using JDBC template.
388.18 -> And basically it simplifies the use of JDBC and
392.32 -> avoids common errors, which is really, really cool.
395.74 -> Then we're going to have postman. So this will be our client.
399.54 -> And I already have a backend which is almost written
403.26 -> for you. And the only thing that we need to change is the Dao,
407.26 -> which has a bunch of unimplemented method.
409.93 -> So to be honest, this is it. So you can
413.29 -> see that it's not that difficult. And hopefully by the end of this
417.28 -> video, you'll have a full understanding of how all of this
420.91 -> works.
427.82 -> So for this video, I already
431.73 -> have this repo, which you can find in the description of this video.
435.42 -> But basically this repository right here already contained
438.9 -> some code that basically helps you to get
442.8 -> started. But in here you can see that I've got the read me
446.45 -> as well as the diagram in here how to get started.
450.02 -> And then we have an exercise at the end right here.
453.56 -> So what I want to show you is I do have this branch
457.51 -> right here called Start, and this is the branch
460.88 -> that you should check out in order for you to follow
464.87 -> along. So check out this branch right here called Start
468.77 -> to follow along. Literally. I want you to follow
472.07 -> along to make sure that you understand what we're doing. And basically
476 -> I just want you to learn from this video. So go ahead and grab this
479.93 -> branch right here. And for this video, I'm going
483.41 -> to be using IntelliJ as my IDE of choice. So I
486.91 -> highly recommend you to use IntelliJ as well
490.19 -> to follow along. I'm going to be using the Ultimate Edition, but you
493.88 -> can follow along with the community. It will also work.
498.81 -> So go ahead and basically open
502.42 -> up this repository with IntelliJ. And next let's
505.87 -> go ahead and get started.
514.03 -> Let me go ahead and walk you through the set up that
517.41 -> I have for you in this project so that you can follow along as quick
521.07 -> as possible. So in here, this is a Maven project.
524.38 -> And if I open up the Pom XML,
527.61 -> you should see that we have. So right here I'm actually using Java 16,
531.93 -> and I've got a couple of dependencies. So in
535.19 -> here let's just make this full screen.
538.52 -> And basically these are like standard dependency for
542.47 -> building a microservice and restful APIs with
546.78 -> spring boot. So here we have spring boot start a web.
550.11 -> We also have started test. This is for testing.
553.53 -> But then this is what we are interested. So here I've got the Postgres
557.5 -> dependency, and this is because we're going to be using Postgres as our database.
561.72 -> And then we have spring boot start JDBC.
565.65 -> So this is what allows us to interact with our database. So this is
569.65 -> the JDBC wrapper. We also have fly away
573.31 -> and this is for database migrations.
576.87 -> So for flyway in here if I open a project and
581.07 -> then going to source and then main and
584.54 -> then resources of look,
587.6 -> there is a folder called DB.
590.57 -> And inside there is another one called Migration.
593.87 -> And inside there is this V one and the
597.82 -> score and the score. And then in the tables SQL.
602.07 -> So this is where we're going to add our SQL to create the
605.17 -> tables in a second. I also have the diagram right here for you.
608.83 -> If you have the Draw IO plugin with an intelligent you
612.1 -> can actually open up this diagram as well. So here if I open,
616.23 -> this is actually black. So that's why I'm using VSCO
620.26 -> to show you in white, but it doesn't look nice all black.
624.18 -> So here let me just close this. Also we have
627.31 -> the Docker compose Yamo. So in here this is
631.09 -> what you need for getting a Postgres database up and running.
635.08 -> So all you do just run this Docker compose
639.18 -> and then you get a database as well as
642.22 -> PG admin. So this is the GUI client in case you
645.64 -> don't have the database integration within IntelliJ.
649.03 -> So right here I've got the database integration, but I'm not
652.99 -> going to be using this, but so that, you know, you can actually access it
656.56 -> through Port. So local host 50 50. And then
660.37 -> you should see the good client. I'm going to show you this in a second
663.61 -> as well. Then also right here I've got some
667.45 -> volume. So this is for storing the data. If you learn
671.63 -> more about Docker, I've got a brand new course on Docker that teaches you Docker
675.27 -> from start to finish, allowing you to fully master everything
679.24 -> you need to know about Docker. So go ahead and check
682.4 -> it out. Link will in the description of this video. All right. So this is
685.55 -> the Docker compose. You've seen the Palm, you've seen
689.27 -> the DB migration, and let me open up the
692.69 -> Java in here and then inside. I already have some
697.18 -> code. And this is really straightforward. And I've taught you a bunch
700.85 -> of times how to build applications like this. So if
704.33 -> you learn about spring boot, go ahead and check my website,
707.12 -> a free course on spring boot teaching you how to build this app.
710.92 -> But basically what I want to touch is so in
714.62 -> here we have the controller. And basically
718.49 -> within this controller, we kind of basically
722.9 -> we have the API V one movie. So this is the path we
726.79 -> also have get mappings for all the movies,
729.82 -> getting a movie by ID, adding a movie and deleting movie.
734.13 -> And then here there is a to do, which is for an exercise that
737.77 -> I want to give you later. All right. Cool. So from
741.25 -> the controller, we move into the service. So this is
744.65 -> following the interior architecture. And then within the service,
748.25 -> we have the movie Dao, which is an interface
752.76 -> in this class movie service. So this is mainly for business
756.39 -> logic. So deleting a movie getting a movie so on and so forth.
760.52 -> Right. So this is really simple stuff. And if I move
763.96 -> into the Dao, so this allows us to basically
768.62 -> interact with our database. So this is an interface. And currently I
772.82 -> have one implementation right here, which is this guy. So movie
776.91 -> data access service. And this is pretty much where
780.49 -> we're going to learn how to use JDBC template
784.76 -> to connect to our database. You can see I've left unsupported
788.77 -> operation exception not implemented. So we're going to implement
792.33 -> all of this. And if I open a project,
796.17 -> so here this is the exception. So not for an
799.47 -> exception. And here we actually have full control of
802.82 -> the response status, which is 404.
806.02 -> And then here we have an actor and
809.7 -> I'm going to touch on actors later. But basically this is for part
813.69 -> of the exercise later. But this is a record and also
817.76 -> here for movie. This is a record. So I'm using Java 16 and I
822.37 -> can use the records instead of having all of these POJOs
827.71 -> with getters and setters constructors on and so forth.
831.33 -> I can just have the records and do ten.
834.55 -> So if you can learn more about records, I've got a video teaching you
838.01 -> about records. And the final thing I want to show you is
841.58 -> the application Yamo. So here application Yamo.
845.39 -> So this defines basically some configuration for connecting
849.32 -> to our database. But here I'll actually pull everything you need
852.51 -> in order to get up and running with your database.
856.05 -> Also, we have the server error so that this is when we throw
859.44 -> the exception, we actually see the error within the exception. So to
863.16 -> be honest, this is it. Next, let's go ahead and get a
866.72 -> database up and running.
874.4 -> We need this database so
877.98 -> that we can then build the data source that will have
882.13 -> the connection pool. And then we can use JDBC template that
886.02 -> will serve as a client to interact with
889.7 -> our database. So I already done the
893.09 -> hard part, which is basically giving you this
896.86 -> Docker compose. So in here.
900.16 -> So inside of this app folder right
903.89 -> here we've got the Docker compose Yamo
907.85 -> in here. So this gives us a database that
912.39 -> we can use to connect our back end
916.18 -> too. Now the reason why I'm using Docker is because if you're on
919.32 -> Windows or Mac, this should just work.
922.72 -> That's the benefit of using Docker instead of installing
926.35 -> the database for the appropriate operating system.
930.13 -> So even learn more about Docker. I've got this
934.02 -> course right here Docker for DevOps engineers.
937.15 -> And here I teach you everything about Docker. So what it is how
941.05 -> it differs from a virtual machine, the architecture,
945.38 -> Docker, images, containers, volumes,
948.25 -> Docker, compose, image tagging, networking, debugging,
951.45 -> security, best practices, and a bunch more.
955.4 -> So I'm going to leave this course right here and the description
958.83 -> of this video as well as with a coupon code which you can use if
962.55 -> you want to learn about Docker. But Docker is a must for
966.32 -> back end engineers these days because it's really important that
969.69 -> you know how you can take your applications package up into an image
973.85 -> and run containers from it. So in here I
976.95 -> do have this Docker compose that you can see here.
980.49 -> And what I'm going to do is within IntelliJ.
984.17 -> I can just run this Docker compose file.
987.45 -> So here I can pretty much just right click on the
990.78 -> Docker compose file. And then I'm going to say run.
995.98 -> Now this will give us a couple of things.
999.5 -> So if you have a look at the logs,
1002.04 -> it's creating the network, the volume. So this is where we
1005.68 -> can store the data and then Postgres. Right here we have
1009.7 -> Postgres and then we have PG admin. So this
1013.34 -> is cool. Now we have a database.
1016.22 -> So also here you can see. So this
1019.68 -> is a Docker compose for our app PG admin,
1023.69 -> and then we have Postgres and then within the volumes.
1026.78 -> So here if I Scroll down, you should see that we have spring
1030.42 -> boot admin as well as Postgres. So this is good stuff.
1034.58 -> Now let's go ahead and open up this
1038.45 -> database tab in here. And if I lapse
1042.18 -> this, I want to open up this database
1045.99 -> tab and then new data source and
1049.84 -> then choose Postgres. And then here we can fill
1053.46 -> in all of this information. Now let me just go back
1056.61 -> for a second. And what I want to show you is within this Docker compose.
1060.6 -> Have a look. We have Postgres user and then
1064 -> the password and then we also have the Port. So 5432
1068.28 -> from the host maps t05432 inside of the
1071.87 -> container. So let's just go and create your data
1075.41 -> source Postgres. And then here
1078.96 -> let's select database. Just leave
1082.83 -> as Postgres for now. And in here local
1086.55 -> host 5432. The user is Amigos
1090.39 -> code and then the password is password.
1094.94 -> And then let's just test the connection and
1099.12 -> there we go. Succeeded. Apply. Okay.
1103.3 -> And we are inside. So this is nice.
1106.83 -> So also if you want to use SQL,
1110.27 -> you can actually do it. So here if I
1114.77 -> clear the screen and then I can type psql and
1118.75 -> I can say for host local and
1123.32 -> a host. And then I think between you Amigos code,
1127.38 -> enter and then the password is password and
1132.56 -> I think I got it wrong. So let's just try again.
1138.36 -> There we go. So I'm inside and yeah.
1141.64 -> So you can see that this is awesome. So basically I can use all
1145.45 -> of these commands in here and you want to learn more about Postgres.
1149.35 -> I've got a course about 4 hours long for
1153.22 -> free teaching you about Postgres. But L
1156.97 -> gives me all the databases and we have Amigos code and Postgres
1161.03 -> in here. So this is cool. Now you saw that I've used psql
1165.29 -> and I've also used this database icon in here, but if
1169.31 -> you don't have neither. So in here. Luckily for you,
1173.26 -> what I was telling you is that within this Docker compose,
1177.46 -> we do have PG admin. So here if I open up
1181.43 -> my browser and then type local host 50 50
1185.58 -> just like that, enter and there we go. If you
1189.37 -> see that we have to set a master password the
1193.33 -> first time we use it. So I'm going to say password. I just want to
1197.17 -> keep this simple. Okay. And then you can basically go and
1201.07 -> add a new server and then connect to your database.
1204.66 -> So also you can write your SQL so on and so forth.
1208.75 -> So in my case, I'm not going to use PG admin because I
1212.62 -> already have this database client in here, which is
1216.45 -> really nice. There we go. So now
1220.12 -> we have a database that we can connect
1223.6 -> to. Next. Let's write some Java code that will allow us
1227.14 -> to connect to our database.
1235.56 -> Okey dokey. So we have the database
1239.39 -> app running. Now let's basically learn how we
1242.63 -> can connect to our database from our spring
1247.04 -> boot back end. So we need to create a data source
1250.65 -> essentially. And also we have to define some configuration file for
1254.29 -> how to access the database. So the URL username password
1258.23 -> tones or four. So let's go back to IntelliJ
1262.07 -> and in here let me just collapse this for a on
1266.3 -> and here I'm going to expand project. And what
1270.05 -> I'm going to do first is to create a package.
1273.97 -> So inside of Com Amigos code, let's create a
1277.4 -> package and we're going to name this as config.
1280.45 -> There we go. And inside of config, let's create a new class
1284.38 -> and name this as data source
1288.91 -> and then config just like that. Now annotate
1292.83 -> this with add configuration. And inside what we need
1296.86 -> is we need to say public. And then here
1300.29 -> let's say a carry carry and
1304.13 -> then data source. And I'm going to say a carry data source.
1307.99 -> And this is basically the recommended data source that Spring book says
1311.93 -> that we should use. So the way that
1315.26 -> we use this is by saying return and
1319.19 -> then here I'm going to say data source and then builder.
1322.87 -> And then here I'm going to say dot and then create.
1326.65 -> And also we need to say dot and then type and
1330.89 -> the type is Akari DataSource dot and
1334.85 -> then class and then dot and then build.
1339.02 -> So there we go. Now. One thing
1342.75 -> that we need to do here is here let's annotate
1346.08 -> this with at and then we're going to say bean.
1350.19 -> I'm also going to say that this is the primary and I'm also
1353.97 -> going to say that at and then configuration properties.
1357.83 -> And then inside I can pass the prefix.
1361.21 -> So just like that and the prefix will
1364.9 -> be. So if I open up application Yamo
1368.88 -> have a look. We have app data source main.
1372.9 -> Right. So this is the prefix. So app
1376.77 -> data source and main.
1380.23 -> And there we go. Now the reason why I have a primary
1384.36 -> here is because we can actually have multiple data
1388.7 -> sources. Right. So you can imagine you can have one data source that connects to
1392.26 -> a different Postgres instance. Right. So here I'm actually
1396.49 -> saying that this is the main one. Then for configuration
1400.81 -> properties. So this code right here expects
1404.43 -> some configuration, and I'm taking the configuration from
1408.72 -> the application of Yamo in here. And this
1413.55 -> is the key and then the value data source.
1417.28 -> And then this also isn't nested. Right. So this is
1420.73 -> the syntax. And then basically I've set app data
1424.81 -> source main. And then I've got all of these properties and you can see the
1428.53 -> driver class name, the JDBC URL, the password
1432.48 -> pull size 30 and then the username.
1435.79 -> So let me just swap these around. So username and password
1440.4 -> just like that. So I think it's just easier.
1443.34 -> And then you see that here we have the database
1447.19 -> that we want to connect to. Now for you if you don't have
1450.97 -> the database called Amigos code. So here if I
1454.15 -> click on database in here and in here, you can
1457.51 -> see one of two. And if I click on that, you can
1460.92 -> see that we have. So the default is Postgres. And then I have Amigos
1464.61 -> code in here. So you can connect to the
1469.14 -> database that it has created with your username or Postgres,
1473.07 -> or you can even create your own database. And then let me close this
1476.76 -> once more. And to be honest, this is everything
1481.18 -> we need in order to configure the data source.
1484.85 -> Now, at this point, I don't want to run the application because it's
1489.06 -> not ready yet. So we still have to add the
1492.33 -> SQL to create the table for our application.
1495.75 -> If you have any questions, please do let me know. Otherwise,
1498.98 -> join me in the next one.
1506.38 -> All right. So we have the data source.
1509.45 -> And this also gives us the connection pool. And I think it was 30
1513.22 -> connections that we initially set. And now what
1516.74 -> we need to do is to use Flyway to apply our database
1521.11 -> migrations. So here we can actually use Flyway or Liquid Base.
1524.69 -> So these are the most popular ones. But I've used Flyway in the past and
1528.59 -> I'm quite comfortable with it. So I'm just going to use Flyway. But you
1532.07 -> could also use Liquid Base now to
1535.34 -> use Flyway as follows. So here if I
1539.1 -> go back to IntelliJ, I said before that we have
1542.44 -> this folder. So DB dot migration. So you need
1546.14 -> to have this folder right here. So DB and
1549.71 -> then this is actually a nested folder. Right. So you have a folder
1553.19 -> called DB and then the inside you have another folder called Migration.
1557.59 -> Now I already have this for you right here. So V one
1561.31 -> underscore underscore in tables SQL.
1564.79 -> So if you're not familiar with Flyway, and how it works. Please let
1568.37 -> me know. And I can have a video dedicated just
1571.85 -> on flyway. But in a nutshell right here. This is their documentation.
1576.81 -> And if I click on getting started,
1579.11 -> I want to go to documentation actually.
1582.76 -> And then I think they have so
1586.33 -> right here. So how it works.
1588.53 -> And you can see that you basically
1591.83 -> start with a shiny database which is
1595.31 -> empty and then you have fly away. And then you apply all of these
1599.42 -> migrations into it. And then you have a database with a
1603.24 -> single empty table called Flyway schema history.
1607.18 -> And basically this contains the history of migrations.
1610.54 -> So you can see as your database or application evolves
1614.7 -> with time, you can keep on applying new changes
1619.02 -> to your database and fly away will keep
1622.5 -> track of it. So here you can see example.
1625.99 -> So initial setup and then you have version one,
1629.32 -> version two, so on and so forth. Again, if you're going to learn
1632.89 -> more about Flyaway, please do let me know. And I can have
1637.19 -> a video on Fly way. So what I want to show you is in migrations.
1641.58 -> I think they have yes. So in here they have this
1645.87 -> example here. And this is how you should basically
1650.01 -> name your migrations. So each version must
1653.86 -> be assigned a unique version. And if I Scroll down,
1656.95 -> I think they have an example here. So have a look SQL based
1660.46 -> migrations. So we kind of have to start with
1664.6 -> V and then the version and then underscore underscore
1668.25 -> the separator and then the description. This could be anything
1671.82 -> you want and SQL.
1675.12 -> If you want to undo, you can also undo by just saying you
1679.04 -> and then the version. And then if you want to have a repeatable
1682.62 -> migration, you can also do it. So I already have within
1686.54 -> IntelliJ inside of DB migration have
1690.09 -> a look V one and the score and the score in it
1693.38 -> and then tables. So let's click on it.
1696.96 -> And now we can type some SQL. So here
1700.85 -> let's just say create and then table. And then I'm going to say movie.
1704.65 -> And within this movie table, I want to have an ID
1708.67 -> and this will be big serial primary key. And this is so
1712.27 -> that this number auto increments. Also let me
1715.79 -> change this from generic SQL two and then Postgres just
1721.26 -> like that. And this is now highlighted. So I also
1725 -> want to have name. So this is the name of the movie.
1728.42 -> This will be text not. And then now this also
1732.97 -> have the release and the score date.
1736.3 -> And this will be date not. And then no.
1740.11 -> And finally I want to have the name to be unique.
1743.57 -> So I'm going to say unique. And then here I'm going to
1746.72 -> have name. All right. Nice and simple. And let's
1750.31 -> now end this with semi column. And there we go.
1753.82 -> Now. One thing that you need to understand with Fly
1757.94 -> Away is once this version is
1761.4 -> applied to your database. You should never
1765.4 -> edit this and I'll show you what
1769.36 -> I mean. But if I run the application and then this is
1773.05 -> successfully applied to our database. If you want to add
1776.84 -> a new table, you need to create a new version. So version
1780 -> two and then, for example, actor. So here we
1783.43 -> want to store actors for a movie. I'll show you later how to do it.
1787.14 -> But once this is applied to your database, do not
1791.58 -> edit this file in here. Otherwise you'll get a checksum mismatch.
1795.55 -> So to be honest, this is it.
1799.98 -> Next let's go ahead and run the application.
1808.22 -> We have the database app running.
1812 -> We have the data source right here. And we also
1816.4 -> configured our first migration, which creates
1819.74 -> a new table called Movie with the following
1824.24 -> column. So ID name, release name and the
1827.73 -> movie name must be unique. Now let's go ahead and basically
1832.36 -> start the application and see whether this works.
1835.49 -> So far we didn't start the application and that's because if we were
1839.12 -> to try to start the application, it would have failed because it would
1842.78 -> have expected the data source already existed.
1845.69 -> So now we have everything in place. So open up
1849.28 -> the demo application in here. And this
1852.92 -> contains the main function for running the Java
1857.49 -> application. So let's just click on Play and
1861.69 -> then run. And now
1865.43 -> if everything goes well, you can see that it worked.
1869.41 -> So let me just collapse this right here. And there
1872.73 -> we go. You see that it worked. And in here
1876 -> what we have is have a look. So I just want to show you the
1879.06 -> logs. So we have.
1882.64 -> So this is the starting application.
1885.59 -> And then in here we have both trapping spring data
1889.93 -> JDBC and then have a look. So Tom Cat
1893.16 -> initialize it for 80 80 and then flyway
1896.97 -> kicked in. And in the meantime, the car
1901.78 -> repool started completed. And then here we have So
1906.28 -> database and then Amigos code right here and
1910.43 -> then successfully validated one migration and you
1914.02 -> can see how long it took. So this was really fast. And also it
1917.65 -> created a new table called Flow a Schema
1921.74 -> history, and then we have zero was affected, and then current version
1925.62 -> it was an empty schema and then migrated to version
1929.94 -> one in it tables. Right. And then zero is affected
1933.81 -> as well. And then successfully applied one migration to Schema
1937.68 -> public. Now at version one.
1940.67 -> And also we can see that some cat started on at
1944.43 -> 80 right here. Which means that we are good to go.
1947.73 -> Now what I want to show you is if I open up the database in
1951.46 -> here and if I open a public so
1955.18 -> before we didn't have anything. So now if I refresh,
1958.42 -> we should have two tables in it and
1962.63 -> one sequence. So this sequence is 4D. So in
1967.26 -> here. So if I open up this V one in it tables is
1970.94 -> this ID big serial. So this gives us a sequence
1974.68 -> and then we have the movie table with three columns in
1978.72 -> here. So ID name and release date. And we also have
1983 -> the flyway Schema history. And this contains a bunch of information about
1987.04 -> the Schema history. So for now, we don't have to worry about this now.
1991.24 -> This is awesome. So what I was telling you is so
1994.87 -> let's say that you want to edit this file in here. Right. So let's say
1998.11 -> that you want to say this is, for example, movie name,
2001.97 -> for example, or let's just say names. Yeah. Just try to
2005.73 -> edit this file. Now, if I rerun the application,
2009.36 -> this will fail. Have a look.
2013.82 -> So migration checksum mismatch
2017.25 -> for migration version one. Either Revere the changes
2020.76 -> to migration or run a repair to update the Schema
2024.39 -> history. And this is because there is already a checksum against
2027.76 -> this SQL right here.
2030.72 -> And as I said, if you want to add this,
2033.84 -> you should really be creating a new file version
2037.66 -> two and then make the changes there. Add new tables
2041.2 -> on and so forth. As soon as it's applied, you cannot change it
2044.88 -> unless it's a repeatable migration. But this
2048.41 -> isn't a repeatable migration. Now let's go ahead. And what
2052.37 -> I want to do is if I open up the more controller and
2056.39 -> in here you can see that we can basically
2059.64 -> test whether this is working. So here you can see that we have
2062.79 -> this list movies and it's under API V
2066.71 -> one and then movies. Now let me go ahead and open a postman because
2070.38 -> not everybody should have the Ultimate Edition for IntelliJ.
2075.38 -> And in here I do have local host API
2079.44 -> view on movies. Or you can basically create a new request and
2083.56 -> you can say the URL. So here let me just send
2087.04 -> this request and then send and this doesn't
2090.94 -> work. And that's because we need to start the server. So let's just
2094.3 -> start the server and
2099.66 -> then send the request. And in here we should get not implemented
2104.39 -> right here. So this is the 500, which means that
2107.66 -> is correct. So in here have a look inside
2112.25 -> of the Movie data access service. We said
2116.17 -> unsupported exception not implemented.
2119.56 -> So we actually have to basically implement all
2123.21 -> of this. So hopefully you managed to get this far.
2126.94 -> If you have any questions, please let me know. Next.
2130.42 -> Let's go ahead and learn how to use JDBC
2135.37 -> and more specifically, JDBC template, so that
2138.71 -> we can basically run queries against our
2142.96 -> database. So from this point, we actually have everything set
2146.51 -> up. Next. Let's go ahead and then how to use JDBC template.
2156.72 -> As I said, JDBC is the
2160.27 -> Java database connectivity programming interface
2163.83 -> for defining how client access databases.
2168.52 -> Back in the days we used to use Raw JDBC.
2172.84 -> And here inside of the Oracle documentation,
2176.13 -> this is the official one where you can pretty much learn how to use
2180.28 -> Plane JDBC. So establishing a connection handling exceptions,
2184.6 -> setting up tables using the result
2188.57 -> set join result sets filtered.
2192.16 -> Basically, you can learn everything about JDBC in this
2196.3 -> page right here. So, for example, establishing a connection,
2199.82 -> you can see that you need to write a bunch of
2203.51 -> code in here. Right. So a connection. And then you get the properties,
2207.37 -> you get the username and password,
2210.79 -> and then you can basically connect using the driver manager,
2214.31 -> so on and so forth. But you can see that with spring boot,
2218.08 -> it was super easy. Right. So we just gave it the properties
2222.29 -> file, and then we have the data source, which was a couple of
2226.04 -> lines. And off we go. So now let's
2230 -> learn how to use JDBC template. So JDBC template
2233.64 -> is a wrapper, and it simplifies the use of JDBC,
2236.98 -> avoiding common errors. Let's go and open up IntelliJ
2241.16 -> and in here what I want to do is so let's
2245.16 -> open up project and inside of the config and then data
2249.52 -> source. What I want to do is let's have. So here
2253.59 -> I'm going to say at and then Bean and
2257.26 -> then here I'm going to say public and then JDBC and
2262.22 -> then template. And I'm going to say JDBC template.
2265.96 -> And in here what I want to do is so
2269.79 -> I'm just going to say return new JDBC
2274.26 -> and then template. Now the reason why
2277.5 -> I'm doing this. The reason why I'm doing this is
2280.73 -> because within the JDBC template, if I press command P,
2284.84 -> we can pass a data source, we can
2289.64 -> pass a data source. So here I
2293.66 -> could actually say, okay. So here I'm going to say care and
2298.08 -> then data source and an ear data source.
2301.76 -> And inside I can pause the ICAR
2305.07 -> data source. Right. So this is this
2309.24 -> data source and this is the primary one.
2312.17 -> Now again, the reason why I'm doing this is
2315.75 -> because I told you that we can have multiple
2319.27 -> data sources. Right. So if you want to have multiple
2322.53 -> JDBC acting on different data sources,
2326.12 -> this is how you do it. So now that we have the
2329.81 -> JDBC template, we can go in here and
2333.56 -> inside of the movie data access. So this is where
2337.37 -> we implement do. So movie
2341.42 -> do. We can say private and then final JDBC
2346.58 -> template JDBC template.
2349.09 -> And let's add this to constructor. And this will be
2353.44 -> this will be injected for. So this is using dependence
2357.23 -> injection. And from this point onwards, we can
2360.89 -> basically now run SQL queries against our database.
2365.24 -> So let me just restart this to make sure that things
2369.49 -> will work. And there
2373.72 -> we go. As expected, things still fine.
2377.07 -> So next, let's go ahead and learn
2380.97 -> how to insert a new movie into our database.
2391.32 -> So I just want to mention that JDBC template
2394.96 -> is one of many JDBC wrappers out there.
2398.52 -> So the reason why I'm teaching you JDBC template is because it comes out
2402.31 -> of the box with spring boot. And yeah, it just
2405.85 -> simplifies everything. But you've got others such as JD,
2410.46 -> I I think or DBI three, something like that.
2414.06 -> But there are plenty of others out there that helps
2417.22 -> you to do the exact same thing. So yeah,
2421 -> let me know if you've used any others in the past.
2424.53 -> I'll be curious to know.
2431.88 -> So from this point onwards, we're just going to
2435.58 -> use JDBC template. And in order for
2439.15 -> us to insert a new movie, let's go ahead and basically remove
2443.45 -> this. And anytime that you want to modify
2447.28 -> data in your database is as
2450.88 -> follows. So here I say JDBC template,
2454.61 -> and then we need to use update in
2458.73 -> here. Update and update because
2462.74 -> we want to add a new row into our table
2466.46 -> called movie. So if I delete this and then press control
2470.51 -> space, you can see that we have a bunch of other methods here. So have
2474.75 -> a look. We have update. We prepare a statement with
2479.6 -> orgs, we have execute query, so on
2483.88 -> and so forth. So I have a bunch of these methods and I'm going to
2486.33 -> show you how they work. But anytime that you want to insert a
2490.96 -> new record into your database, you say update.
2493.5 -> Now the update takes the SQL
2497.67 -> and then a list of arguments. So here let's together
2501.83 -> type SQL. And what I want to do first is
2506.22 -> so let's just say SQL in here and
2509.97 -> let's have so basically this returns the
2515.47 -> number of rows affected. So this is why we have an integer here.
2519.7 -> Now let's have the string and then SQL
2524.24 -> equals two and then three double quotes.
2528.16 -> And in here I can end this with semi column
2532.03 -> and an indent is just like that. Now the
2535.64 -> SQL is. So let's type together insert
2540.33 -> into and then movie. So you can see that I actually
2543.46 -> have auto completion there. So movie. And then inside
2547.47 -> what I want to add is the name release date.
2551.46 -> So I actually let me just say values
2555.57 -> here and then these parentheses.
2560.04 -> There we go. And what
2563.17 -> I want to show you is that I don't need to add the ID because
2566.62 -> this is order generated for us. So insert
2570.62 -> into movie and the name and then release
2573.77 -> date. Now here let's pass the
2577.81 -> question Mark twice. And this is because the update
2581.59 -> takes a list of. So here our first command P a
2586.2 -> list of so here we want movie
2590.43 -> dot and then name and then
2594.26 -> movie dot and then release date.
2597.44 -> So these are the arguments
2601.17 -> that we are passing. So let me just put this like this. There we go.
2605.5 -> So you can see properly.
2609.6 -> And also I like to invent my SQL just
2613.85 -> like this. And hopefully if I press yes.
2618.58 -> So if I invent this or format, you can see that this is nicely
2621.89 -> formatted now. So we have insert into movie values.
2626.08 -> Question Mark, question Mark. This belongs to the
2630.08 -> first question Mark. This belongs to the second question Mark,
2633.7 -> and this is the SQL itself. And then
2637.54 -> we actually returning the result. So this returns
2640.86 -> the number of rows affected. Now let's restart
2645.43 -> the application and let's try to add a new movie
2648.97 -> with postman. So here let me open a postman
2653.28 -> and let's try to send a post
2656.5 -> request. So here we're going to say we want
2660.1 -> to have a post request and then in the body.
2664.76 -> In here select draw and then text JSON.
2669.8 -> Blob. And here this has to be a JSON.
2673.23 -> Blob. So here we need to send the name
2677.64 -> and let's see that this is Avengers.
2683.4 -> And then we also need to pass the release date.
2687.29 -> So release and then date
2691.66 -> and this will be let's say 2020
2695.99 -> and then zero three and
2700.24 -> then three. So the date doesn't really matter. So you can
2703.66 -> pick any date. But this is the year month and then date.
2707.34 -> So let me just say 20. So it makes more sense.
2710.44 -> So now let's try and send this request to our
2714.18 -> server and see what happens. So if I send this request,
2718.33 -> have a look. We have a 200 status code.
2721.86 -> So this is awesome. Now what I want to do is send
2726.05 -> a get request. So let's send a get request. And hopefully
2729.87 -> we should see that we have a list of movies.
2732.99 -> So if I send and it's
2737.57 -> not implemented. So it's fine. Right. So we haven't implemented this,
2740.47 -> which makes sense. So now if I go back to IntelliJ and in
2744.43 -> here let's open up the database and
2748.91 -> you can see that we have the movie. And in fact, I have
2753.42 -> this console right here. So let me say introspect
2757.15 -> database. And here I can say select store
2764.83 -> and then from and then movie.
2768.12 -> Let's run this by clicking on the play button.
2771.6 -> We have Avengers. So this is nice. Let's add another
2775.42 -> movie. So post and in
2779.03 -> here body and then let's say Spider and
2782.79 -> then Man. And let's say this was
2786.6 -> this year 2021 send.
2790.12 -> And now if I run this execute
2795.13 -> we have two movies. So this is cool. So we actually are
2799.43 -> storing data in our database and you can see
2803.03 -> how easy that was with JDBC template.
2807.44 -> Next, let's learn how to select data from our tables.
2817.38 -> So you saw before. If we try
2821.2 -> to get all movies we have not implemented.
2825.16 -> And this is because right here. So select all
2829.1 -> movies. It's not implemented. So in order for
2832.39 -> us to use JDBC template to get data
2836.34 -> from our database, we need to use.
2839.79 -> So in here we have JDBC. So JDBC
2844.54 -> template dot and then we have query.
2848.8 -> So the query is really interesting.
2852.2 -> It takes the SQL, but also it
2855.53 -> takes the row Mapper. Now in here, if I select
2860.08 -> the first one, you can see that there are a bunch of other different variations,
2864.09 -> but most of the time you're just going to use the SQL
2868.01 -> and the row Mapper. If you also want
2871.75 -> to pass for example arguments, you can't do it.
2875.01 -> But in here let's just say SQL.
2878.34 -> So again, we're going to create this in a second and we
2882.07 -> need a row Mapper. So here let's just say
2886.35 -> string or we can even say VAR.
2890.32 -> Sql equals two.
2893.55 -> And basically
2896.98 -> I want three of these. And this would save me column.
2900.68 -> And let me change this too far as well. There we go. In here.
2904.39 -> We need to pass the row Mapper. So here let
2908.2 -> me just press control space and I'm going to
2911.41 -> select this first one and then here within
2915.93 -> this set of curly brackets. We're going to add some code
2919 -> in a second. But here let's go ahead and say
2922.3 -> return. And then this will return the query.
2925.98 -> Currently it's red. But we're going to fix this in a second.
2929.52 -> So now we need to pass the SQL that we want.
2932.7 -> We can say select and then we
2937.2 -> want ID name and then release
2941.15 -> and this core date and then from
2945.42 -> date and then from
2949.3 -> and the movies. And it's always good
2953.1 -> practice to have a limit in here because
2957.09 -> if you have 1 million movies, you don't want to fetch all 1
2961.195 -> million movies. You kind of want to have pagination. So here let's just say
2964.73 -> that we want to fetch 100 movies and there
2968.45 -> we go. So let me see if I can intend. This indentation
2972.93 -> is key for me. I like to have my code indented as
2976.94 -> much as possible. And we're good to go.
2980.74 -> So now that we have this SQL, let me also
2984.02 -> end this with same. I called here. So we have the SQL and
2989.07 -> this will give us where is the database
2993.06 -> two in here. So let's just click on console.
2996.47 -> So let's just run this. So this will give this ID name
3000.76 -> and release state. Now we need to take each of these rows and
3005.32 -> then map them into a Java class. So this is what it's known as the
3009.82 -> road Mapper. So here if I collapse this
3013.62 -> and the row Mapper works as follows.
3016.63 -> So here I'm going to say return and then we're going
3020.2 -> to return new and then movie.
3023.77 -> And then inside we need to pause the ID. So how
3027.59 -> do we get the ID? We say result set dot,
3031.75 -> get. And you can see that we can say get
3036.3 -> the column and the column is ID just
3040.71 -> like that. Then we also need to get the name. So here comma
3046.47 -> and let me just put this on a new line.
3049.19 -> Comma and then basically result set dot
3054.08 -> and then get and then string.
3057.12 -> And basically I'm just getting the data type
3060.36 -> for each of those. Right. And then this is name.
3063.52 -> And in here we also have.
3066.44 -> So let me just press command P. We have the
3070.13 -> list of actives. So for now I'm just going to send no there
3076.81 -> because we don't have anything. And in the release date. So I'm
3080.51 -> going to say local date dot
3084.45 -> and then parse and then D string.
3088.63 -> So the string comes from result set dot
3093.12 -> and then get and then string. The column
3096.57 -> label is release and then date.
3100.67 -> And to be honest, this is it. So now if I end
3104.08 -> this with semi column, you can see that the error went away and
3108.37 -> I can see that this is actually incorrect. They should be movie.
3111.72 -> And if I then this you can
3114.97 -> see that I can actually in line. So this right here. So this
3119.2 -> entire thing returns. So if I extract this to a variable returns
3123.84 -> a list of movies. Right. So here if I see movies,
3127.4 -> you can see that this returns a list of movie.
3131.48 -> Now one thing that you could do is you could
3135.01 -> actually take this role Mapper and put it on its own class.
3138.71 -> So here let's just create a new class. I'm going to say movie
3144.72 -> row Mapper and
3148.46 -> then implement row and then
3152.42 -> Mapper and the type of it will be movie.
3156.18 -> And then let's implement the methods. Let's go
3159.79 -> back here. Let's take this. So we're
3163.75 -> going to take this command C and then
3167.38 -> paste that in and you can see that this.
3170.68 -> Now it's much cleaner. Right. Which means that if
3174.01 -> I go back to move data
3177.14 -> access service instead of passing this here.
3180.68 -> Right. Instead of passing all of this like that,
3184.56 -> I can say new and the movie
3188.16 -> row Mapper. And the cool thing here is that we can reuse this road
3192.31 -> Mapper, whatever we need within this class and
3195.49 -> elsewhere. So also let me in line this and
3199.56 -> there we go. So you see that this is much cleaner. Now let's
3203.9 -> restart the application.
3207.92 -> And hopefully this should work. So let's go back to
3211.51 -> postman. And before we had a 500. Let's now
3214.72 -> send a request and check this
3218.16 -> out. So this is awesome. So you can see that we
3221.99 -> are getting the move is that we added previously in
3228.66 -> our database. So here we have a list of
3232.21 -> two movies in the Ray of two movies. So two objects right
3235.39 -> here. And this is working beautifully. So also
3240.55 -> what we could do is within the row Mapper. So let's just go
3244.31 -> to the row Mapper here. Let's just say list and
3248.28 -> then and then off. So an empty list.
3251.78 -> So let's just run this and
3255.16 -> then send it and you can see that we have an empty listing here.
3258.61 -> And obviously you want to basically get the list of actors.
3262.23 -> But this will be part of the exercise later.
3265.23 -> So if you have any questions on road mappers and how
3268.75 -> they work, please do. Let me know. But you can see that
3272.05 -> it's basically just a way of us getting access to
3275.54 -> the result set and then constructing the row.
3279.85 -> So the SQL row into a Java
3283.42 -> class and we're just getting so get it.
3286.69 -> This is because the ID type is an integer string
3290.3 -> for the name, an empty list
3294.27 -> here for the actors. And then we parse the local date
3297.74 -> in here and we get it as a string. So if
3301.7 -> you have any questions on this, please let me know. Otherwise.
3304.85 -> Let's move on.
3312.89 -> Okay. Okay. So our code is taking
3316.03 -> shape. Now let's implement the so in
3319.54 -> here we have. So let's implement select movie by ID,
3323.64 -> which should be very similar to what we've done before. So in
3328.37 -> here let's just so this returns an optional.
3331.95 -> And what we need to do is say JDBC
3335.08 -> template. And then in here we
3338.51 -> can have so we can say query. We can get a list
3342.07 -> if we want. We can also so in here we
3345.92 -> can say query. So query for
3349.18 -> object in here. So this will give us actually an
3352.81 -> object that weekend return.
3356.22 -> Now often people will
3360.42 -> use this query for object or the
3364.38 -> query which returns a list. And the reason is so if I
3367.92 -> select query for object and then go inside in
3371.5 -> here, let's just select the first one which takes the
3375.25 -> SQL and the road Mapper. And in here
3378.43 -> I want to show you the implementation and that is that
3381.91 -> this right here. You can see that it gives you
3385.6 -> basically a list. Right. So this gives you
3389.25 -> a list in here. And yeah.
3393.09 -> So basically, in a nutshell, it's actually using the query and
3396.81 -> then have a look data access exception. So it will
3400.86 -> throw that exception as well. And then here it says nullable
3405.51 -> Singleton list. And basically what it's doing is
3408.68 -> so if it's empty, it returns empty result
3412.72 -> data access exception. If it's bigger than
3416.11 -> one, then it says incorrect result data
3421.05 -> access. Otherwise it gives you the next element inside.
3424.46 -> And you can see that it throws this exception in here.
3429.1 -> So what we can do is since we know that
3432.77 -> it's using query. So what we can do is
3436.5 -> we can just use query. So we can say query.
3439.98 -> And in here let's just steal some
3443.74 -> code. So we're going to basically
3447.04 -> take this SQL in here.
3451.12 -> And then I'm going to paste it here.
3453.96 -> And the difference is that instead of saying limit,
3457.86 -> I'm going to say where and then
3461.74 -> ID equals. And then to question
3465.72 -> Mark. Now here we're going to pass the SQL.
3469.69 -> And then here we're going to pass the due and then movie
3473.95 -> row Mapper. So you see that now we can reuse that.
3477.85 -> And then also we want to pause the ID as the argument.
3481.61 -> So the ID will actually be filled. So it will
3485.02 -> be filled in here. So we'll be posting here.
3489.03 -> So this now returns a list.
3491.98 -> Right. So here I'm going to say movies. So now
3496.37 -> that we have this list, what we could do is we could say movies,
3499.84 -> dot and stream dot and then find
3503.89 -> and then first just like that.
3507.28 -> So this returns an optional. There we go. And I can say
3511.54 -> return and then basically the
3515.45 -> optional. So this returns an option right here. So what
3519.2 -> I like to do always is to invent this. So I just want
3522.47 -> to show you how the syntax looks. But then I like to invent
3526.73 -> this so just like that. So this looks much
3531.02 -> neater and there we go. So basically
3536.13 -> if it exists, then it gives us the movie.
3540.02 -> Otherwise it will be an empty optional
3543.68 -> right here. Which then if I go to the service.
3548.14 -> So movie service in here movie service.
3552.2 -> So the logic is so get movie if
3556.79 -> exists. Give the movie. Otherwise throw not found exception.
3561.12 -> Right. So let's restart the server and
3564.83 -> see what happens in here.
3567.76 -> So there we go up and running open a postman here
3572.31 -> we have the movie ID of one. Send.
3576.06 -> This works. We also have the movie
3579.5 -> ID of two. This works if I say
3583.19 -> three have a look four
3586.82 -> or four not found. So this is really
3590.72 -> cool. And you see that we didn't even use so
3594.14 -> in here we didn't even use query for object because
3598.64 -> we know that it uses a list.
3601.9 -> Right. And we can just stream and then find first
3605.66 -> and this will return an optional with the movie
3609.29 -> or an empty optional. Next, let's go ahead and
3612.59 -> implement the final method here, which is to delete
3616.1 -> a movie.
3623.12 -> So in order for us to delete a movie,
3626.6 -> let's use again JDBC template. So JDBC
3632.01 -> template dot and then query is
3636.43 -> for us to get a data from our table.
3639.44 -> And remember I said that if we want to modify we
3643.49 -> have to say update in here. So let's just say
3647.04 -> update. And here we're going to pause the SQL
3651.5 -> and we can also pass some.
3654.28 -> Right. So the R in here will be ID.
3658.09 -> There we go. And in here let's just
3661.74 -> have the VAR and then SQL equals
3666.63 -> two. And inside here let's
3670.86 -> just have the delete and
3675.33 -> then from and then movie
3679.5 -> on the new line where and then
3683.16 -> movie or actually ID. So where ID equals
3688.27 -> to and then question Mark. And it's as simple as this.
3691.93 -> So here this returns the number of
3695.41 -> rows affected. I can just say return and then JDBC
3699.35 -> template update. Now if I restart
3702.95 -> this and let's open a postman.
3706.46 -> So let's basically get here
3710.24 -> we're going to get we have two
3714.28 -> movies. Let's now delete movie movie
3718.54 -> and then two and then change this from get to delete
3723.69 -> and then send have
3727.13 -> a look 200. This work. If we
3730.69 -> get again we should have
3734.12 -> only one movie which is Avengers. Now if I try to
3737.96 -> delete the movie. So I think it was Spiderman. If I try to delete
3742.44 -> it, we have a four four movie with ID not
3746.3 -> found. So this is cool. Right. And this is happening because in
3750.66 -> here if I show you the movie service.
3753.92 -> So where with the lead in here. So we actually
3757.83 -> select the mode by ID. If present, then we can delete.
3762.3 -> Otherwise we just throw a note found exception.
3765.92 -> So this is awesome. Now let me go back in here
3769.54 -> and let's just delete one again.
3773.24 -> So basically we want to delete everything. Let's try and delete everything.
3776.93 -> So in here one delete.
3782.57 -> And now if I get to the movies, it's empty.
3786.56 -> Let's post in here. So I think we did post Spiderman.
3792.2 -> Let's also post. I think it was
3796.77 -> that was Spiderman. This was Spiderman.
3800.25 -> And this was yes
3804.06 -> by the man. Okay, we can change this to Avengers.
3810.05 -> There we go. And Oops
3815.05 -> Avengers. There we go. Send this.
3818.7 -> We have Avengers. Let's also say F nine four fast
3822.12 -> nine send. And if I get
3825.74 -> in here send, we should have three movies. Now have
3829.63 -> a look, Spiderman, Avengers and F nine. So there
3833.76 -> you have it. We successfully managed to implement this
3839.58 -> interface right here, which had these
3844.94 -> four methods. Select insert the lead and select movie
3848.55 -> buy. So we kind of have the cred
3852.21 -> without the update.
3855.38 -> But this is part of the exercise.
3858.29 -> If you have any questions for what we've done,
3861.67 -> we delete, which should be really straightforward.
3865.74 -> Please do let me know. Otherwise catch me on the next one.
3875.98 -> So before we end up this video,
3879.32 -> what I need to do is for you to go and try
3883.07 -> and attempt this exercise. So I want you
3886.67 -> to try and add the ability to edit
3890.35 -> movies. So that's one part of the exercise. And also
3893.93 -> I want you to add the actor table. So you saw that
3897.56 -> we have an actor record or an actor class,
3901.24 -> and I want you to basically associate them with movies.
3905.17 -> So for that, you need to create a new migration called V two, and then
3909.35 -> just say, for example, actor table SQL. And then here you
3913.1 -> can just grab the SQL and then just
3917.06 -> paste it. And then it should have a new table and then go and
3920.75 -> give that a go and also change the code so that you can have a
3924.55 -> joint. Right. So when, for example, you fetch a movie,
3928.51 -> you should also fetch all of the actors for that
3931.76 -> particular movie. I'm going to be leaving the solution very
3935.85 -> soon so you can basically see the way I've done it. But there is
3939.26 -> no on one specific way of doing things because
3943.98 -> at the end of the day, the way I think it's completely different
3947.53 -> than the way you think and the way you do things. Right. So go
3951.43 -> ahead and challenge yourself and also go ahead and
3954.88 -> join the community. So discord and both Facebook group.
3958.38 -> So basically there's always people online ready to
3962.23 -> help you if you have any questions. Next,
3965.86 -> let's wrap up this video.
3973.86 -> So this is everything
3977.99 -> for this video. Thanks for sticking around throughout
3981.43 -> this video. If you enjoyed what you saw, give me a thumbs up so I
3984.61 -> can keep on recording these videos. Also, let me know what you think about this
3989.14 -> video in general. Comment down below any suggestions that
3992.83 -> you may you might have or any
3996.15 -> new videos that you want to see from Amigos code.
3999.66 -> And yeah, let's just connect. So this is
4003.27 -> all for the this video. You can grab all of the source code from
4006.84 -> the repo, as I said. And also, if you're gonna basically get more
4010.83 -> courses like this, basically more professional courses, go ahead
4014.82 -> and check Amigos code. Com. There's plenty of free and paid
4018.83 -> courses that will help you to become a professional software
4022.43 -> engineer. This is all for now. I'll catch you on the next one.
4025.53 -> Assalamualaikum

Source: https://www.youtube.com/watch?v=CJjHdchLY9Y