📺 Develpreneur YouTube Episode

Video + transcript

Spring Boot JDBC Template Part 2

2023-11-23 •Youtube

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
1.35

[Music]

28.88

and okay so I think I want that to be

33.239

there I don't know why that got changed

36.84

there

39.399

but sa

43.399

update sorry while I do some little C

46.48

code correction I'll talk while I

47.68

correct code uh it is obviously K

50.68

sensitive and obviously that's something

52.64

we want to deal with so what we're going

54.879

to end up doing here is for each of our

57.96

calls or our functions that we put or

60.559

methods to be technically correct that

63.079

we put in our interface we have to

64.92

actually now write the code that does it

67.32

just as a brief if we you know if you

69.56

don't know what an interface is an

71.04

interface is basically like a a cover on

74.799

a box that said you know that has holes

77.36

in it for where buttons are supposed to

79.119

be and so that cover only is going to be

83.079

useful to boxes that have buttons in the

85.24

correct places and that's sort of what

87.28

we have here so an interface says hey

90.04

this is roughly how you're going to

91.84

interface this is how you're going to

93.079

work with this data or this thing and in

97.399

so doing the interface is going to have

99.799

certain requirements to it and those are

102.479

in this case there's methods so it says

104.28

hey you can integrate your

108.32

opportunity through this

110.24

Dao you can integrate it with

113

whatever data source you want so we're

116.24

going to use a

117.759

jdbc but it could be things like it

119.92

could be stored in a file so we could

122.079

have a a file implementer that says take

125.68

this and instead of pointing it at the

127.36

database let's save it in a

130.8

file or something similar to that and so

134.04

what this says is it says I don't care

135.8

where you store it you need to be able

138.8

to support these features and that's

142.239

what we do in the implementation which I

145.08

could go find where I put the

146.92

Implement and I want to start with so

150.879

so that's what we're going to be

151.72

building now the jdbc

154.36

part that

156.599

is uh in this case we've got org.

159.4

springf framework. jdbc doc core jdbc

162.879

and

163.76

template that is what we that is like

165.92

the core of what we need because what

168.56

we're going to do with the jdbc template

171.56

is we're going to create a I'm going to

174.12

create a variable basically

175.92

to use my jdbc template it is going to

179.76

to be this is key is I've got or spring

183.04

spring framework stereotype

185.4

repository and that is going to be right

189.36

here I'm going to need this because when

190.879

I use that rep at repository it says in

195.72

the system it says Hey create this

198.92

object go connect and by the connect is

202.599

hey there's a data source that's going

204.319

to be sent to it and so the data source

207.72

is built based on our product properties

211.12

back

214.56

here and it's going to grab those

216.599

properties it's going to basically

218.48

essentially what it's going to do is

219.4

it's going to connect to the database

220.799

Force so now what I can do is I can take

222.799

this effectively this database

225.56

connection and I can do stuff with it

227.879

for example I can say with my jdbc

232.079

template I can do a query which it

234.72

doesn't

235.72

like uh let's

238.68

see

243.56

it is deprecated because

247.079

of uh probably it's because it's an

249.799

object I believe but we will we will

252.959

deal with this in a little bit uh let's

255.72

see oh new object ID new oh because it

259.6

probably needs a mapper is I think what

261.959

it

264.16

wants oh it needs

267.639

a yeah we'll come back to that that I

270.56

can suppress it and just say no but

272.28

since it's

273.44

being uh config you know this is one of

275.8

those things we will dig with it we will

277.08

dig into this in another one because I

278.52

want to correct this for us because this

280.24

is an older thing that we did it's an

281.8

older approach to the jdbc

284.28

template I digress so what we can do is

287.72

we can do a

289

query we can give it a uh in this case

292.4

we're going to give it a r mapper and

293.479

I'll show what that is and so this is

294.759

the

295.52

list um it is a

298.56

straight

300.16

SQL query so we're just going to select

302.84

star where status is basically where

305.68

it's still there order by this and then

307.68

we're going to go P that into the

309.52

opportunity row mapper so we're going to

312.24

need to create a row mapper row mappers

316.16

are also fairly simple because what

318.28

we're going to do is we're going to

