In addition to some sweet Inversion of Control injection action, Spring brings flowers and baby deer. And it brings some nice database connection help. Spring JdbcTemplate is a vanguard of strength and ease for connecting to your favorite RDBMS. And it so happens that the DBA I’m working with right now digs the stored procs. So, we’ll meet in the middle with some more goodness born of Spring.
Blessed Rod Johnson gives us the
org.springframework.jdbc.object.StoredProcedure. It’s abstract, and you’ll want to extend it and fully implement it in order to specify what stored proc in your database you’re calling. And here’s a sample implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Points to consider:
ItemInsertis itself injectable as a Spring bean
- Input and output parameters are declared in the constructor
super.execute()is call, which is the function that really does the magic.
The only other interesting thing…
From the JavaDoc:
Execute the stored procedure. Subclasses should define a strongly typed execute method (with a meaningful name) that invokes this method, populating the input map and extracting typed values from the output map. Subclass execute methods will often take domain objects as arguments and return values. Alternatively, they can return void.
Thusly, out method is called
insert() because it’s a meaningful name. It takes our domain object,
Item and pulls out the necessary fields inside the method, putting them into a map for the call
Call it from a service
Now the only work left is just to call your awesome procedure and use the results for something wonderful. Eg,
1 2 3 4 5 6 7 8 9 10 11
Items are now stored procedurally and you can rest easy knowing that Oracle has all your data.