Detailed Notes
We are continuing a series of tutorials focused on Spring Boot. The crux of the project is to convert/migrate an old JSP/Tomcat application to a modern Spring Boot application and Java 7/8 to a current version. We continue the JDBC temaplte tutorial in this episode with our rowmappers and results extractors.
You can learn more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list, and you will periodically receive coupons for courses and notifications of the latest releases.
Transcript Text
[Music] and okay so I think I want that to be there I don't know why that got changed there but sa update sorry while I do some little C code correction I'll talk while I correct code uh it is obviously K sensitive and obviously that's something we want to deal with so what we're going to end up doing here is for each of our calls or our functions that we put or methods to be technically correct that we put in our interface we have to actually now write the code that does it just as a brief if we you know if you don't know what an interface is an interface is basically like a a cover on a box that said you know that has holes in it for where buttons are supposed to be and so that cover only is going to be useful to boxes that have buttons in the correct places and that's sort of what we have here so an interface says hey this is roughly how you're going to interface this is how you're going to work with this data or this thing and in so doing the interface is going to have certain requirements to it and those are in this case there's methods so it says hey you can integrate your opportunity through this Dao you can integrate it with whatever data source you want so we're going to use a jdbc but it could be things like it could be stored in a file so we could have a a file implementer that says take this and instead of pointing it at the database let's save it in a file or something similar to that and so what this says is it says I don't care where you store it you need to be able to support these features and that's what we do in the implementation which I could go find where I put the Implement and I want to start with so so that's what we're going to be building now the jdbc part that is uh in this case we've got org. springf framework. jdbc doc core jdbc and template that is what we that is like the core of what we need because what we're going to do with the jdbc template is we're going to create a I'm going to create a variable basically to use my jdbc template it is going to to be this is key is I've got or spring spring framework stereotype repository and that is going to be right here I'm going to need this because when I use that rep at repository it says in the system it says Hey create this object go connect and by the connect is hey there's a data source that's going to be sent to it and so the data source is built based on our product properties back here and it's going to grab those properties it's going to basically essentially what it's going to do is it's going to connect to the database Force so now what I can do is I can take this effectively this database connection and I can do stuff with it for example I can say with my jdbc template I can do a query which it doesn't like uh let's see it is deprecated because of uh probably it's because it's an object I believe but we will we will deal with this in a little bit uh let's see oh new object ID new oh because it probably needs a mapper is I think what it wants oh it needs a yeah we'll come back to that that I can suppress it and just say no but since it's being uh config you know this is one of those things we will dig with it we will dig into this in another one because I want to correct this for us because this is an older thing that we did it's an older approach to the jdbc template I digress so what we can do is we can do a query we can give it a uh in this case we're going to give it a r mapper and I'll show what that is and so this is the list um it is a straight SQL query so we're just going to select star where status is basically where it's still there order by this and then we're going to go P that into the opportunity row mapper so we're going to need to create a row mapper row mappers are also fairly simple because what we're going to do is we're going to within our row mapper we're going to take an object our model which in this case is the uh opportunity itself and we're going to create basically just create a new instance and we're going to populate it so we're going to set the ID the title in this case all of the variables or all of the values that are the columns from the database that are also the properties on the class so this is how we map our data from the database into our model and then we also have a result set extractor uh which is another thing we can use because we're basically say hey I'm going to take a result set that I get from SQL and I'm going to go through in this case I'm going through each record in my result set and I am now going to map I'm going to create a mapper and I'm going to pull those values in so it's basically query the database get your result set pass a result set into the uh the extractor which says take the extractor and turn that into a instead of each row being a result set it's going to each row is going to be a uh opportunity row a row mapper it's basically going to say take that data make it something that is more useful to me so instead of a database result set it's going to be a list of rows in memory and that's where I'm going to use the mapper and so that point oh so here we go so I'm going to come through here it says hey give it an opportunity R mapper we're off we are off and ready to go uh let's see so with that and we can get more complicated we can do some pretty complicated uh queries we will talk more about these later uh cuz right now what I would just want to do is I'm going to say hey let's list out the let's see if I can list the data in my table uh in my database right now that means that I'm going to swing back over because I've got my I've got my mod model I've got my Dao so I've got a an interface that says this is how we're going to interact with some store some data store I've got the the Dao imp the implementation class I've got something that actually uses that interface and implements it for in this case Maria DB and I could name that something like opportunity do Dao Implement Maria DB something like that to to really be specific about it I I also have my result set extractor that says hey whatever I get from the database I'm going to map that into memory objects I'm going to with my row mapper I says I say for each row in a result set here's how I'm going to map it and so now I need to go back to my hello controller which is not my rest it's the other where did that go so I go to my hello controller and so now I want to be able to actually put this code together so first thing I need to do is I'm going to actually get an instance of of my opportunity Dao now it's not the implementer I'm going to I've already got that like wired up it's going to say hey this implementation already exists this is the one it's going to take so I'm going to use an autowire again which basically just goes through walks through that code and creates an instance of an opportunity Dao which we're just going to call lowercase o opportunity Dao because then we get to call that and that notice that we don't have to talk about the implement or anything like that we've already got that stuff wired in everything's good because it says hey here's my this is my repository that's where it basically links my Dao impl to the actual Dao uh through the I've got my jdbc template I'm creating connecting everything up now I come over to my hello controller and now that I've got it all hooked up now I can work with it just like any other class so now this is where I can do my list I can do a get because I generated those I could actually delete I can do a few different things and this one let's see I'm just going to leave it like that uh I'm not going to present results like that CU that was a result set now I can now I have here uh add attribute results we've talked about this before where I can actually come in and like we did with name here so for for Greetings uh where's greetings that was all the way up here is I have something very simple is I'm just passing that name in now what I can do is and so here if I run it right now this is one I didn't even pass it a name but I can see here's my you know my opportunities and stuff what I can do is I can change this so let's change this to well um let's change this to be uh here is your opportunity if I can spell it right and we're just going to call this op and let's see what this looks like so we change this up a little bit it's probably not what I want to do I probably don't because I'm using greetings in two places so I probably should let me go look at this real quick Oh wrong one uh get over here so if I go over here and I do my hello controller yes I'm using greetings twice so let's change that let's say um let's roll this back there we go and instead I'm going to duplicate that uh let's do it this way sure um um yeah fine I'll save that this is going to be opportunity and actually I could probably get far more complicated so let me go see I think I have uh I don't know if I've got it all set up let's see how good this works if I go back to my prior list is I had somewhere in here so we're going to take our p page here let's make sure I'm on the right one yep and this is now going to be the we're going to do an opportunity and we're going to do it this way and we're just going to call it op as we're going to pass that in so now this is going to be uh add attribute name actually I'm sorry op equals item right yeah because that's the one that we got so now if we go look at [Music] it oops did I go oh because I didn't change this to change that to opportunity and is it going to the right place let's see so add attribute oh did I get that right add attribute [Music] name going to give the model back did I create my model I did okay so model add attribute is the item let's just do let's see did I get that right he is not going oh because I've got to reset I've got to rebuild it because it was not going to the right place that happens sometimes boom and here's my opportunity so I get this big honking to string because that's what it's going to do it's going to take my U opportunity much as I did down here and somewhere in here you could see it pretty nasty where it does it out to a string now I can clean this up I can put you know name it has as I need to and things like that we will stop there and we're going to come back next time around we'll get a little deeper into this and we're going to look at uh we'll go ahead and like list out our uh opportunities We'll add that in and uh we're just going to keep on chugging along so if you have any questions shoot an email out to info@ developin or.com and uh we'll just you know do our best to keep chugging along here happy to help you out with in any way that we can if you have any questions any struggles let us know know as always go out there and have yourself a great day and a great week we'll talk to you next time hello this is Rob with developing or also known as building better developers wanted to announce that we have school. developer.com feel free to check it out if you like any of this information any of the content that we've sent and you would like to see more you can come out you can enroll for free we have free courses we've got places for you to get better at just learning a technology or how toos you you can work on your business skills we can help you with becoming a better developer as in coding and things like that a lot of the stuff you've seen on YouTube we also have out at school. develop andur we just have it in a little more of a uh educational format and a way for you to track your progress as you move forward becoming a better [Music] developer
Transcript Segments
[Music]
and okay so I think I want that to be
there I don't know why that got changed
there
but sa
update sorry while I do some little C
code correction I'll talk while I
correct code uh it is obviously K
sensitive and obviously that's something
we want to deal with so what we're going
to end up doing here is for each of our
calls or our functions that we put or
methods to be technically correct that
we put in our interface we have to
actually now write the code that does it
just as a brief if we you know if you
don't know what an interface is an
interface is basically like a a cover on
a box that said you know that has holes
in it for where buttons are supposed to
be and so that cover only is going to be
useful to boxes that have buttons in the
correct places and that's sort of what
we have here so an interface says hey
this is roughly how you're going to
interface this is how you're going to
work with this data or this thing and in
so doing the interface is going to have
certain requirements to it and those are
in this case there's methods so it says
hey you can integrate your
opportunity through this
Dao you can integrate it with
whatever data source you want so we're
going to use a
jdbc but it could be things like it
could be stored in a file so we could
have a a file implementer that says take
this and instead of pointing it at the
database let's save it in a
file or something similar to that and so
what this says is it says I don't care
where you store it you need to be able
to support these features and that's
what we do in the implementation which I
could go find where I put the
Implement and I want to start with so
so that's what we're going to be
building now the jdbc
part that
is uh in this case we've got org.
springf framework. jdbc doc core jdbc
and
template that is what we that is like
the core of what we need because what
we're going to do with the jdbc template
is we're going to create a I'm going to
create a variable basically
to use my jdbc template it is going to
to be this is key is I've got or spring
spring framework stereotype
repository and that is going to be right
here I'm going to need this because when
I use that rep at repository it says in
the system it says Hey create this
object go connect and by the connect is
hey there's a data source that's going
to be sent to it and so the data source
is built based on our product properties
back
here and it's going to grab those
properties it's going to basically
essentially what it's going to do is
it's going to connect to the database
Force so now what I can do is I can take
this effectively this database
connection and I can do stuff with it
for example I can say with my jdbc
template I can do a query which it
doesn't
like uh let's
see
it is deprecated because
of uh probably it's because it's an
object I believe but we will we will
deal with this in a little bit uh let's
see oh new object ID new oh because it
probably needs a mapper is I think what
it
wants oh it needs
a yeah we'll come back to that that I
can suppress it and just say no but
since it's
being uh config you know this is one of
those things we will dig with it we will
dig into this in another one because I
want to correct this for us because this
is an older thing that we did it's an
older approach to the jdbc
template I digress so what we can do is
we can do a
query we can give it a uh in this case
we're going to give it a r mapper and
I'll show what that is and so this is
the
list um it is a
straight
SQL query so we're just going to select
star where status is basically where
it's still there order by this and then
we're going to go P that into the
opportunity row mapper so we're going to
need to create a row mapper row mappers
are also fairly simple because what
we're going to do is we're going to
within our row mapper we're going to
take an
object our model which in this case is
the uh opportunity itself
and we're going to create basically just
create a new instance and we're going to
populate it so we're going to set the ID
the title in this case all of the
variables or all of the values that are
the columns from the database that are
also the properties on the class so this
is how we map our data from the database
into our
model and then we also have a result set
extractor uh which is another thing we
can use because we're basically say hey
I'm going to take a result set that I
get from
SQL and I'm going to go through in this
case I'm going through each record in my
result set and I am now going
to
map I'm going to create a mapper and I'm
going to pull those values in so it's
basically query the database get your
result set pass a result set into the uh
the extractor which says take the
extractor and turn that into
a instead of each row being a result set
it's going to each row is going to be a
uh opportunity row a row mapper it's
basically going to say take that data
make it something that is more useful to
me so instead of a database result set
it's going to be a list of rows in
memory and that's where I'm going to use
the mapper and so that
point oh so here we go so I'm going to
come through here it says hey give it an
opportunity R mapper we're off we are
off and ready to
go uh let's see so with that and we can
get more complicated we can do some
pretty complicated uh queries we will
talk more about these later uh cuz right
now what I would just want to do is I'm
going to say hey let's list out the
let's see if I can list the data in my
table uh in my database right now that
means that I'm going to swing back over
because I've got my I've got my mod
model I've got my Dao so I've got a an
interface that says this is how we're
going to interact with some store some
data store I've got the the Dao imp the
implementation class I've got something
that actually uses that interface and
implements it for in this case Maria DB
and I could name that something like
opportunity do Dao Implement Maria DB
something like that to to really be
specific about
it I I also have my result set extractor
that says hey whatever I get from the
database I'm going to map that into
memory objects I'm going to with my row
mapper I says I say for each row in a
result set here's how I'm going to map
it and so now I need to go back to my
hello controller which is not my rest
it's the other where did that go so I go
to my hello
controller and so now I want to be able
to actually put this code together so
first thing I need to
do is I'm going to actually get an
instance of of my opportunity Dao now
it's not the implementer I'm going to
I've already got that like wired up it's
going to say hey this implementation
already exists this is the one it's
going to take so I'm going to use an
autowire again which basically just goes
through walks through that code and
creates an instance of an opportunity
Dao which we're just going to call
lowercase o opportunity
Dao because then we get to call
that and that notice that we don't have
to
talk about the implement or anything
like that we've already got that stuff
wired in everything's good because it
says hey here's my this is my repository
that's where it basically links my Dao
impl to the actual
Dao uh through the I've got my jdbc
template I'm creating connecting
everything up now I come over to my
hello controller and now that I've got
it all hooked up now I can work with it
just like any other class so now this is
where I can do my list I can do a get
because I generated those I could
actually delete I can do a few different
things and this one let's see I'm just
going to leave it like that uh I'm not
going to present results like that CU
that was a result
set now I can now I have here uh add
attribute results we've talked about
this before where I can actually come
in and like we did with name here so for
for
Greetings uh where's greetings that was
all the way up
here is I have something very simple is
I'm just passing that name in now what I
can
do is and so here if I run it right now
this is one I didn't even pass it a name
but I can see here's my you know my
opportunities and stuff what I can do is
I can change this so let's change this
to well um let's change this to be uh
here
is
your
opportunity if I can spell it
right and we're just going to call this
op and let's see what this looks
like
so we change this up a little
bit it's probably not what I want to do
I probably don't because I'm using
greetings in two places so I probably
should let me go look at this real quick
Oh wrong one uh get over here so if I go
over here and I do my hello controller
yes I'm using greetings twice so let's
change that let's
say um let's roll this back there we go
and instead I'm going to duplicate that
uh let's do it this
way sure um
um yeah fine I'll save that this is
going to
be
opportunity and actually I could
probably get far more
complicated so let me go see I think I
have uh I don't know if I've got it all
set up let's see how good this works if
I go back to my prior
list is I had somewhere in
here so we're going to take our p page
here let's make sure I'm on the right
one yep and this is now going to be the
we're going to do an
opportunity and we're going to do it
this
way and we're just going to call it op
as we're going to pass that
in so now this is going to be uh add
attribute
name actually I'm sorry
op equals
item right yeah because that's the one
that we
got so now if we go look at
[Music]
it oops did I go oh because I didn't
change this
to change that to
opportunity and is it going to the right
place let's see so add attribute oh did
I get that
right add attribute
[Music]
name going to give the model
back did I create my model I did okay so
model add
attribute is the item let's just do
let's see did I get that
right
he is not going oh because I've got to
reset I've got to rebuild it because it
was not going to the right
place that happens sometimes boom and
here's my opportunity so I get this big
honking to string because that's what
it's going to do it's going to take my U
opportunity much as I did down here and
somewhere in here you could see it
pretty nasty where it does it out to a
string now I can clean this up I can put
you know name it has as I need to and
things like
that we will stop there and we're going
to come back next time around we'll get
a little deeper into this and we're
going to look
at uh we'll go ahead and like list out
our uh opportunities We'll add that in
and uh we're just going to keep on
chugging along so if you have any
questions shoot an email out to info@
developin or.com and uh we'll just you
know do our best to keep chugging along
here happy to help you out with in any
way that we can if you have any
questions any struggles let us know know
as always go out there and have yourself
a great day and a great week we'll talk
to you next time hello this is Rob with
developing or also known as building
better developers wanted to announce
that we have school. developer.com feel
free to check it out if you like any of
this information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology or how toos you
you can work on your business skills we
can help you with becoming a better
developer as in coding and things like
that a lot of the stuff you've seen on
YouTube we also have out at school.
develop andur we just have it in a
little more of a uh educational format
and a way for you to track your progress
as you move forward becoming a better
[Music]
developer