320.4

within our row mapper we're going to

321.72

take an

323.52

object our model which in this case is

326.759

the uh opportunity itself

330.16

and we're going to create basically just

331.919

create a new instance and we're going to

333.56

populate it so we're going to set the ID

335.08

the title in this case all of the

336.759

variables or all of the values that are

339.12

the columns from the database that are

341.479

also the properties on the class so this

343.24

is how we map our data from the database

346.4

into our

349.36

model and then we also have a result set

353.68

extractor uh which is another thing we

356.88

can use because we're basically say hey

358.16

I'm going to take a result set that I

360.24

get from

361.56

SQL and I'm going to go through in this

364.6

case I'm going through each record in my

366.479

result set and I am now going

370.319

to

372.12

map I'm going to create a mapper and I'm

374.479

going to pull those values in so it's

376.72

basically query the database get your

379.96

result set pass a result set into the uh

385.44

the extractor which says take the

387.44

extractor and turn that into

390.599

a instead of each row being a result set

393.88

it's going to each row is going to be a

396.12

uh opportunity row a row mapper it's

398.759

basically going to say take that data

400.599

make it something that is more useful to

402.44

me so instead of a database result set

406.72

it's going to be a list of rows in

409.639

memory and that's where I'm going to use

411.28

the mapper and so that

415.16

point oh so here we go so I'm going to

418.12

come through here it says hey give it an

420.16

opportunity R mapper we're off we are

422.759

off and ready to

426.199

go uh let's see so with that and we can

430.56

get more complicated we can do some

432.52

pretty complicated uh queries we will

434.36

talk more about these later uh cuz right

436.639

now what I would just want to do is I'm

437.84

going to say hey let's list out the

440.84

let's see if I can list the data in my

442.52

table uh in my database right now that

445.24

means that I'm going to swing back over

447.08

because I've got my I've got my mod

449.36

model I've got my Dao so I've got a an

452.68

interface that says this is how we're

454.919

going to interact with some store some

457.12

data store I've got the the Dao imp the

461.199

implementation class I've got something

463.039

that actually uses that interface and

464.96

implements it for in this case Maria DB

468.56

and I could name that something like

469.919

opportunity do Dao Implement Maria DB

473.72

something like that to to really be

475.759

specific about

478.08

it I I also have my result set extractor

482.039

that says hey whatever I get from the

483.319

database I'm going to map that into

485.039

memory objects I'm going to with my row

487.28

mapper I says I say for each row in a

489.68

result set here's how I'm going to map

491.479

it and so now I need to go back to my

494.319

hello controller which is not my rest

496.879

it's the other where did that go so I go

499.36

to my hello

500.639

controller and so now I want to be able

502.759

to actually put this code together so

504.8

first thing I need to

506.479

do is I'm going to actually get an

508.639

instance of of my opportunity Dao now

511.56

it's not the implementer I'm going to

513.719

I've already got that like wired up it's

516.279

going to say hey this implementation

518

already exists this is the one it's

520.039

going to take so I'm going to use an

522

autowire again which basically just goes

523.959

through walks through that code and

525.8

creates an instance of an opportunity

528.839

Dao which we're just going to call

530.36

lowercase o opportunity

532.72

Dao because then we get to call

536.399

that and that notice that we don't have

538.959

to

539.72

talk about the implement or anything

541.079

like that we've already got that stuff

543.72

wired in everything's good because it

545.56

says hey here's my this is my repository

547.839

that's where it basically links my Dao

551.76

impl to the actual

554.48

Dao uh through the I've got my jdbc

557.079

template I'm creating connecting

559.44

everything up now I come over to my

562.279

hello controller and now that I've got

564.32

it all hooked up now I can work with it

566.76

just like any other class so now this is

569.279

where I can do my list I can do a get

572.88

because I generated those I could

574.2

actually delete I can do a few different

576.44

things and this one let's see I'm just

580.079

going to leave it like that uh I'm not

581.399

going to present results like that CU

583.32

that was a result

585.44

set now I can now I have here uh add

589.959

attribute results we've talked about

591.92

this before where I can actually come

595.8

in and like we did with name here so for

599.079

for

599.92

