The greatest thing since sliced bread If you have ever had to write an application in an object oriented language that communicates with a relational database, then you understand the word tedium down to its very core. There is nothing more mind-numbing than writing lots of 'for' and 'while' loops to populate an object from a result set. It is a trivial exercise that takes lots of time and is prone to errors. When I first started writing web applications it took me less than a few hours of using the JDBC api to realize that populating an object from the DB was a job for a peon, not a mighty software engineer. Thus I was quite motivated as I jumped on Google in search of a solution. The coding gods were smiling on me that day because I quickly stumbled upon iBATIS SQL Maps. It took me an afternoon to get comfortable with their api, and soon I was writing all my sql into XML files. Here is what I am talking about. [code lang="xml"]
[/code]
Notice the "resultMap" part. Each property of the object is associated with a column in the database. Simply put, this is an adaptor. It is analagous to what you use to plug your laptop into a wall jack in Europe, except instead of moving current through this adapter, we are moving data. The next part to notice is the select statement. Look familiar? It is plain old SQL wrapped in an XML statement. By associating this statement with the resultMap we defined all the work is done. We need only call out to this "selectAll" statement from our java code and we get fully populated objects, or in this case a list of objects. SQL Maps does all the work for us. If you don't see the beauty of this... *shakes head* Trust me, it is beautiful! Clinton Begin, the guy who wrote SQL Maps is a really great guy. He created a good product, and stayed behind it. His dedication to his creation is what kindled the Apache foundation's interest in iBATIS. My own experience with this was his activity on the iBATIS mailing list. No matter how remedial the question he would answer it. That level of support was the core of a community that has really grown. Here is a nice link to the proposal that introduced iBATIS into the Apache foundation. Thanks Clinton, may your mapper live a thousand years. The one itch I have forgotten to scratch here is "WHAT THE HECK DOES iBATIS MEAN?" Lucky for us they explain themselves here, but I'm going to paraphrase anyway. Back in 2001 iBATIS was all about cryptographic software solutions. They picked a name that was a combination of two words: internet + abatis = iBATIS. If you are reading this you must already know what the internet is, but an abatis, in case you didn't know, is a felled tree with the brances sharpened. Such trees used to be used in warfare as obstacles for your adversary (a fitting name for a security software company). Here is a picture of an abatis in Kansas (click the image to enlarge). It says it was taken in Leavenworth which is only about 30 miles from where I live. Note that the picture is very old, and it is a yellowed black and white shot. Kansas isn't really black and white, (we have all the colors the rest of the world has). If you look close you can see a sign in front of the abatis. I think it says "Abatis". For some reason this really struck me as funny. Apparently the guys at the fort didn't want to forget what to call that pile of sticks. References
No comments:
Post a Comment