Greetings uh where's greetings that was

602.279

all the way up

606

here is I have something very simple is

608.519

I'm just passing that name in now what I

610.279

can

611.16

do is and so here if I run it right now

614.24

this is one I didn't even pass it a name

616.839

but I can see here's my you know my

619.68

opportunities and stuff what I can do is

621.88

I can change this so let's change this

623.56

to well um let's change this to be uh

629.68

here

630.839

is

633.279

your

635.519

opportunity if I can spell it

640.279

right and we're just going to call this

642.399

op and let's see what this looks

645.079

like

648.399

so we change this up a little

652.92

bit it's probably not what I want to do

656.04

I probably don't because I'm using

657.12

greetings in two places so I probably

659.2

should let me go look at this real quick

662.2

Oh wrong one uh get over here so if I go

666.72

over here and I do my hello controller

668.72

yes I'm using greetings twice so let's

670.32

change that let's

672.16

say um let's roll this back there we go

677.6

and instead I'm going to duplicate that

682.24

uh let's do it this

686.6

way sure um

690

um yeah fine I'll save that this is

693.36

going to

696.279

be

698.72

opportunity and actually I could

701.24

probably get far more

704.04

complicated so let me go see I think I

706.8

have uh I don't know if I've got it all

708.88

set up let's see how good this works if

710.399

I go back to my prior

713.32

list is I had somewhere in

716.36

here so we're going to take our p page

719.6

here let's make sure I'm on the right

721.2

one yep and this is now going to be the

724.6

we're going to do an

727.8

opportunity and we're going to do it

729.6

this

733.36

way and we're just going to call it op

735.8

as we're going to pass that

737.56

in so now this is going to be uh add

742.72

attribute

744.639

name actually I'm sorry

747.199

op equals

751.079

item right yeah because that's the one

753

that we

754.48

got so now if we go look at

756.89

[Music]

760.639

it oops did I go oh because I didn't

763.48

change this

765.92

to change that to

773.32

opportunity and is it going to the right

775.639

place let's see so add attribute oh did

779.36

I get that

781.92

right add attribute

783.82

[Music]

788.88

name going to give the model

794.399

back did I create my model I did okay so

797.36

model add

799.44

attribute is the item let's just do

802.639

let's see did I get that

807.76

right

811.199

he is not going oh because I've got to

813.639

reset I've got to rebuild it because it

816

was not going to the right

817.839

place that happens sometimes boom and

820.32

here's my opportunity so I get this big

822.199

honking to string because that's what

824.88

it's going to do it's going to take my U

828.24

opportunity much as I did down here and

831.88

somewhere in here you could see it

833

pretty nasty where it does it out to a

834.519

string now I can clean this up I can put

837.12

you know name it has as I need to and

839.44

things like

840.8

that we will stop there and we're going

843.68

to come back next time around we'll get

845.24

a little deeper into this and we're

847.279

going to look

848.92

at uh we'll go ahead and like list out

851.6

our uh opportunities We'll add that in

854.56

and uh we're just going to keep on

855.68

chugging along so if you have any

857.32

questions shoot an email out to info@

859.12

developin or.com and uh we'll just you

861.72

know do our best to keep chugging along

863.16

here happy to help you out with in any

865.04

way that we can if you have any

866.399

questions any struggles let us know know

869.639

as always go out there and have yourself

870.72

a great day and a great week we'll talk

872.68

to you next time hello this is Rob with

876.199

developing or also known as building

878.04

better developers wanted to announce

880.36

that we have school. developer.com feel

883.32

free to check it out if you like any of

885.68

this information any of the content that

887.839

we've sent and you would like to see

889.16

more you can come out you can enroll for

891.16

free we have free courses we've got

893.56

places for you to get better at just

896.12

learning a technology or how toos you

898.639

you can work on your business skills we

900.56

can help you with becoming a better

902.36

developer as in coding and things like

904.72

that a lot of the stuff you've seen on

906.279

YouTube we also have out at school.

909.04

develop andur we just have it in a

910.639

little more of a uh educational format

913.12

and a way for you to track your progress

915.6

as you move forward becoming a better

920.53

[Music]

927.6

developer