<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4368777317649012081</atom:id><lastBuildDate>Fri, 03 May 2013 16:57:47 +0000</lastBuildDate><category>ruby</category><category>i18n</category><category>javascript</category><category>java</category><category>mysql</category><category>funny</category><category>interesting</category><category>pojo</category><category>junit</category><category>jstl</category><category>videos</category><category>jsp</category><category>web applications</category><category>tomcat</category><category>gui</category><category>opinions</category><category>mybatis</category><category>log4j</category><category>el</category><category>android</category><category>css</category><category>struts</category><category>agile</category><category>git</category><category>spring</category><category>servlets</category><category>rails</category><category>dao</category><category>logback</category><category>eclipse</category><category>wsh</category><category>tld</category><title>The IdleWorx Blog</title><description>Java,Tomcat,Servlets,JSP,Android,Ruby and other programming stuff</description><link>http://blog.idleworx.com/</link><managingEditor>noreply@blogger.com (IdleWorx)</managingEditor><generator>Blogger</generator><openSearch:totalResults>53</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/idleworxblog" /><feedburner:info uri="idleworxblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-7228755949920594512</guid><pubDate>Sat, 16 Mar 2013 04:31:00 +0000</pubDate><atom:updated>2013-03-15T21:31:25.361-07:00</atom:updated><title>You should update your TableSorter jQuery plugin</title><description>If you're still using the cool &lt;a href="http://tablesorter.com/docs/"&gt;jQuery TableSorter&lt;/a&gt; plugin by Christian Bach, be advised that the last update to it was back in 2008 (version 2.0.8 as of today).&lt;br /&gt;
&lt;br /&gt;
There is a newer forked version of &lt;a href="https://github.com/Mottie/tablesorter"&gt;Table Sorter&lt;/a&gt; on github, v2.7.x that seems to be maintained by someone else and up to date. Docs &lt;a href="http://mottie.github.com/tablesorter/docs/index.html"&gt;here&lt;/a&gt;.</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/LcJBVQUvRrs/you-should-update-your-tablesorter.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2013/03/you-should-update-your-tablesorter.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-42814837646676780</guid><pubDate>Tue, 12 Mar 2013 02:52:00 +0000</pubDate><atom:updated>2013-03-11T19:52:34.328-07:00</atom:updated><title>Teach a Dog to REST</title><description>A good video on how to build REST APIs the right way.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe src="http://player.vimeo.com/video/17785736?badge=0" width="400" height="300" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/oqa3bGUXj48/teach-dog-to-rest.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2013/03/teach-dog-to-rest.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-5938111996039499377</guid><pubDate>Sat, 22 Dec 2012 00:56:00 +0000</pubDate><atom:updated>2011-06-28T18:01:48.312-07:00</atom:updated><title>Welcome to the IdleWorx blog</title><description>&lt;p&gt; This is the official blog of &lt;a href="http://www.idleworx.com" target="_blank"&gt;idleworx.com&lt;/a&gt;. 
A blog covering Android, JSP, Servlets, Tomcat, JavaScript, Ruby and other web development stuff. &lt;/p&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/1KaJL_HhDFg/welcome.html</link><author>noreply@blogger.com (IdleWorx)</author><feedburner:origLink>http://blog.idleworx.com/2000/01/welcome.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-8321301948380244138</guid><pubDate>Fri, 16 Nov 2012 02:57:00 +0000</pubDate><atom:updated>2012-11-15T18:57:58.854-08:00</atom:updated><title>The Unbearable Immutability of Java Strings</title><description>&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
Here is a quick explanation of what an Immutable String is in Java. &lt;/div&gt;
Basically, an immutable string means that the string 'object' can't be changed, but references to it can.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;a href="http://3.bp.blogspot.com/-5JwyiVhuzg8/UKWq1DM2xHI/AAAAAAAAAE8/7774Maq-lIM/s1600/theunbearableimmutabilityofstrings.png"&gt;&lt;br /&gt;
&lt;img border="0" src="http://3.bp.blogspot.com/-5JwyiVhuzg8/UKWq1DM2xHI/AAAAAAAAAE8/7774Maq-lIM/s400/theunbearableimmutabilityofstrings.png" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/xE1TOo3OBG4/the-unbearable-immutability-of-java.html</link><author>noreply@blogger.com (IdleWorx)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-5JwyiVhuzg8/UKWq1DM2xHI/AAAAAAAAAE8/7774Maq-lIM/s72-c/theunbearableimmutabilityofstrings.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/11/the-unbearable-immutability-of-java.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-5419101821378678880</guid><pubDate>Sat, 10 Nov 2012 22:24:00 +0000</pubDate><atom:updated>2012-11-10T14:24:06.943-08:00</atom:updated><title>Jerker Desk Replacement Screws and Bolts</title><description>Yes, there is a &lt;a href="http://www.ikeahackers.net/2012/01/split-level-jerker-desk.html" target="_blank"&gt;cult like following&lt;/a&gt; on the internet for IKEA Jerker Desks. &lt;br /&gt;
&lt;br /&gt;
It's hard to find replacement screws for the desk since it's discontinued. Here's a list of the screw sizes I found at Lowe's that should work with any Jerker desk. I used these to add on some additional shelves (since I only had the screws and bolts that came with the main table part of the desk)&lt;br /&gt;
&lt;br /&gt;
You can find these at your local Lowe's or HomeDepot store in the METRIC/European section of the nuts and bolts isle. Ask for it, it's there :-)  &lt;br /&gt;
&lt;br /&gt;
Screws - Hex Cap Screws (&lt;b&gt;Class 8.8 - M6-1.00x50&lt;/b&gt;)&lt;br /&gt;
Nuts - Hex Nuts (&lt;b&gt;Class 8 - M6-1.00&lt;/b&gt;) [option 1]&lt;br /&gt;
Nuts - Insert Lock Nuts (&lt;b&gt;Nylon Class 8 - M6-1.00&lt;/b&gt;)  [option 2]&lt;br /&gt;
Washers - Flat Washers - &lt;b&gt;6mm&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;TIP&lt;/i&gt;: Use the washers between the metal part and the nut for tighter grip]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I got the Jerker desk a while back, as I needed a better desk for programming. I wouldn't say it's the most amazing desk ever but it's comfortamble and makes excellent use of space particularly if you have multiple things you need around eg. scanner, printer, subwoofer, midi keyboard etc. &lt;br /&gt;
&lt;br /&gt;
IKEA has a similar desk on sale called the &lt;a href="http://www.ikea.com/us/en/catalog/products/40111124/" target="_blank"&gt;FREDRIK&lt;/a&gt;. Some people seem to think the Jerker desk doesn't even compare with the FREDERIK, but I don't know as I've never tried it so can't compare.&lt;br /&gt;
&lt;br /&gt;
So far the Jerker desk is great for what I need. Use your own judgement. &lt;br /&gt;
&lt;br /&gt;
Hope you found this useful in case you are looking to add some additional shelves to your Jerker desk.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/EhEw5qthYjE/jerker-desk-replacement-screws-and-bolts.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>4</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/11/jerker-desk-replacement-screws-and-bolts.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-1639697718509726</guid><pubDate>Thu, 01 Nov 2012 00:03:00 +0000</pubDate><atom:updated>2012-10-31T17:23:04.151-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">spring</category><title>All Spring Schema Configurations and JARs for Spring 2.0, 2.5, 3.0</title><description>This post contains basic info about Spring Schema Configuration XML syntax for different versions of Spring 2.0.x, 2.5.x and 3.0.x.&lt;br /&gt;
&lt;br /&gt;
Not sure why it's so hard to find this data all put together so here it is. Direct download links for the Spring JARs and the spring xml file header configuration for each version.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Spring 2.0 - October 2006&lt;/h3&gt;&lt;br /&gt;
&lt;pre class="brush: xml; wrap-lines:false;"&gt;&lt;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
"&gt;
&lt;!-- &lt;bean/&gt; definitions here --&gt;
&lt;/beans&gt;
&lt;/pre&gt;&lt;br /&gt;
Direct Download Link: &lt;a href="http://sourceforge.net/projects/springframework/files/springframework-2/2.0.8/" target="_blank"&gt;Spring Framework 2.0.8&lt;/a&gt; (latest Spring 2.0.x release) &lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Spring 2.5 - November 2007&lt;/h3&gt;&lt;br /&gt;
&lt;pre class="brush: xml; wrap-lines:false;"&gt;&lt;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd
"&gt;
      &lt;!-- &lt;bean/&gt; definitions here --&gt;
&lt;/beans&gt;
&lt;/pre&gt;&lt;br /&gt;
Direct Download Link: &lt;a href="http://spring-framework-2.5.6.sec03.zip/"&gt;spring-framework-2.5.6.SEC03.zip&lt;/a&gt; (latest Spring 2.5.x maintenance release)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt;The only difference between the syntax for Spring 2.0 and 2.5 seems to be the addition of the context schema in 2.5. &lt;br /&gt;
Also as of Spring 2.5, Spring provides support for configuring the MVC framework components using &lt;a href="http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html#mvc-annotation" target="_blank"&gt;annotations&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Spring 3.0 - December 2009&lt;/h3&gt;&lt;br /&gt;
&lt;pre class="brush: xml; wrap-lines:false;"&gt;&lt;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
"&gt;
 &lt;property-placeholder&gt;
 &lt;annotation-config&gt;
 &lt;!-- &lt;bean/&gt; definitions here --&gt;
&lt;/annotation-config&gt;&lt;/property-placeholder&gt;&lt;/beans&gt;
&lt;/pre&gt;&lt;br /&gt;
Direct Download Link: &lt;a href="http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.1.3.RELEASE.zip" target="_blank"&gt;spring-framework-3.1.3.RELEASE.zip&lt;/a&gt; (current production release)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt;The spring.jar that contained almost the entire framework is no longer provided in Spring 3.0 so you must include each jar as needed separately. &lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;New Features in Spring 3.0&lt;/h4&gt;&lt;br /&gt;
Spring 3.0 also introduced a few &lt;a href="http://static.springsource.org/spring/docs/3.0.x/reference/new-in-3.html" target="_blank"&gt;new features&lt;/a&gt; such as additional annotations for MVC, server-side support for building RESTful applications, the &lt;a href="http://static.springsource.org/spring/docs/3.0.x/reference/expressions.html" target="_blank"&gt;Spring Expression Language&lt;/a&gt; and integration of some core features from the &lt;a href="http://javaconfig.sourceforge.net/" target="_blank"&gt;JavaConfig&lt;/a&gt; project.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Useful Links&lt;/h3&gt;&lt;a href="http://static.springsource.org/spring/docs/2.0.x/reference/index.html" target="_blank"&gt;Spring 2.0.x Reference&lt;/a&gt; &lt;br /&gt;
&lt;a href="http://static.springsource.org/spring/docs/2.0.x/reference/xsd-config.html" target="_blank"&gt;Spring 2.0.x Schema Configuration&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://static.springsource.org/spring/docs/2.5.x/reference/index.html" target="_blank"&gt;Spring 2.5.x Reference&lt;/a&gt; &lt;br /&gt;
&lt;a href="http://static.springsource.org/spring/docs/2.5.x/reference/xsd-config.html" target="_blank"&gt;Spring 2.5.x Schema Configuration&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/" target="_blank"&gt;Spring 3.0.x Reference&lt;/a&gt; &lt;br /&gt;
&lt;a href="http://static.springsource.org/spring/docs/3.0.x/reference/xsd-config.html" target="_blank"&gt;Spring 3.0.x Schema Configuration&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://blog.springsource.com/2009/12/16/spring-framework-3-0-goes-ga/" target="_blank"&gt;New in Spring 3.0 &lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.springsource.org/download" target="_blank"&gt;Spring Main Download Page&lt;/a&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/C-E-0hvYmtw/all-spring-schema-configurations-and.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/10/all-spring-schema-configurations-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-1094918308884475157</guid><pubDate>Thu, 12 Apr 2012 23:12:00 +0000</pubDate><atom:updated>2012-04-12T16:12:47.197-07:00</atom:updated><title>Android Development Is Not Easy</title><description>As a seasoned software engineer I spent a good number of hours over the past week trying to figure out how to do the following on an Android device.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Problem:&lt;/b&gt; Create an android application that checks a local SQLite database at a given interval (say every day @ 12:00 am), does something with that data, then displays a notification to the user.&lt;br /&gt;
&lt;br /&gt;
Seemed like a trivial thing to do.&lt;br /&gt;
&lt;br /&gt;
Initially I thought I would need only a Service that my Activity starts. I was wrong.&lt;br /&gt;
&lt;br /&gt;
After reading more thoroughly through the Android API documentation, numerous stackoverflow articles, and some trial and error I figured it out.&lt;br /&gt;
&lt;br /&gt;
In order to do this on android you have to understand the following concepts:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;BroadcastReceivers&lt;/li&gt;
&lt;li&gt;Services (more specifically IntentServices)&lt;/li&gt;
&lt;li&gt;Intents (more specifically PendingIntents)&lt;/li&gt;
&lt;li&gt;Reading/Writing to an SQLite database&lt;/li&gt;
&lt;li&gt;The AlarmManager&amp;nbsp;&lt;/li&gt;
&lt;li&gt;How notifications work&lt;/li&gt;
&lt;li&gt;+ a few other things&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
The short answer is the following.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt; You have to implement a custom broadcast receiver, that uses the AlarmManager to schedule a PendingIntent that starts a custom Service which does the work, which creates a Notification at (or around) a set time for the user to click on.&lt;br /&gt;
&lt;br /&gt;
There's some quirks too you have to consider:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;How do you make sure your app continues to run after the user reboots the android device?&lt;/li&gt;
&lt;li&gt;How do you schedule the first Notification after your app is started by the user for the first time?&lt;/li&gt;
&lt;li&gt;When do you schedule the alarm to run? What happens if the device is off?&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
So if you just got an awesome idea for an Android app, great. I hope you'll be able to use your skills to build it and make it a success. But be warned, you need not only coding skills, but&amp;nbsp;passion&amp;nbsp;and patience, and don't fool yourself into thinking that it will take you a few hours.&lt;br /&gt;
&lt;br /&gt;
Building an app (a non trivial app that is) is one thing. Building an android application that is well designed and coded, runs well, and provides a true service to your users is hard.&lt;br /&gt;
&lt;br /&gt;
If you don't have the luxury of time, the patience required, or the necessary skills to build an Android Application yourself, don't sweat it, it's not easy stuff.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;So do yourself a favor and &lt;a href="http://www.idleworx.com/" target="_blank"&gt;hire a developer&lt;/a&gt;&amp;nbsp;to build it for you.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/schyz1MbyX4/android-development-is-not-easy.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>1</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/04/android-development-is-not-easy.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-4455820721088616223</guid><pubDate>Mon, 06 Feb 2012 00:50:00 +0000</pubDate><atom:updated>2012-02-05T16:50:37.632-08:00</atom:updated><title>The Do Not Track movement is useless</title><description>&lt;h2&gt;
... or why privacy is your own responsibility&lt;/h2&gt;
The &lt;b&gt;Do Not Track&lt;/b&gt; movement (&lt;a href="http://donottrack.us/"&gt;donottrack.us&lt;/a&gt;)&amp;nbsp;is, according to their website&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;"a technology and policy proposal that enables users to opt out of tracking by websites they do not visit"&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
and from a recent article in USA Today,&amp;nbsp;&lt;a href="http://www.usatoday.com/tech/news/story/2011-11-15/facebook-privacy-tracking-data/51225112/1" target="_blank"&gt;How Facebook tracks you across the web&lt;/a&gt;&amp;nbsp;we find out the following&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;"Facebook's efforts to track the browsing habits of visitors to its site have made the company a player in the "Do Not Track" debate, which focuses on whether consumers should be able to prevent websites from tracking the consumers' online activity."&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
Companies like Facebook, Google, etc have a vested interest in finding out as much information about &lt;b&gt;EVERYTHING&lt;/b&gt; you do.&amp;nbsp;That's how they make money. So any &lt;i&gt;movement&lt;/i&gt;&amp;nbsp;that proposes these companies self-regulate and take more care of our&amp;nbsp;privacy needs is simply useless. &lt;br /&gt;
&lt;br /&gt;
Even if regulation was passed forcing these companies to allow users to opt out of tracking,&amp;nbsp;that doesn't mean it wouldn't still occur.&lt;br /&gt;
&lt;br /&gt;
The &lt;a href="http://donottrack.us/"&gt;donottrack.us&lt;/a&gt; website also mentions that&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;"Several large third parties have already committed to honor Do Not Track ... "&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
So what? Does that mean anything really?&lt;br /&gt;
&lt;br /&gt;
What do you know about what they do with this information even if they "&lt;i&gt;committed"&lt;/i&gt;&amp;nbsp;to work against their own self and business interests&amp;nbsp;by not tracking you?&lt;br /&gt;
&lt;br /&gt;
I'll tell you. Absolutely nothing!&lt;br /&gt;
&lt;br /&gt;
Do you have a signed contract with them that says they can never track you?&amp;nbsp;Is there an actual law&amp;nbsp;that prevents them from tracking you? No?&amp;nbsp;Then stop daydreaming.&lt;br /&gt;
&lt;br /&gt;
When it comes to personal privacy and security, you can't outsource it (especially to those who have an interest in your lack of privacy). There's no easy way.
&lt;br /&gt;
&lt;br /&gt;
So if you care about &lt;a href="http://www.schneier.com/blog/archives/2009/12/my_reaction_to.html" target="_blank"&gt;your privacy&lt;/a&gt;, &lt;a href="http://www.documentary24.com/privacy-is-dead-get-over-it--317/" target="_blank"&gt;educate yourself&lt;/a&gt;&amp;nbsp;and &lt;a href="http://torrentfreak.com/which-vpn-providers-really-take-anonymity-seriously-111007/" target="_blank"&gt;protect yourself&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/HO5pFMwwjqg/do-not-track-movement-is-useless.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/02/do-not-track-movement-is-useless.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-5381207489493623322</guid><pubDate>Thu, 02 Feb 2012 06:22:00 +0000</pubDate><atom:updated>2012-02-01T22:22:01.079-08:00</atom:updated><title>Goodbye Delicious, Hello Pinboard</title><description>&lt;br /&gt;
&lt;h2&gt;
&lt;span style="font-weight: normal;"&gt;Goodbye Delicious&lt;/span&gt;&lt;/h2&gt;
There are two types of people in the world, those who rarely save bookmarks, and those who have thousands. If you're like me, you probably have thousands or at least a few hundred. For me it's more exactly 4466 bookmarks at last count. (If you're not like me, a &lt;i&gt;bookmark hoarder&lt;/i&gt;, then you probably want to skip this article.)&lt;br /&gt;
&lt;br /&gt;
Over the past 10-15 years I've collected tons of bookmarks because I like to keep track of cool, useful and interesting things I find online so if I ever want to go back to them I can.&lt;br /&gt;
&lt;br /&gt;
I've spent countless hours over the years keeping things clean, ordered and organized so I can actually make use of this information. As you can imagine if you don't keep this many bookmarks organized and neat there's absolutely no point in saving them in the first place, since you won't be able to find anything if you so choose at a later time.&lt;br /&gt;
&lt;br /&gt;
Bookmarks, when used correctly are nothing more than a self maintained cache of the things you've found most interesting or useful. Thus they are a treasure trove of data for various services such as &amp;nbsp;&lt;a href="http://delicious.com/" target="_blank"&gt;delicious&lt;/a&gt;&amp;nbsp;or &lt;a href="http://en.wikipedia.org/wiki/List_of_social_bookmarking_websites" target="_blank"&gt;various others&lt;/a&gt; which have capitalized on this.&lt;br /&gt;
&lt;br /&gt;
Delicious was founded in 2003, and was essentially the company that introduced social bookmarking and tagging to the masses. That said and done I didn't really get on board with them until a few years ago when I outgrew my&amp;nbsp;bookmarking&amp;nbsp;needs. Trying to manage several thousand bookmarks&amp;nbsp;across&amp;nbsp;multiple browsers and multiple computers does that to you.&lt;br /&gt;
&lt;br /&gt;
But delicious has not delivered and has recently started to fall short, in part due to Yahoo firing the delicious team, and its subsequent purchase by &lt;a href="http://www.avos.com/delicious-press-release/" target="_blank"&gt;Youtube founders&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Here's just a few problems I found:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Slow syncing of bookmarks in browser&lt;/li&gt;
&lt;li&gt;Having to sometimes log in every day&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Firefox addons would stop working after upgrading to certain version of Firefox&lt;/li&gt;
&lt;li&gt;The pain and waste of time trying to find a way to mark all my bookmarks private when bookmarking through a browser plugin. Why, delicious? Why?&lt;/li&gt;
&lt;li&gt;Inconsistent tag naming - You could use spaces in an older versions of the Firefox plugin, but only commas in newer versions. Because it wasn't made clear, myself and I'm sure plenty of others, had had to go through tons of delicious bookmarks and convert tags like "programming jsp reference useful" into the 4 separate tags they should have been in the first place. Not cool)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
And there were other &lt;a href="http://blog.earth2marsh.com/2012/01/dearest-delicious.html" target="_blank"&gt;unhappy campers&lt;/a&gt;.
&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;
&lt;span style="font-weight: normal;"&gt;Hello Pinboard&lt;/span&gt;&lt;/h2&gt;
&lt;br /&gt;
As delicious was going down, I learned of other social bookmarking sites, &lt;a href="http://pinboard.in/"&gt;pinboard.in&lt;/a&gt; being one that caught my attention.&lt;br /&gt;
&lt;br /&gt;
Throughout the years, I had migrated over from IE Favorites to Firefox Bookmarks to delicious bookmarks. I had hand&amp;nbsp;curated my bookmarks and cleaned them up with link checkers to remove expired ones. It was time to move on to something better.&lt;br /&gt;
&lt;br /&gt;
So last night, I finally made the switch and created an account on pinboard.in. It's not free mind you&amp;nbsp;(&lt;i&gt;there is a one time fee of about $9&lt;/i&gt;), but from everything I read it's worth it &amp;nbsp;Not to mention &lt;a href="http://web.appstorm.net/general/interviews/interview-meet-maciej-ceglowski-of-pinboard-in/" target="_blank"&gt;the guy behind it&lt;/a&gt; seems like a great chap and has his stuff together.&lt;br /&gt;
&lt;br /&gt;
And from the first looks, I love it.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Importing almost 4500 bookmarks from both firefox and delicious was painless. Took a few minutes.&lt;/li&gt;
&lt;li&gt;Interface is great, simple, minimal and quick, exactly what I wanted&lt;/li&gt;
&lt;li&gt;Great default privacy settings give you the ability to 'add bookmarks as private by default'&lt;/li&gt;
&lt;li&gt;'enable public profile' is OFF by default&lt;/li&gt;
&lt;li&gt;and more&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
I did notice there was no &lt;i&gt;easy&lt;/i&gt; way to rename a tag in pinboard (&lt;a href="http://pinboard.in/faq#rename_tag" target="_blank"&gt;there is a way though&lt;/a&gt;), but I'm planning to play around with the Pinboard API soon and see what I can cook up.&lt;br /&gt;&lt;br /&gt;I'm sure pinboard is not perfect, but hey, it is the best I've found so far for my bookmarking needs.&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/bc9R1WKfFCg/goodbye-delicious-hello-pinboard.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>1</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/02/goodbye-delicious-hello-pinboard.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-6374180030512657660</guid><pubDate>Wed, 18 Jan 2012 03:31:00 +0000</pubDate><atom:updated>2012-01-17T19:36:11.383-08:00</atom:updated><title>A simple birthday reminder app</title><description>Are you looking for an easy breazy birthday reminder app to keep track of all your friends' birthdays that you seem to forget every year? Well, look no further.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The time has come for you to get your self organized and never forget another birthday again. Check out &lt;a href="http://beta.birthdayduck.com/"&gt;http://beta.birthdayduck.com&lt;/a&gt; a simple, clean and useful birthday reminder service from IdleWorx&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Here's a screenshot&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;a href="http://1.bp.blogspot.com/-I5vOiB5Vt3c/TxY8GVwMC6I/AAAAAAAAADc/8LFyUDMPt-s/s1600/main.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://1.bp.blogspot.com/-I5vOiB5Vt3c/TxY8GVwMC6I/AAAAAAAAADc/8LFyUDMPt-s/s320/main.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
It has the essential features you need in a birthday reminder application:&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Reminders by email&lt;/li&gt;
&lt;li&gt;Reminders by SMS/text message&lt;/li&gt;
&lt;li&gt;A simple way to add names ands birthdays&lt;/li&gt;
&lt;li&gt;Import contacts from Facebook&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
No bells and whistles. birthdayduck.com aims to help you organize your life, by making it very easy to remember people's birthdays.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
It works like a birthday reminder calendar, except you won't need to worry about it all the time. The application will send you reminders by email and sms before someone's birthday.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
We're currently in &lt;b&gt;beta&lt;/b&gt;, and getting ready to launch soon.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #0b5394;"&gt;&lt;b&gt;If you sign up now at
&amp;nbsp;&lt;a href="http://beta.birthdayduck.com/"&gt;http://beta.birthdayduck.com&lt;/a&gt;&amp;nbsp;&amp;nbsp;and you are one of the first 50 sign-ups you'll receive a free lifetime account on birthdayduck.com once we launch.&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #0b5394;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #0b5394;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="color: #0b5394;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/kDi4ta4YrfE/birthday-reminder-app.html</link><author>noreply@blogger.com (IdleWorx)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-I5vOiB5Vt3c/TxY8GVwMC6I/AAAAAAAAADc/8LFyUDMPt-s/s72-c/main.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2012/01/birthday-reminder-app.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-3294680615175376737</guid><pubDate>Thu, 29 Dec 2011 00:35:00 +0000</pubDate><atom:updated>2011-12-28T16:35:15.736-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">videos</category><category domain="http://www.blogger.com/atom/ns#">interesting</category><title>How algorithms shape our world (video)</title><description>&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;object width="320" height="266" class="BLOGGER-youtube-video" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" data-thumbnail-src="http://1.gvt0.com/vi/TDaFwnOiKVE/0.jpg"&gt;&lt;param name="movie" value="http://www.youtube.com/v/TDaFwnOiKVE&amp;fs=1&amp;source=uds" /&gt;
&lt;param name="bgcolor" value="#FFFFFF" /&gt;
&lt;embed width="320" height="266"  src="http://www.youtube.com/v/TDaFwnOiKVE&amp;fs=1&amp;source=uds" type="application/x-shockwave-flash"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/div&gt;
&lt;br /&gt;
Kevin Slavin argues that we're living in a world designed for -- and 
increasingly controlled by -- algorithms. In this riveting talk from 
TEDGlobal, he shows how these complex computer programs determine: 
espionage tactics, stock prices, movie scripts, and architecture.</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/LgQvmszTPWU/how-algorithms-shape-our-world-video.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/12/how-algorithms-shape-our-world-video.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-6114301729051161879</guid><pubDate>Wed, 21 Dec 2011 02:05:00 +0000</pubDate><atom:updated>2011-12-20T18:05:17.434-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">opinions</category><title>Your time is not worth that much</title><description>I noticed a lot of people in the startup/programming/HN community seem to measure their time in the equivalent of how much money they could be making during that time.&lt;br /&gt;
&lt;br /&gt;
For example, you ask a friend to do something for you that takes 2 hours, and they tell you that in those 2 hours they could be making
300$ because that's their hourly rate if they were doing freelance work. So essentially if they aren't paid for these 2 hours they are basically
loosing 300$.&lt;br /&gt;
&lt;br /&gt;
I say this way of estimating how much your time is worth is flawed.&lt;br /&gt;
&lt;br /&gt;
The only time this estimate is true is when you have a guy waiting at your door 24/7 for you to open it and give you work, and let you charge him 150$/hr for that work (per the example above).&lt;br /&gt;
&lt;br /&gt;
Given any other time, your time estimate is probably way off. Because if you don't have work available NOW at the rate you're estimating yourself to be worth, your time is just not worth as much as you think it is.
In fact, if you don't have any work lined up for that rate, your time is worth almost nothing. It's free time.&lt;br /&gt;
&lt;br /&gt;
Could you be making money during your free time? Yes, however if you are not ready at that very moment to take on the work at your going rate, your lost opportunity cost is not 150$/hr, but 0$.&lt;br /&gt;
&lt;br /&gt;
The other problem with correctly estimating what your time is worth is how easy it is easy to overestimate. I mean hey, in this next hour that I don't do anything I could be making a million dollars by winning the lottery. 
So my hourly rate is a million dollars. Just like the example above, that rate IS possible, but highly unprobable not to mention ridiculously unsustainable.&lt;br /&gt;
&lt;br /&gt;
To dig in further, the wiki informs us that:&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
"The notion of opportunity cost plays a crucial part in ensuring that scarce resources are used efficiently"&amp;nbsp;&lt;/blockquote&gt;
&lt;br /&gt;
And that's a good thing. If you're wasting your time on the couch picking your nose, you could be doing much better things with your time (Eg. creating something useful for others and getting paid for it too)&lt;br /&gt;
&lt;br /&gt;
Some people however have the tendency to put a dolar value on &lt;b&gt;EVERYTHING&lt;/b&gt; in their lives. And sadly it's a rather limited world view when everything has a dollar value and an opportunity cost.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
News Flash: Not everything in life has a dollar value&lt;/h3&gt;
&lt;br /&gt;
The wiki has a fairly straight forward example of this&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
"Opportunity costs are not always measured in monetary units or being able to produce one good over another. 
For instance, an individual could choose not to mow his or her lawn, in an attempt to create a prairie land for additional wild life. Neighbors of this individual may see this as unsightly, and want the lawn to be mowed. In this case, the opportunity cost of additional wild life is unhappy neighbors."
&lt;/blockquote&gt;
&lt;br /&gt;
We are not robots and life is not a well written ruby script (as some would like to believe).&lt;br /&gt;
&lt;br /&gt;
You can always look at everything in terms of the lost opportunity cost (and the dollar value associated with it), however, often the lost opportunity cost in dollars is far less valuable as an indicator of how much your time is really worth, and in some cases it can't even be used to accurately describe the lost opportunity cost, such as the opportunity cost of non-tangible things.&lt;br /&gt;
&lt;br /&gt;
For example, spending 2 hours playing with your kids or volunteering and genuinely helping out another human being, is &lt;b&gt;INFINITELY&lt;/b&gt; more important, beneficial and fulfilling than your lost opportunity cost of 300$ that you could be making doing freelance work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So to sum it up, while it's good to have a perspective about how much your time is worth, don't over do it.&lt;br /&gt;
&lt;br /&gt;
There are obvious cases when estimating your time makes perfect sense, but breaking down your entire waking time into lost opportunity costs is a bit too much in my opinion.&lt;br /&gt;
&lt;br /&gt;
Just like with most things in life, aim for moderation.</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/BJAkckZmd6Q/your-time-is-not-worth-that-much.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/12/your-time-is-not-worth-that-much.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-8223264219872265851</guid><pubDate>Sat, 01 Oct 2011 16:16:00 +0000</pubDate><atom:updated>2011-10-01T09:16:06.660-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">agile</category><category domain="http://www.blogger.com/atom/ns#">funny</category><title>Agile Hitler is not Happy</title><description>&lt;iframe width="420" height="315" src="http://www.youtube.com/embed/l1wKO3rID9g" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/uu3gUTYOoAA/agile-hitler-is-not-happy.html</link><author>noreply@blogger.com (IdleWorx)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/l1wKO3rID9g/default.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/10/agile-hitler-is-not-happy.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-2854899576369654261</guid><pubDate>Sat, 24 Sep 2011 01:01:00 +0000</pubDate><atom:updated>2011-09-23T18:01:20.277-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">dao</category><title>MyBatis DAO Example Code Tutorial</title><description>&lt;p&gt;This tutorial will show how how to integrate MyBatis with the Data Access Object pattern (DAO) and MySQL for use in Java Web Apps.&lt;/p&gt;

&lt;p&gt;In case you're not familiar, MyBatis is the new version of the iBatis Data Mapper Java Framework, which allows you to use a relational database with object-oriented applications.&lt;/p&gt;

&lt;p&gt;If you're not familiar with the DAO pattern or the benefits of using it read more about it here &lt;a href="http://www.ibm.com/developerworks/java/library/j-genericdao.html" target="_blank"&gt;Generic implementation of the DAO pattern&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Full code of this mybatis dao example tutorial is available on &lt;a href="https://github.com/idleworx/MyBatisDAOHelper" target="_blank"&gt;github here&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;Step 1 - Define the interface&lt;/h2&gt;
&lt;p&gt;
Create an IParentDAO interface to define the main method calls for all objects. This interface can be used as the parent for various DAO implementations. Here are the default CRUD methods that all objects will have
&lt;/p&gt;

&lt;pre&gt;
get(id)
getByName(name)
getAll()
create(obj)
update(object)
delete(id)
&lt;/pre&gt;

&lt;p&gt;Create the IParentDAO.java class &lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

public interface IParentDAO&amp;lt;T, PK&amp;gt;{
 public T get(PK id) throws PersistenceException;//get obj of type T by the primary key 'id' 
 public T getByName(String name) throws PersistenceException;//get obj of type T by the 'name' field, if one exists for that table
 public ArrayList&amp;lt;T&amp;gt; getAll() throws PersistenceException;//get all objects of type T
 public int create(T objInstance) throws PersistenceException;//insert an object of type T into the database
 int update(T transientObject) throws PersistenceException; //update an object of type T    
 int delete(PK id)  throws PersistenceException;//delete an object of type T
}

&lt;/pre&gt;


&lt;h2&gt;Step 2 - Create the base class&lt;/h2&gt;

&lt;p&gt;Create the MyBatisDAO abstract base class. This will be the default MyBatis implementation of the DAO. You can of course write a different one for JDBC, Spring etc. &lt;i&gt;Feel free to remove the logging code if you don't need it. I used &lt;a href="http://logback.qos.ch/" target="_blank"&gt;Logback&lt;/a&gt; for that.&lt;/i&gt;&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

import java.util.ArrayList;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 
 *  Class contains all the basic CRUD related methods which are inherited by all objects
 *  Children daos should generally not overwrite these method but add extra ones as needed.  
 */
public abstract class MyBatisDAO&amp;lt;T, PK&amp;gt; implements IParentDAO&amp;lt;T, PK&amp;gt;{
 
 private static Logger log = LoggerFactory.getLogger(MyBatisDAO.class);
 private static final String NAMESPACE = &amp;quot;mappers&amp;quot;; 
 
 private SqlSessionFactory sf; //reference to mybatis session factory 
 private Class&amp;lt;T&amp;gt; type;
  
 /** 
  * Define prefixes for easier naming convetions between XML mapper files and the DAO class 
  **/ 
 public static final String PREFIX_SELECT_QUERY = &amp;quot;get&amp;quot;;     //prefix of select queries in mapper files (eg. getAddressType) 
 public static final String PREFIX_INSERT_QUERY = &amp;quot;create&amp;quot;; //prefix of create queries in mapper files (eg. createAddressType)
 public static final String PREFIX_UPDATE_QUERY = &amp;quot;update&amp;quot;;  //prefix of update queries in mapper files (eg. updateAddressType)
 public static final String PREFIX_DELETE_QUERY = &amp;quot;delete&amp;quot;;  //prefix of delete queries in mapper files (eg. deleteAddressType)
 
 /** Default Constructor */
    public MyBatisDAO(Class&amp;lt;T&amp;gt; type,SqlSessionFactory sf) {
        this.type = type;
        this.sf = sf;
        if(sf==null)
   log.error(&amp;quot;Error: Could not instantiate MyBatisDAO. Loading myBatis sessionFactory failed.&amp;quot;);  
    }
    
    /** Use this method to get a session factory for using in any methods impelmented in child dao classes */
    protected SqlSessionFactory getSessionFactory() {
  return sf;
 }

    /** 
     *  Default get by id method. 
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt; 
     *  Almost all objects in the db will 
     *  need this (except mapping tables for multiple joins, which you 
     *  probably shouldn't even have as objects in your model, since proper 
     *  MyBatis mappings can take care of that).
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;
     *  Example: 
     *  &amp;lt;/br&amp;gt;
     *  If your DAO object is called CarInfo.java, 
     *  the corresponding mapper query id should be: &amp;amp;lt;select id=&amp;quot;getCarInfo&amp;quot; ...  
     */
    public T get(PK id) throws PersistenceException {
        
     SqlSession session = sf.openSession(); 
     T obj = null;
  try
  {  
   String query = NAMESPACE+&amp;quot;.&amp;quot;+PREFIX_SELECT_QUERY+this.type.getSimpleName();  //If the object's calls name is AddressType.java, this matches the mapper query id: &amp;quot;namespace.getAddressType&amp;quot;
   obj = (T)session.selectOne(query,id);      
  }
  finally
  {
   session.close();
  }
  return obj;
    }
    
    /** 
     *  Method returns all rows for this object.
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  Example:
     *  &amp;lt;/br&amp;gt;  
     *  If your DAO object is called CarInfo.java, 
     *  the corresponding mapper query id should be: &amp;amp;lt;select id=&amp;quot;getAllCarInfo&amp;quot; ...  
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  SQL Executed: select * from [tablename]
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  Notes:
     *  &amp;lt;/br&amp;gt;   
     *  Consider overdiding this method in order to handle large numbers of objects 
     *  with multiple references.  
     *  LAZY LOADING should be enabled in this case, otherwise you might run out of memory (eg. get all UserAccounts if the table has 1,000,000 rows)
     *  look into the aggresiveLazyLoading property 
     *  */
    public ArrayList&amp;lt;T&amp;gt; getAll() throws PersistenceException {
        
     SqlSession session = sf.openSession(); 
     ArrayList&amp;lt;T&amp;gt; list = null;
  try
  {     
   String query = NAMESPACE+&amp;quot;.&amp;quot;+PREFIX_SELECT_QUERY+&amp;quot;All&amp;quot;+this.type.getSimpleName();
   list = (ArrayList&amp;lt;T&amp;gt;)session.selectList(query); 
  }
  finally
  {
   session.close();
  }   
  return list;
    }
    
    /** 
     *  Method returns first object which matches the given name (exact match).
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  It's up to you to decide what constitutes an object's name. Typically you would have a 
     *  NAME column in the table, but not all objects have this. Generally this method should be overriden (if you need it at all)
     *  in the child dao class.
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;
     *  Example:
     *  &amp;lt;/br&amp;gt;  
     *  If your DAO object is called CarInfo.java, 
     *  the corresponding mapper query id should be: &amp;amp;lt;select id=&amp;quot;getCarInfoByName&amp;quot; ...  
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  SQL Executed (example): select * from [tablename] where NAME = ? 
     *  
     */
    public T getByName(String name) throws PersistenceException {
        
     SqlSession session = sf.openSession();
     T obj = null;
  try
  { 
   String query = NAMESPACE+&amp;quot;.&amp;quot;+PREFIX_SELECT_QUERY+this.type.getSimpleName()+&amp;quot;ByName&amp;quot;;
   obj = (T)session.selectOne(query,name);   
  }
  finally
  {
   session.close();
  }
  return obj;
    }
    
    
    /** 
     *  Method inserts the object into the table.
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;
     *  You will usually override this method, especially if you're inserting associated objects.
     *  &amp;lt;/br&amp;gt; 
     *  Example:
     *  &amp;lt;/br&amp;gt;  
     *  If your DAO object is called CarInfo.java, 
     *  the corresponding mapper query id should be: &amp;amp;lt;insert id=&amp;quot;createCarInfo&amp;quot; ...  
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  SQL Executed (example): insert into [tablename] (fieldname1,fieldname2,...) values(value1,value2...) ... 
     *  
     */
    public int create(T o) throws PersistenceException{        
     SqlSession session = sf.openSession();
     Integer status = null;
     try
  {   
      String query = NAMESPACE+&amp;quot;.&amp;quot;+PREFIX_INSERT_QUERY+o.getClass().getSimpleName();
   status = (Integer)session.insert(query,o);
   session.commit();   
  }
  finally
  {
   session.close();
  }  
  return status;
    }
    
    
    /** 
     *  Method updates the object by id.
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;
     *  You will usually override this method. But it can be used for simple objects.
     *  &amp;lt;/br&amp;gt; 
     *  Example:
     *  &amp;lt;/br&amp;gt;  
     *  If your DAO object is called CarInfo.java, 
     *  the corresponding mapper query id should be: &amp;amp;lt;update id=&amp;quot;updateCarInfo&amp;quot; ...  
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  SQL Executed (example): update [tablename] set fieldname1 = value1 where id = #{id} 
     *  
     */
    public int update(T o)throws PersistenceException {
        
     SqlSession session = sf.openSession();
  Integer status = null;
     try
  {   
   String query = NAMESPACE+&amp;quot;.&amp;quot;+PREFIX_UPDATE_QUERY+o.getClass().getSimpleName();
      status = session.update(query,o);
   session.commit();
   
  }
  finally
  {
   session.close();
  } 
  return status;
     
    }

    
    /** 
     *  Method deletes the object by id.
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;
     *  Example:
     *  &amp;lt;/br&amp;gt;  
     *  If your DAO object is called CarInfo.java, 
     *  the corresponding mapper query id should be: &amp;amp;lt;delete id=&amp;quot;deleteCarInfo&amp;quot; ...  
     *  &amp;lt;/br&amp;gt;&amp;lt;/br&amp;gt;  
     *  SQL Executed (example): update [tablename] set fieldname1 = value1 where id = #{id} 
     *  
     */
    public int delete(PK id)  throws PersistenceException{
  SqlSession session = sf.openSession();
  Integer status = null;
  try
  {   
   String query = NAMESPACE+&amp;quot;.&amp;quot;+PREFIX_DELETE_QUERY+this.type.getSimpleName();
   status = session.delete(query,id);
   session.commit();
  } 
  finally
  {
   session.close();
  } 
  return status;
  
    }
}

&lt;/pre&gt;

&lt;h3&gt;Naming Convetions&lt;/h3&gt;

&lt;p&gt;You'll notice there are four prefix constants defined in the class above.&lt;/p&gt;

&lt;p&gt;The reason for this is to keep consistency between the sql query ids you will define in the mybatis mapper.xml files (see Step 4) and the method names defined in the MyBatisDAO class we're implementing.&lt;/p&gt;

&lt;p&gt;This won't work exactly like ActiveRecord or similar frameworks where there is a pluralization engine but it will still simplify things a lot. &lt;/p&gt;

&lt;p&gt;For example, if you have an object called Status for which you will create a DAO, you will have to define the following mybatis querries&lt;/p&gt;

&lt;style&gt;
#tableNamingConvetions{
        text-align: left;
 border-spacing: 0px;
 border: 1px solid #aeb3b6;
 border-collapse: collapse;
        width:90%;        
}
#tableNamingConvetions td,th{
   font-size:12px;
}

&lt;/style&gt;

&lt;table id="tableNamingConvetions" border="1"&gt;
&lt;tr&gt;
  &lt;th&gt;Java Method&lt;/th&gt;
  &lt;th&gt;MyBatis Query Id&lt;/th&gt;
  &lt;th&gt;Convention&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;dao.get(1)&lt;/td&gt;
  &lt;td&gt;&amp;lt;select id="getStatus" ...&lt;/td&gt;
  &lt;td&gt;get&lt;b&gt;ClassName&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;dao.getAll()&lt;/td&gt;
  &lt;td&gt;&amp;lt;select id="getAllStatus" ...&lt;/td&gt;
  &lt;td&gt;getAll&lt;b&gt;ClassName&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;dao.getByName(String name)&lt;/td&gt;
  &lt;td&gt;&amp;lt;select id="getStatusByName" ...&lt;/td&gt;
  &lt;td&gt;get&lt;b&gt;ClassName&lt;/b&gt;ByName&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;dao.create(obj)&lt;/td&gt;
  &lt;td&gt;&amp;lt;insert id="createStatus" ...&lt;/td&gt;
  &lt;td&gt;create&lt;b&gt;ClassName&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;dao.update(obj)&lt;/td&gt;
  &lt;td&gt;&amp;lt;update id="updateStatus" ...&lt;/td&gt;
  &lt;td&gt;update&lt;b&gt;ClassName&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;dao.delete(id)&lt;/td&gt;
  &lt;td&gt;&amp;lt;delete id="deleteStatus" ...&lt;/td&gt;
  &lt;td&gt;delete&lt;b&gt;ClassName&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Don't worry, this will make a lot more sense once you get to &lt;b&gt;Step 4&lt;/b&gt;&lt;/p&gt;

&lt;h2&gt;Step 3 - Write the actual DAO classes&lt;/h2&gt;

&lt;p&gt;Now you need to implement your concrete DAO classes.&lt;/p&gt;

&lt;p&gt;Let's say you have a simple object called Status which maps to a simple table, and has 2 attributes, an ID and a NAME. Think of it as an object you could use to represent the status of a task. &lt;i&gt;I chose this to illustrate a very basic example here&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;The table for this object would look like this&lt;/p&gt;

&lt;table border="1"&gt;
&lt;tr&gt;
  &lt;th&gt;ID&lt;/th&gt;
  &lt;th&gt;NAME&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt;
  &lt;td&gt;Done&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;2&lt;/td&gt;
  &lt;td&gt;In Progress&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;3&lt;/td&gt;
  &lt;td&gt;Not Started&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;And the java class (or DTO object) would look like this (Status.java)&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

public class Status 
{
 private int id;
 private String name;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {  
  return "[Status] " + "(" + id + ") " + name;
 }
}

&lt;/pre&gt;

&lt;p&gt;Writing the DAO class for the Status object now becomes trivial since you're inheriting all the default CRUD methods from MyBatisDAO.java defined above.&lt;/p&gt; 

&lt;p&gt;Here's how the &lt;b&gt;StatusDAO.java&lt;/b&gt; should look:&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

import org.apache.ibatis.session.SqlSessionFactory;

import com.idleworx.mybatisdao.MyBatisDAO;
import com.idleworx.mybatisdao.tests.objects.Status;

public class StatusDAO extends MyBatisDAO&amp;lt;Status,Integer&amp;gt;{
 
 //Don't forget to define the default custom constructor when implementing a new 
 //child DAO class, and set the class type accordingly 
 public StatusDAO(Class&amp;lt;Status&amp;gt; type,SqlSessionFactory containerSessionFactory) {
  super(type,containerSessionFactory);
 }
 
}

&lt;/pre&gt;

&lt;p&gt;You'll notice here that all you need to do is call the constructor from the parent MyBatisDAO.java class. Now all the default CRUD methods are available for the StatusDAO.java class.&lt;/p&gt;

&lt;p&gt;You are free of course to add any additional methods as needed here that go beyond the CRUD methods that were defined. For example something like&lt;br&gt;&lt;br&gt;&lt;b&gt; getStatusForBlog(Integer blogId)&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;See the &lt;a href="https://github.com/idleworx/MyBatisDAOHelper" target="_blank"&gt;full code example&lt;/a&gt;. &lt;br&gt;&lt;i&gt;Both the MyBatisDAO.java and IParentDAO.java classes are included.&lt;/i&gt;&lt;/p&gt; 

&lt;p&gt;Of course you will still have to define the MyBatis mapper SQL statements for the default CRUD methods defined in MyBatisDAO.java, as well as additional SQLs for other DAO methods you choose to implement. Which brings us to the next step ... &lt;/p&gt;

&lt;h2&gt;Step 4 - Defining the mybatis mapper&lt;/h2&gt;

&lt;p&gt;The last major step in the puzzle, is writing the actual SQL that will implement all the default CRUD methods and any other methods you've implemented for the Status object.&lt;/p&gt;

&lt;p&gt;Your code may differ of course, but note the use of the &lt;b&gt;naming conventions&lt;/b&gt; for naming the sql statements which I talked about earlier. &lt;/p&gt;

&lt;p&gt;That is the key to this whole thing, and it will make your life a lot easier when writing other DAOs which extend the MyBatisDAO class.&lt;/p&gt;

&lt;p&gt;Here is an example of how the mapper file for the StatusDAO object would look like. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;StatusMapper.xml&lt;/b&gt;&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;

&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;
&amp;lt;!DOCTYPE mapper PUBLIC &amp;quot;-//mybatis.org//DTD Mapper 3.0//EN&amp;quot; &amp;quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&amp;quot;&amp;gt;

&amp;lt;mapper namespace=&amp;quot;mappers&amp;quot;&amp;gt;
 
 &amp;lt;select id=&amp;quot;getStatus&amp;quot; parameterType=&amp;quot;int&amp;quot; resultType=&amp;quot;Status&amp;quot;&amp;gt;
  select 
   status_id as &amp;quot;id&amp;quot;,
   status_name as &amp;quot;name&amp;quot;
  from daohelper.status where status_id = #{id}
 &amp;lt;/select&amp;gt;
 
 &amp;lt;select id=&amp;quot;getAllStatus&amp;quot; parameterType=&amp;quot;int&amp;quot; resultType=&amp;quot;Status&amp;quot;&amp;gt;
  select 
   status_id as &amp;quot;id&amp;quot;,
   status_name as &amp;quot;name&amp;quot;
  from daohelper.status order by status_id
 &amp;lt;/select&amp;gt;
 
 &amp;lt;select id=&amp;quot;getStatusByName&amp;quot; parameterType=&amp;quot;int&amp;quot; resultType=&amp;quot;Status&amp;quot;&amp;gt;
  select 
   status_id as &amp;quot;id&amp;quot;,
   status_name as &amp;quot;name&amp;quot;
  from daohelper.status where status_name = #{name}
 &amp;lt;/select&amp;gt;
 
 &amp;lt;insert id=&amp;quot;createStatus&amp;quot; keyColumn=&amp;quot;status_id&amp;quot; useGeneratedKeys=&amp;quot;true&amp;quot; parameterType=&amp;quot;Status&amp;quot;&amp;gt;
  insert into daohelper.status (status_name)
  values (#{name})  
 &amp;lt;/insert&amp;gt;
 
 &amp;lt;update id=&amp;quot;updateStatus&amp;quot; parameterType=&amp;quot;Status&amp;quot;&amp;gt;
  update daohelper.status set status_name = #{name} where status_id = #{id}  
 &amp;lt;/update&amp;gt;
 
 &amp;lt;delete id=&amp;quot;deleteStatus&amp;quot; parameterType=&amp;quot;int&amp;quot;&amp;gt;
  delete from daohelper.status where status_id = #{id}  
 &amp;lt;/delete&amp;gt; 
 
&amp;lt;/mapper&amp;gt;

&lt;/pre&gt;

&lt;p&gt;Note here the use of the &lt;b&gt;mapper&lt;/b&gt; namespace which is the same as referenced by the MyBatisDAO abstract class. &lt;/p&gt;

&lt;p&gt;It's used for convenience in this case, and generally you should be able to use multiple mapper files with the same namespace as long as the you don't have multiple queries with the same id across several xml mapper files (which would be a bad idea anyway)&lt;/p&gt;

&lt;h2&gt;Errors&lt;/h2&gt;

&lt;p&gt;Just a side note on some errors you may encounter.&lt;/p&gt;

&lt;p&gt;If you call one of the default CRUD methods but don't define the appropriate query in the mapper.xml file, you will get an error like this:&lt;/p&gt;

&lt;pre&gt;
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mappers.getAllFrog
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mappers.getAllFrog
&lt;/pre&gt;


&lt;h2&gt;Wrapping it up&lt;/h2&gt;

&lt;p&gt;By this point I hope you've managed to build yourself a small but working DAO implementation based on the MyBatisDAO abstract class. &lt;p&gt;

&lt;p&gt;You can download the &lt;a href="https://github.com/idleworx/MyBatisDAOHelper" target="_blank"&gt;full code example&lt;/a&gt; for more a better overview. &lt;/p&gt;

&lt;p&gt;It also contains a jUnit example, the mybatis configuration file (mybatis.config.xml) as well as the simple DDL statments for setting up the MySQL table.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt;If you need to use this from a Servlet or Spring controller, see my other blog post about &lt;a href="http://blog.idleworx.com/2010/06/initialize-mybatis-servletcontextlisten.html" target="_blank"&gt;defining a servletcontextlistener&lt;/a&gt;

&lt;h3&gt;Hope you found this useful. Improvements or suggestions are welcome.&lt;/h3&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/QlQiOd14LRU/mybatis-dao-example-code-tutorial.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>4</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/09/mybatis-dao-example-code-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-965972119421870947</guid><pubDate>Wed, 29 Jun 2011 00:59:00 +0000</pubDate><atom:updated>2011-06-28T17:59:50.591-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><title>Creating a simple android app with 2 buttons</title><description>&lt;p&gt;How do I build a very simple android app you ask? 
It's easy, building a basic android application is not that hard. 
Making something really useful, cool and bug free, now that takes a good developer.&lt;/p&gt;

&lt;p&gt;
This is a brief tutorial will show you how to code a very basic two button android application (with a start button and a stop button). &lt;/p&gt;
&lt;p&gt;
You can use this code as an android project template or starting point for simple projects. 
&lt;/p&gt;

&lt;h3&gt;Two Button Application Use Case&lt;/h3&gt;

&lt;p&gt;A simple two button application, can be your starting point for various android projects. It can be useful for example when you have an application that starts and stops a service from the click of a button &lt;a href="#"&gt;Watch for tutorial on this soon. &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt;This tutorial assumes you have completed the &lt;a href="http://developer.android.com/resources/tutorials/hello-world.html" target="_blank"&gt;HelloWorld Tutorial&lt;/a&gt; in the Android docs and you are a little familiar with Eclipse and ADT. The tutorial is aimed at developers who are very new to Android.&lt;/p&gt;

&lt;h3&gt;Creating a two button app&lt;/h3&gt;

&lt;h4&gt;1) Create the project &lt;/h4&gt;

&lt;p&gt;In Eclipse create a new Android project and define the properties as shown below. Feel free to use your own package names.&lt;/p&gt;

&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-93u87AJdFjc/Tgp00B7q__I/AAAAAAAAADA/XfLJnv12c-c/s1600/01_create_project.png" imageanchor="1" style=""&gt;&lt;img border="0" height="400" width="263" src="http://3.bp.blogspot.com/-93u87AJdFjc/Tgp00B7q__I/AAAAAAAAADA/XfLJnv12c-c/s400/01_create_project.png" /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;h4&gt;2) Create a simple layout&lt;/h4&gt;

&lt;p&gt;For our main activity we're going to define a very basic layout consisting of one TextView and two Buttons (a start button and a stop button).&lt;/p&gt;

&lt;p&gt;Modify your &lt;b&gt;project/res/layout/main.xml&lt;/b&gt; file to this:&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;

&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
&amp;lt;LinearLayout xmlns:android=&amp;quot;http://schemas.android.com/apk/res/android&amp;quot;
    android:orientation=&amp;quot;vertical&amp;quot;
    android:layout_width=&amp;quot;fill_parent&amp;quot;
    android:layout_height=&amp;quot;fill_parent&amp;quot;
    &amp;gt;
&amp;lt;TextView  
    android:layout_width=&amp;quot;fill_parent&amp;quot; 
    android:layout_height=&amp;quot;wrap_content&amp;quot; 
    android:text=&amp;quot;This is a simple two button app. \r\n \r\n Tell your user what your app does here \r\n \r\n \r\n \r\n&amp;quot;
    android:padding=&amp;quot;10dp&amp;quot;
    android:textColor=&amp;quot;#FFFFFF&amp;quot;
    /&amp;gt;
&amp;lt;Button
 android:id=&amp;quot;@+id/buttonStart&amp;quot;
 android:layout_width=&amp;quot;fill_parent&amp;quot;
 android:layout_height=&amp;quot;wrap_content&amp;quot;
 android:text=&amp;quot;Start&amp;quot;/&amp;gt;
&amp;lt;Button
 android:id=&amp;quot;@+id/buttonStop&amp;quot;
 android:layout_width=&amp;quot;fill_parent&amp;quot;
 android:layout_height=&amp;quot;wrap_content&amp;quot;
 android:text=&amp;quot;Stop&amp;quot;/&amp;gt;
&amp;lt;/LinearLayout&amp;gt;

&lt;/pre&gt;

&lt;p&gt;If you run your project in the emulator it should look like this&lt;/p&gt;

&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-VNxpujxNgkg/Tgp2AD_-ruI/AAAAAAAAADI/Q4ZD88AzCuY/s1600/02_first_peek.png" imageanchor="1" style=""&gt;&lt;img border="0" height="400" width="240" src="http://3.bp.blogspot.com/-VNxpujxNgkg/Tgp2AD_-ruI/AAAAAAAAADI/Q4ZD88AzCuY/s400/02_first_peek.png" /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;h4&gt;3) Adding onClickListeners to the buttons&lt;/h4&gt;

&lt;p&gt;In order for the buttons to actually do something useful, and to provide a template for where you can put future functionality, we'll need to define onClickListeners for each button.&lt;/p&gt;

&lt;p&gt;Let's update the TwoButtonApp.java main activiy with the following code:&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

package com.idleworx.android.twobuttonapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TwoButtonApp extends Activity {
    
 private static String logtag = "TwoButtonApp";//for use as the tag when logging 
  
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button buttonStart = (Button)findViewById(R.id.buttonStart);        
     buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
      
     Button buttonStop = (Button)findViewById(R.id.buttonStop);        
     buttonStop.setOnClickListener(stopListener); // Register the onClick listener with the implementation above
    }
    
    //Create an anonymous implementation of OnClickListener
    private OnClickListener startListener = new OnClickListener() {
        public void onClick(View v) {
          Log.d(logtag,"onClick() called - start button");              
          Toast.makeText(TwoButtonApp.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
          Log.d(logtag,"onClick() ended - start button");
        }
    };
    
    // Create an anonymous implementation of OnClickListener
    private OnClickListener stopListener = new OnClickListener() {
        public void onClick(View v) {
         Log.d(logtag,"onClick() called - stop button"); 
         Toast.makeText(TwoButtonApp.this, "The Stop button was clicked.", Toast.LENGTH_LONG).show();
          Log.d(logtag,"onClick() ended - stop button");
        } 
    };
    
    
    @Override
 protected void onStart() {//activity is started and visible to the user
  Log.d(logtag,"onStart() called");
  super.onStart();  
 }
 @Override
 protected void onResume() {//activity was resumed and is visible again
  Log.d(logtag,"onResume() called");
  super.onResume();
  
 }
 @Override
 protected void onPause() { //device goes to sleep or another activity appears
  Log.d(logtag,"onPause() called");//another activity is currently running (or user has pressed Home)
  super.onPause();
  
 }
 @Override
 protected void onStop() { //the activity is not visible anymore
  Log.d(logtag,"onStop() called");
  super.onStop();
  
 }
 @Override
 protected void onDestroy() {//android has killed this activity
   Log.d(logtag,"onDestroy() called");
   super.onDestroy();
 }
}

&lt;/pre&gt;



&lt;h2&gt;Notes&lt;/h2&gt; 

&lt;ul&gt;
&lt;li&gt;As you can see, each button (or any resource for that matter) can be retrieved by the id was associated with in the layout:
&lt;pre&gt;
@+id/buttonStart 

can be retreived with:

Button buttonStart = (Button)findViewById(R.id.buttonStart);  
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;We have attached two anonymous OnClickListeners() to each of our two buttons&lt;/li&gt;

&lt;li&gt;Each listener displays a simple Toast (a brief message to the user).&lt;/li&gt;

&lt;li&gt;Logging is very important especially during debugging, so make sure you use it where ever you need&lt;/li&gt;

&lt;li&gt;Not the &lt;b&gt;logtag&lt;/b&gt; variable defined in the very top. It's a quick shortcut to save time when logging statements in your app with the Log.X methods. I usually use the same name as the class name and use the Create Filter option in the LogCat view to filter my code out.&lt;/li&gt;

&lt;li&gt;For easier debugging I've added logging of all the other lifecycle methods in the Activity. When you do debugging of your apps it's very important to understand the &lt;a href="http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle" target="_blank"&gt;lifecycle of an android app&lt;/a&gt;, and even more important to see how it's called in relation to your code.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So this is it, Introduction to Android 101. You should now have a very basic android app running. &lt;/p&gt;

&lt;p&gt;&lt;i&gt;For reference here is how your &lt;b&gt;AndroidManifest.xml&lt;/b&gt; file should look (you should not have had to modify this file so far if you have followed this tutorial):
&lt;/i&gt;&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;

&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;
&amp;lt;manifest xmlns:android=&amp;quot;http://schemas.android.com/apk/res/android&amp;quot;
      package=&amp;quot;com.idleworx.android.twobuttonapp&amp;quot;
      android:versionCode=&amp;quot;1&amp;quot;
      android:versionName=&amp;quot;1.0&amp;quot;&amp;gt;
    &amp;lt;uses-sdk android:minSdkVersion=&amp;quot;8&amp;quot; /&amp;gt;

    &amp;lt;application android:icon=&amp;quot;@drawable/icon&amp;quot; android:label=&amp;quot;@string/app_name&amp;quot;&amp;gt;
        &amp;lt;activity android:name=&amp;quot;.TwoButtonApp&amp;quot;
                  android:label=&amp;quot;@string/app_name&amp;quot;&amp;gt;
            &amp;lt;intent-filter&amp;gt;
                &amp;lt;action android:name=&amp;quot;android.intent.action.MAIN&amp;quot; /&amp;gt;
                &amp;lt;category android:name=&amp;quot;android.intent.category.LAUNCHER&amp;quot; /&amp;gt;
            &amp;lt;/intent-filter&amp;gt;
        &amp;lt;/activity&amp;gt;

    &amp;lt;/application&amp;gt;
&amp;lt;/manifest&amp;gt;

&lt;/pre&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/O8li1NvuJjU/build-simple-android-app-2-button.html</link><author>noreply@blogger.com (IdleWorx)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-93u87AJdFjc/Tgp00B7q__I/AAAAAAAAADA/XfLJnv12c-c/s72-c/01_create_project.png" height="72" width="72" /><thr:total>13</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/06/build-simple-android-app-2-button.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-6058755465738657137</guid><pubDate>Mon, 20 Jun 2011 02:55:00 +0000</pubDate><atom:updated>2011-06-19T19:55:23.372-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">android</category><title>First Android App Goes Live</title><description>&lt;p&gt;I've taken an interest in Android App Development a few months ago, and tonight I decided to launch the first android app I created. &lt;/p&gt;

&lt;p&gt;It's a very simple taxi finder app targeted for people in Bucharest, Romania.&lt;/p&gt;
&lt;p&gt;
If you ever find yourself in Bucharest, Romania and you need a taxi, and you happen to have an android phone, make sure to install this app. It may just be what you need. From the Android market you can search for "vreau taxi" or check out the link below:&lt;/p&gt;

&lt;p&gt;
&lt;a href="https://market.android.com/details?id=com.idleworx.vreautaxi&amp;feature=search_result" target="_blank"&gt;VreauTaxi&lt;/a&gt; 
&lt;/p&gt;

&lt;p&gt;The goal of this was to learn the whole process of android app development from top to bottom and I think it went rather well despite a few painful hours dealing with Windows 7 Android USB device installation issues. &lt;/p&gt;

&lt;p&gt;I will write more about Android soon, as some new blog post ideas have been rolling through my head.&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/f-M4GCJdW88/bucharest-romania-taxi-app-android.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>1</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/06/bucharest-romania-taxi-app-android.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-7294767691331236424</guid><pubDate>Wed, 30 Mar 2011 20:33:00 +0000</pubDate><atom:updated>2011-03-30T13:34:44.728-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ruby</category><title>RubyNation 2011</title><description>Going to the RubyNation conference this weekend. Cool stuff.

&lt;a href="http://www.rubynation.org/" target="_blank"&gt;
  &lt;img src="http://www.rubynation.org/images/conference/badges/2011/webBadgesConference.png"/&gt;
&lt;/a&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/-cGx7H3CmYc/rubynation-2011.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/03/rubynation-2011.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-5814408525884501393</guid><pubDate>Fri, 04 Feb 2011 03:35:00 +0000</pubDate><atom:updated>2011-02-03T16:38:30.483-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">eclipse</category><category domain="http://www.blogger.com/atom/ns#">web applications</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>The IdleWorx Monster MyBatis Tutorial</title><description>&lt;h2&gt;Building an entire Java web application with MyBatis 3, Tomcat, MySQL and Eclipse (on Windows 7)&lt;/h2&gt;

&lt;p&gt;This is a multi part tutorial which will show you how to build a Servlet/JSP based web application from scratch, using MySQL, Tomcat and MyBatis (the new version of the iBatis framework) on a windows platform using Eclipse.&lt;/p&gt;

&lt;h3&gt;You will make the most of this tutorial if&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;You have built at least a few small webapplications in the past.&lt;/li&gt;
&lt;li&gt;You know how to setup Tomcat 5.x or higher version&lt;/li&gt;
&lt;li&gt;You are somewhat familiar with the Eclipse IDE&lt;/li&gt;
&lt;li&gt;You are familiar with SQL. &lt;/li&gt;
&lt;li&gt;You know how to set-up MySQL on your development box and integrate it with your java web application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Let's get started&lt;/h3&gt;

&lt;p&gt;To see how Tomcat, MyBatis and MySQL work together we're going to create a tiny java web application in Eclipse, called &lt;b&gt;ModelingAgency&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;If you don't want to follow the whole tutorial, you can skip through to the different sections below and pick out the information you need, otherwise continue to &lt;a href="http://blog.idleworx.com/2011/02/part1-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 1 - The Setup&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Table of Contents&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part1-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 1: The Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part2-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 2: The ModelingAgency Application&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part3-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 3: Creating the ModelingAgency project in Eclipse&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part4-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 4: Creating a Test Servlet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part5-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 5: Setting up Logging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part6-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 6: The Java POJO model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part7-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 7: Configuring MyBatis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part8-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 8: Testing MyBatis with jUnit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part9-idleworx-monster-mybatis-tutorial.html" target="_blank"&gt;Part 9: Creating the DAO Layer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part10-idleworx-monster-mybatis.html" target="_blank"&gt;Part 10: Initializing MyBatis on Application Startup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.idleworx.com/2011/02/part11-idleworx-monster-mybatis.html" target="_blank"&gt;Part 11: Finalizing the Interface&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;At the end of this tutorial you will hopefully be able to create a basic Java JSP/Servlet based webapp with MyBatis, MySQL and Tomcat in an Eclipse environment.&lt;/p&gt;

&lt;p&gt;You can download a full version of finished ModelingAgency webapp &lt;a href="https://sites.google.com/site/idleworxblog/modelingagency.war?attredirects=0&amp;d=1" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;&lt;p&gt;This tutorial took me a long time to write, so if you found it useful in any way, if you found errors with it or you think something is missing, please let me know and I will do my best to improve it.&lt;/p&gt;&lt;/h4&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/YcVI3wFqwQE/mybatis-3-tomcat-mysql-eclipse.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>4</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/mybatis-3-tomcat-mysql-eclipse.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-3499171989800946325</guid><pubDate>Fri, 04 Feb 2011 00:32:00 +0000</pubDate><atom:updated>2011-02-03T16:51:17.797-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">gui</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Part 11: Finalizing the Interface</title><description>&lt;p&gt;If you have followed this tutorial so far, you will probably remember that in &lt;a href="http://blog.idleworx.com/2011/02/part2-idleworx-monster-mybatis-tutorial.html"&gt;Part 2&lt;/a&gt; I had shown you how the GUI will look when we're done. Here it is again:

&lt;br&gt;&lt;br&gt;

&lt;a href="http://3.bp.blogspot.com/_uwTR1dSr5Co/TUrc2HIO4TI/AAAAAAAAACg/eHxP1zLTPyw/s1600/interface01.gif" imageanchor="1" &gt;&lt;img border="0" height="339" width="400" src="http://3.bp.blogspot.com/_uwTR1dSr5Co/TUrc2HIO4TI/AAAAAAAAACg/eHxP1zLTPyw/s400/interface01.gif" /&gt;&lt;/a&gt;

&lt;/p&gt;

&lt;p&gt;We have put together everything except our interface, so it's time to modify our &lt;b&gt;index.jsp&lt;/b&gt; to finalize the interface. Here's how your &lt;b&gt;index.jsp&lt;/b&gt; file should look right now:&lt;/p&gt;

&lt;pre class="brush: html; wrap-lines:false;"&gt;
&amp;lt;%@ taglib prefix=&amp;quot;c&amp;quot; uri=&amp;quot;http://java.sun.com/jsp/jstl/core&amp;quot; %&amp;gt;
&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD HTML 4.01 Transitional//EN&amp;quot; &amp;quot;http://www.w3.org/TR/html4/loose.dtd&amp;quot;&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
 &amp;lt;title&amp;gt;Modeling Agency Index&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;h1&amp;gt;The IdleWorx Modeling Agency&amp;lt;/h1&amp;gt;
&amp;lt;hr&amp;gt;

&amp;lt;div style=&amp;quot;width:300px;float:left;&amp;quot;&amp;gt;
 
 &amp;lt;h2&amp;gt;Available Clients&amp;lt;/h2&amp;gt;
 &amp;lt;c:forEach items=&amp;quot;${all_clients}&amp;quot; var=&amp;quot;client&amp;quot;&amp;gt;
  &amp;lt;a href=&amp;quot;client.do?id=${client.id}&amp;quot;&amp;gt;${client.name}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;
 &amp;lt;/c:forEach&amp;gt; 
&amp;lt;/div&amp;gt;

&amp;lt;div style=&amp;quot;width:300px;float:left;&amp;quot;&amp;gt;
 &amp;lt;h2&amp;gt;Available Models&amp;lt;/h2&amp;gt;
 &amp;lt;c:forEach items=&amp;quot;${all_models}&amp;quot; var=&amp;quot;model&amp;quot;&amp;gt;
  &amp;lt;a href=&amp;quot;model.do?id=${model.id}&amp;quot;&amp;gt;${model.name}&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;
 &amp;lt;/c:forEach&amp;gt; 
&amp;lt;/div&amp;gt;

&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;
 &amp;lt;h2&amp;gt;Bookings&amp;lt;/h2&amp;gt;
 &amp;lt;table border=&amp;quot;1&amp;quot;&amp;gt;
 &amp;lt;tr&amp;gt;
  &amp;lt;th&amp;gt;Client&amp;lt;/th&amp;gt;
  &amp;lt;th&amp;gt;Model&amp;lt;/th&amp;gt;
  &amp;lt;th&amp;gt;Time&amp;lt;/th&amp;gt;
  &amp;lt;th&amp;gt;Place&amp;lt;/th&amp;gt;
 &amp;lt;/tr&amp;gt;
 &amp;lt;c:forEach items=&amp;quot;${all_bookings}&amp;quot; var=&amp;quot;booking&amp;quot;&amp;gt;
 &amp;lt;tr&amp;gt;
  &amp;lt;td&amp;gt;${booking.client.name}&amp;lt;/td&amp;gt;
  &amp;lt;td&amp;gt;${booking.model.name}&amp;lt;/td&amp;gt;
  &amp;lt;td&amp;gt;${booking.time}&amp;lt;/td&amp;gt;
  &amp;lt;td&amp;gt;${booking.location}&amp;lt;/td&amp;gt;
  &amp;lt;td&amp;gt;&amp;lt;/td&amp;gt;
 &amp;lt;/tr&amp;gt;
 &amp;lt;/c:forEach&amp;gt; 
 &amp;lt;/table&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Open up: &lt;a href="http://localhost:8080/ModelingAgency/test.do" target="_blank"&gt;http://localhost:8080/ModelingAgency/test.do&lt;/a&gt;. You should now have an interface very similar to the image above.&lt;/p&gt;

&lt;p&gt;You'll notice I used the JSTL and EL libraries to nicely write our output (please don't use scriptlets in your JSP classes).&lt;/p&gt;

&lt;p&gt;Also if you haven't noticed so far, in your ModelingAgency web application folder, you should have a file called &lt;b&gt;sql.log&lt;/b&gt; wich has logged all of the SQL statements that MyBatis generated. You may find it useful when debugging your MyBatis apps&lt;/p&gt;

&lt;h2&gt;Congrats&lt;/h2&gt;

&lt;p&gt;If you got this far, I hope you now feel more comfortable about MyBatis,Tomcat and MySQL. You should now have all the basics to get a very simple app started in Eclipse using these technologies.&lt;/p&gt;

&lt;p&gt;You can download a full version of the app &lt;a href="https://sites.google.com/site/idleworxblog/modelingagency.war?attredirects=0&amp;d=1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;&lt;p&gt;This tutorial took me a long time to write, so if you found it useful in any way, if you found errors with it or you think something is missing, please let me know and I will do my best to improve it.&lt;/p&gt;&lt;/h4&gt;

&lt;p&gt;Go back to the &lt;a href="http://blog.idleworx.com/2011/02/mybatis-3-tomcat-mysql-eclipse.html"&gt;IdleWorx Monster MyBatis Tutorial&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/vnOlgSfQf6Q/part11-idleworx-monster-mybatis.html</link><author>noreply@blogger.com (IdleWorx)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_uwTR1dSr5Co/TUrc2HIO4TI/AAAAAAAAACg/eHxP1zLTPyw/s72-c/interface01.gif" height="72" width="72" /><thr:total>18</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part11-idleworx-monster-mybatis.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-4890871238098698640</guid><pubDate>Fri, 04 Feb 2011 00:30:00 +0000</pubDate><atom:updated>2011-02-03T16:49:41.735-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Part 10: Initializing MyBatis on Application Startup</title><description>&lt;p&gt;If you've been following &lt;a href="http://blog.idleworx.com/2011/02/part9-idleworx-monster-mybatis-tutorial.html"&gt;Part 9&lt;/a&gt; of this tutorial, we now need to make sure our application has an sqlSessionVariable available ready to go when the TestServlet is invoked.&lt;/p&gt;

&lt;p&gt;I had recently written a more detailed article about how to initialize MyBatis when a web application starts up. You can read that &lt;a href="http://blog.idleworx.com/2010/06/initialize-mybatis-servletcontextlisten.html" target="_blank"&gt;here&lt;/a&gt;.

&lt;p&gt;For this part of the tutorial, I'm just going to summarize what you need to do to get MyBatis to automatically initialize when the ModelingAgency webapp starts.&lt;/p&gt;  

&lt;h2&gt;Setting up a ServletContextListener&lt;/h2&gt;

&lt;p&gt;A ServletContextListener is useful when you want to do something when a java application starts up, more specifically, when a ServletContext is initalized.&lt;/p&gt;

&lt;p&gt;In our case, we're going to use a ServletContextListener to set a MyBatis SqlSessionan application scoped varible. This will allow us to instantiate our ModelingAgencyDAO object from our TestServlet. Here's how to do it: &lt;/p&gt;

&lt;h3&gt;1) Register the listener in web.xml&lt;/h3&gt;

&lt;p&gt;Add the following code to &lt;b&gt;web.xml&lt;/b&gt;&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;
&amp;lt;listener&amp;gt;
   &amp;lt;listener-class&amp;gt;
  com.modelingagency.listeners.CustomServletContextListener
   &amp;lt;/listener-class&amp;gt;
 &amp;lt;/listener&amp;gt;
&lt;/pre&gt;

&lt;h3&gt;2) Create the Listener class&lt;/h3&gt;

&lt;p&gt;Create the class &lt;b&gt;CustomServletContextListener.java&lt;/b&gt; under the &lt;b&gt;com.modelingagency.listeners&lt;/b&gt; package:&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

package com.modelingagency.listeners;
 
import java.io.Reader;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
public class CustomServletContextListener implements ServletContextListener
{
 public void contextInitialized(ServletContextEvent event)
 {  
  ServletContext ctx = event.getServletContext(); 
        
     String resource = "mybatis.config.xml";
     try{      
      Reader reader = Resources.getResourceAsReader(resource);     
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //this will load the default environment configured in mybatis.config.xml
      ctx.setAttribute("sqlSessionFactory", sqlSessionFactory); //set the sqlSessionFactory as an application scoped variable
     }
     catch(Exception e){
      System.out.println("FATAL ERROR: myBatis could not be initialized");
      System.exit(1);
     }    
 }
 
 @Override
 public void contextDestroyed(ServletContextEvent event){
   //nothing to do here right now
 }
}

&lt;/pre&gt;

&lt;p&gt;If everything went well so far, you should be able to run the application again. Going to &lt;a href="http://localhost:8080/ModelingAgency/test.do" target="_blank"&gt;http://localhost:8080/ModelingAgency/test.do&lt;/a&gt; however will show the same things as before. So we need to finalize our interface. &lt;/p&gt;

&lt;h4&gt;&lt;a href="http://blog.idleworx.com/2011/02/part11-idleworx-monster-mybatis.html"&gt;Part 11: Finalizing the Interface&lt;/a&gt;&lt;/h4&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/doBa2BIYz28/part10-idleworx-monster-mybatis.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part10-idleworx-monster-mybatis.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-164456841571150782</guid><pubDate>Fri, 04 Feb 2011 00:26:00 +0000</pubDate><atom:updated>2011-09-23T18:11:17.345-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><category domain="http://www.blogger.com/atom/ns#">dao</category><title>Part 9: Creating the DAO Layer</title><description>&lt;p&gt;
Now that both our Java Data Model and our Database Model are connected through a MyBatis mapper file, we will add a Data Access Object class so we can abstract the calling of SQL statements, and keep our servlet clean of database related code. &lt;/p&gt;

&lt;p&gt;
I know it's not a 'pure' implementation of the DAO pattern however it serves the main purpose of decoupling servlet code from database related code.&lt;/p&gt;

&lt;p&gt;If you want to see a better way to integrate MyBatis and basic DAO classes, check out my other blog post &lt;a href="http://blog.idleworx.com/2011/09/mybatis-dao-example-code-tutorial.html"&gt;MyBatis DAO Example Code Tutorial&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;ModelingAgencyDAO.java&lt;/h2&gt;

&lt;p&gt;ModelingAgencyDAO is going to be a Java class which encapsulates all the 'logical' database functionality we need, such as getting a list of all clients, models and bookings in our database. It will accomplish this by using the MyBatis framework.&lt;/p&gt;

&lt;p&gt;For now, create the file &lt;b&gt;ModelingAgencyDAO.java&lt;/b&gt; under the &lt;b&gt;com.modelingagency.db&lt;/b&gt; package, with the following content:&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

package com.modelingagency.db;

import java.util.ArrayList;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import com.modelingagency.objects.Booking;
import com.modelingagency.objects.Client;
import com.modelingagency.objects.Model;

public class ModelingAgencyDAO {

 private SqlSessionFactory sf;
 
 //constructor will receive a myBatis sessionFactory object
 public ModelingAgencyDAO(SqlSessionFactory containerSessionFactory) {
  if(containerSessionFactory==null)
   System.err.println(&amp;quot;Error: could not load myBatis sessionFactory&amp;quot;);  
  sf = containerSessionFactory;
 }
   
 public ArrayList&amp;lt;Client&amp;gt; getClients() throws PersistenceException{
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList&amp;lt;Client&amp;gt; clients = (ArrayList&amp;lt;Client&amp;gt;)session.selectList(&amp;quot;com.modelingagency.objects.SimpleMappers.allClients&amp;quot;);    
   return clients;  
  }
  finally
  {
   session.close();
  }
  
 }
  
 public ArrayList&amp;lt;Model&amp;gt; getModels() throws PersistenceException{
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList&amp;lt;Model&amp;gt; models = (ArrayList&amp;lt;Model&amp;gt;)session.selectList(&amp;quot;com.modelingagency.objects.SimpleMappers.allModels&amp;quot;);    
   return models;
  }
  finally
  {
   session.close();
  }
  
 }
 
 @Test
 public ArrayList&amp;lt;Booking&amp;gt; getBookings() throws PersistenceException{
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList&amp;lt;Booking&amp;gt; bookings = (ArrayList&amp;lt;Booking&amp;gt;)session.selectList(&amp;quot;com.modelingagency.objects.SimpleMappers.allBookings&amp;quot;);    
   return bookings;   
  }
  finally
  {
   session.close();
  }
  
 }
 
}

&lt;/pre&gt;

&lt;p&gt;You will notice the methods in this DAO class are very similar to the ones we defined in the MyBatisTest class in &lt;a href="http://blog.idleworx.com/2011/02/part8-idleworx-monster-mybatis-tutorial.html"&gt;Part 8&lt;/a&gt;. It's a good idea to have a test case setup for your DAO classes.&lt;/P&gt;

&lt;p&gt;Now in order to use the DAO from a servlet, all you have to do is create a new object and pass in a MyBatis SqlSessionFactory through the constructor. &lt;/p&gt;

&lt;p&gt;Let's modify our TestServlet so we can make use of our DAO class to retreive a list of all clients, a list of all models, and a list of all bookings. Your TestServlet.java class should now look like this:&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

package com.modelingagency.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.modelingagency.db.ModelingAgencyDAO;
import com.modelingagency.objects.*;

public class TestServlet  extends HttpServlet{
 
 private static Logger log = LoggerFactory.getLogger(TestServlet.class);
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
  HttpSession session = request.getSession();   
  ServletContext sc = getServletContext();
  
  SqlSessionFactory sf = (SqlSessionFactory)getServletContext().getAttribute(&amp;quot;sqlSessionFactory&amp;quot;);
  ModelingAgencyDAO dao = new ModelingAgencyDAO(sf);
  
  try
  {
   ArrayList&amp;lt;Client&amp;gt; clients = dao.getClients();    
   ArrayList&amp;lt;Model&amp;gt; models = dao.getModels();    
   ArrayList&amp;lt;Booking&amp;gt; bookings = dao.getBookings();    
   
   request.setAttribute(&amp;quot;all_clients&amp;quot;, clients); 
   request.setAttribute(&amp;quot;all_models&amp;quot;, models);
   request.setAttribute(&amp;quot;all_bookings&amp;quot;, bookings);
       
  }
  catch(PersistenceException p)
  {
   p.printStackTrace();  
  }
  finally
  {
   request.setAttribute(&amp;quot;message&amp;quot;,&amp;quot;Welcome to the modeling agency MyBatis tutorial&amp;quot;);
   log.info(&amp;quot;The message attribute has been set&amp;quot;);
  } 
                
  RequestDispatcher view = request.getRequestDispatcher(&amp;quot;index.jsp&amp;quot;);
  view.forward(request, response);
 }
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request,response);
 }
 
}

&lt;/pre&gt;

&lt;p&gt;But before you can run the web application, you'll need to make sure that MyBatis has been initalized and available to the ModelingAgency app. &lt;h4&gt;&lt;a href="http://blog.idleworx.com/2011/02/part10-idleworx-monster-mybatis.html"&gt;Part 10: Initializing MyBatis on application startup&lt;/a&gt;&lt;/h4&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/NHKVNxc89rw/part9-idleworx-monster-mybatis-tutorial.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>1</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part9-idleworx-monster-mybatis-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-2425245755924263058</guid><pubDate>Fri, 04 Feb 2011 00:24:00 +0000</pubDate><atom:updated>2011-02-03T16:48:30.470-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">junit</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Part 8: Testing MyBatis with jUnit</title><description>&lt;p&gt;To be certain that the MyBatis configuration and Mapper files you setup in &lt;a href="http://blog.idleworx.com/2011/02/part7-idleworx-monster-mybatis-tutorial.html"&gt;Part 7&lt;/a&gt; are working properly, let's create a jUnit test to make sure.&lt;/p&gt;

&lt;p&gt;First, you will need to add the jUnit library to your Eclipse project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Right Click on the ModelingAgency project in Project Explorer&lt;/li&gt;
&lt;li&gt;Go to Properties -&gt; Java Build Path -&gt; Libraries Tab -&gt; Add Library (button)&lt;/li&gt;
&lt;li&gt;Select jUnit4 and click Finish&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Second, create a new package under your webapp called &lt;b&gt;com.modelingagency.tests&lt;/b&gt;. &lt;br&gt;We will keep our jUnit test classes in here.&lt;/p&gt;

&lt;p&gt;Third, add the file &lt;b&gt;MyBatisTest.java&lt;/b&gt; to the &lt;b&gt;com.modelingagency.tests&lt;/b&gt; package:&lt;/p&gt; 

&lt;pre class="brush: java; wrap-lines:false;"&gt;
package com.modelingagency.tests;

import static org.junit.Assert.*;

import java.io.Reader;
import java.util.ArrayList;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.modelingagency.objects.Booking;
import com.modelingagency.objects.Client;
import com.modelingagency.objects.Model;

public class MyBatisTest {
 
 private static Logger log = LoggerFactory.getLogger(MyBatisTest.class);
 private static SqlSessionFactory sf;
 
  
 @BeforeClass
 public static void setUp() throws Exception {
  log.info("starting up myBatis tests");
  String resource = "mybatis.config.xml";
  Reader reader = Resources.getResourceAsReader(resource);  
  sf = new SqlSessionFactoryBuilder().build(reader,"testing"); //we're using the 

'testing' 
 }
 
 @AfterClass
 public static void tearDown() throws Exception {  
  log.info("closing down myBatis tests");
 }
 
 @Test
 public void getClients(){
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList&lt;Client&gt; client = (ArrayList&lt;Client&gt;)session.selectList

("com.modelingagency.objects.SimpleMappers.allClients");    
   assertNotNull("Client list is null",client);   
  }
  finally
  {
   session.close();
  }
  
 }
  
 @Test
 public void getModels(){
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList&lt;Model&gt; model = (ArrayList&lt;Model&gt;)session.selectList

("com.modelingagency.objects.SimpleMappers.allModels");    
   assertNotNull("Model list is null",model);
  }
  finally
  {
   session.close();
  }
  
 }
 
 @Test
 public void getBookings(){
    
  SqlSession session = sf.openSession();
  try
  {   
   ArrayList&lt;Booking&gt; bookings = (ArrayList&lt;Booking&gt;)session.selectList

("com.modelingagency.objects.SimpleMappers.allBookings");    
   assertNotNull("Booking is null",bookings);   
  }
  finally
  {
   session.close();
  }
  
 }
 
 
}
&lt;/pre&gt;

&lt;p&gt;I'm not going into detail into what the MyBatisTest class does, but it should be self explanatory. It loads the MyBatis configuration we have defined, using the 'test' environment, and then runs some test methods against the queries we have defined in the &lt;b&gt;SimpleMappers.xml&lt;/b&gt; mapper file.&lt;/p&gt;  
  
&lt;p&gt;To run the test, right click on the MyBatisTest.java, and select &lt;br&gt;Run As -&gt; jUnit Test&lt;/p&gt;

&lt;p&gt;If everything went well, you should see the following output in the jUnit window: &lt;br&gt;&lt;br&gt;

&lt;a href="http://3.bp.blogspot.com/_uwTR1dSr5Co/TUrvFetlVZI/AAAAAAAAACo/Ea2VAq78rJw/s1600/junittest01.gif" imageanchor="1" &gt;&lt;img border="0" height="132" width="400" src="http://3.bp.blogspot.com/_uwTR1dSr5Co/TUrvFetlVZI/AAAAAAAAACo/Ea2VAq78rJw/s400/junittest01.gif" /&gt;&lt;/a&gt;

&lt;/p&gt;


&lt;p&gt;Now you're ready to move on. &lt;h4&gt;&lt;a href="http://blog.idleworx.com/2011/02/part9-idleworx-monster-mybatis-tutorial.html"&gt;Part 9: Creating the DAO Layer&lt;/a&gt;&lt;/h4&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/-qHVTNr4cG8/part8-idleworx-monster-mybatis-tutorial.html</link><author>noreply@blogger.com (IdleWorx)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/_uwTR1dSr5Co/TUrvFetlVZI/AAAAAAAAACo/Ea2VAq78rJw/s72-c/junittest01.gif" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part8-idleworx-monster-mybatis-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-5191425260512292352</guid><pubDate>Fri, 04 Feb 2011 00:18:00 +0000</pubDate><atom:updated>2011-02-03T16:47:55.511-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">eclipse</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Part 7: Configuring MyBatis</title><description>&lt;p&gt;If you have followed the first parts of this tutorial &lt;a href="http://blog.idleworx.com/2011/02/part1-idleworx-monster-mybatis-tutorial.html"&gt;Part 1&lt;/a&gt; to &lt;a href="http://blog.idleworx.com/2011/02/part6-idleworx-monster-mybatis-tutorial.html"&gt;Part 6&lt;/a&gt; you should now have a very simple MySQL database setup and the java layer ready for the ModelingAgency web application.&lt;/p&gt;

&lt;p&gt;Now I'm going to show you how to integrate your database and java model layer with MyBatis 3&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting up the MyBatis configuration file&lt;/li&gt;
&lt;li&gt;Configuring the mapper file&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;The MyBatis configuration file&lt;/h2&gt;

&lt;p&gt;First it's time to add the main MyBatis configuration file to your web application. You can call this file anything you want, but let's use &lt;b&gt;mybatis.config.xml&lt;/b&gt; for now.&lt;/p&gt;

&lt;p&gt;This file will hold all the MyBatis configuration info and references to all the other sql mapping files we will define.&lt;/p&gt;

&lt;p&gt;Create the following file &lt;b&gt;/src/mybatis.config.xml&lt;/b&gt;:&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;

&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;
&amp;lt;!DOCTYPE configuration PUBLIC &amp;quot;-//mybatis.org//DTD Config 3.0//EN&amp;quot; 
 &amp;quot;http://mybatis.org/dtd/mybatis-3-config.dtd&amp;quot;&amp;gt;
&amp;lt;!-- Created by 2011: http://blog.idleworx.com http://www.idleworx.com --&amp;gt;
&amp;lt;configuration&amp;gt;
 
 &amp;lt;properties resource=&amp;quot;dbcp.properties&amp;quot;/&amp;gt;
  
 &amp;lt;settings&amp;gt;
  &amp;lt;setting name=&amp;quot;useGeneratedKeys&amp;quot; value=&amp;quot;true&amp;quot;/&amp;gt;
 &amp;lt;/settings&amp;gt;
    
 &amp;lt;typeAliases&amp;gt;  
  &amp;lt;typeAlias alias=&amp;quot;Client&amp;quot; type=&amp;quot;com.modelingagency.objects.Client&amp;quot;/&amp;gt;
  &amp;lt;typeAlias alias=&amp;quot;Model&amp;quot; type=&amp;quot;com.modelingagency.objects.Model&amp;quot;/&amp;gt;
  &amp;lt;typeAlias alias=&amp;quot;Booking&amp;quot; type=&amp;quot;com.modelingagency.objects.Booking&amp;quot;/&amp;gt;  
 &amp;lt;/typeAliases&amp;gt;
  
 &amp;lt;environments default=&amp;quot;development&amp;quot;&amp;gt;
  
  &amp;lt;environment id=&amp;quot;development&amp;quot;&amp;gt;
   &amp;lt;transactionManager type=&amp;quot;JDBC&amp;quot;/&amp;gt;
   &amp;lt;dataSource type=&amp;quot;JNDI&amp;quot;&amp;gt;
    &amp;lt;property name=&amp;quot;initial_context&amp;quot; value=&amp;quot;java:comp/env&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;data_source&amp;quot; value=&amp;quot;/jdbc/mydb&amp;quot;/&amp;gt; 
   &amp;lt;/dataSource&amp;gt;
  &amp;lt;/environment&amp;gt;
  
  &amp;lt;environment id=&amp;quot;testing&amp;quot;&amp;gt;
   &amp;lt;transactionManager type=&amp;quot;JDBC&amp;quot;/&amp;gt;
   &amp;lt;dataSource type=&amp;quot;POOLED&amp;quot;&amp;gt;
    &amp;lt;property name=&amp;quot;driver&amp;quot; value=&amp;quot;${db.driver}&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;url&amp;quot; value=&amp;quot;${db.url}&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;username&amp;quot; value=&amp;quot;${db.user}&amp;quot;/&amp;gt;
    &amp;lt;property name=&amp;quot;password&amp;quot; value=&amp;quot;${db.pass}&amp;quot;/&amp;gt;
   &amp;lt;/dataSource&amp;gt;
  &amp;lt;/environment&amp;gt;
    
 &amp;lt;/environments&amp;gt;

 &amp;lt;mappers&amp;gt;
  &amp;lt;mapper resource=&amp;quot;com/modelingagency/objects/SimpleMappers.xml&amp;quot;/&amp;gt;
 &amp;lt;/mappers&amp;gt;

&amp;lt;/configuration&amp;gt;

&lt;/pre&gt;

&lt;p&gt;Also, create the following file &lt;b&gt;/src/dbcp.properties&lt;/b&gt;:&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;
db.url=jdbc:mysql://localhost/modeling_agency
db.driver=com.mysql.jdbc.Driver
db.user=test
db.pass=test
&lt;/pre&gt;

&lt;h3&gt;Notes&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;You'll notice we have a 'testing' environment setup. We'll use that for testing our app with jUnit.&lt;/li&gt;
&lt;li&gt;Using the &lt;b&gt;useGeneratedKeys&lt;/b&gt; property will allow MyBatis to make use of the autogenerated row id values from the database, as we defined in the SQL in &lt;a href="http://blog.idleworx.com/2011/02/part2-idleworx-monster-mybatis-tutorial.html"&gt;Part 2&lt;/a&gt;: &lt;i&gt;client_id INTEGER NOT NULL AUTO_INCREMENT&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;typeAliases&lt;/b&gt; will simplify things for you, by helping MyBatis identify fully qualified java class names. This way you won't have to type the fully qualified names of your objects in MyBatis mapper files&lt;/li&gt; 
&lt;li&gt;In a typical application there are usually many SQL mapper files, however for our simple application we only really need one, the SimpleMappers.xml file&lt;/li&gt;
&lt;li&gt;The &lt;b&gt;mybatis.config.xml&lt;/b&gt; file should go under &lt;b&gt;/src/mybatis.config.xml&lt;/b&gt;   (this is because the file should be on the application's classpath)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Tip:&lt;/b&gt;If you're wondering how to setup multiple environments in MyBatis see my other blog posting: &lt;a href="http://blog.idleworx.com/2010/06/initialize-mybatis-servletcontextlisten.html" target="_blank"&gt;Initializing MyBatis Using a ServletContextListener&lt;/a&gt;&lt;/p&gt;        


&lt;h2&gt;Configuring the Mapper file&lt;/h2&gt;

&lt;p&gt;MyBatis SQL mappers allow us to map our java data model to our database model. &lt;/p&gt;

&lt;p&gt;The mapper file holds all the sql statments you need to interact with your database, so it's pretty important, and it also happens to be a critical element of the MyBatis framework, which will simplify your life a lot, once you pass the learning curve :)&lt;/p&gt;

&lt;p&gt;The mapper file also stores definitions of objects that will be used to retrieve data from the database. By doing this MyBatis abstracts most of the hard work of writing tons of sql statements, and most importantly jdbc code.&lt;/p&gt;

&lt;p&gt;Since we've configured this file's location (&lt;b&gt;com/modelingagency/objects/SimpleMappers.xml&lt;/b&gt;) in the &lt;b&gt;mybatis.config.xml&lt;/b&gt; file above, MyBatis now knows to load this resource whenever it is initialized.&lt;/p&gt;

&lt;br&gt;

&lt;p&gt;The main functionality we're going to need now in the SimpleMapper.xml file is this: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1) Getting a list of clients&lt;/li&gt;
&lt;li&gt;2) Getting a list of models&lt;/li&gt;
&lt;li&gt;3) Getting a list of all bookings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create the following file: &lt;b&gt;/src/com/modelingagency/objects/SimpleMappers.xml&lt;/b&gt;:&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;

&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;
&amp;lt;!DOCTYPE mapper PUBLIC &amp;quot;-//mybatis.org//DTD Mapper 3.0//EN&amp;quot; &amp;quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&amp;quot;&amp;gt;
&amp;lt;!-- Created by: http://blog.idleworx.com, http://www.idleworx.com --&amp;gt;
&amp;lt;mapper namespace=&amp;quot;com.modelingagency.objects.SimpleMappers&amp;quot;&amp;gt;
 
 &amp;lt;resultMap id=&amp;quot;clientResultMap&amp;quot; type=&amp;quot;Client&amp;quot;&amp;gt;
  &amp;lt;id property=&amp;quot;id&amp;quot; column=&amp;quot;client_id&amp;quot; /&amp;gt;  
  &amp;lt;result property=&amp;quot;name&amp;quot; column=&amp;quot;client_name&amp;quot; /&amp;gt;  
 &amp;lt;/resultMap&amp;gt;
 
 &amp;lt;resultMap id=&amp;quot;modelResultMap&amp;quot; type=&amp;quot;Model&amp;quot;&amp;gt;
  &amp;lt;id property=&amp;quot;id&amp;quot; column=&amp;quot;model_id&amp;quot; /&amp;gt;  
  &amp;lt;result property=&amp;quot;name&amp;quot; column=&amp;quot;model_name&amp;quot; /&amp;gt; 
 &amp;lt;/resultMap&amp;gt;
  
 &amp;lt;resultMap id=&amp;quot;bookingResultMap&amp;quot; type=&amp;quot;Booking&amp;quot;&amp;gt;
  &amp;lt;id property=&amp;quot;id&amp;quot; column=&amp;quot;booking_id&amp;quot; /&amp;gt;  
  &amp;lt;result property=&amp;quot;time&amp;quot; column=&amp;quot;booking_time&amp;quot; /&amp;gt;    
  &amp;lt;result property=&amp;quot;location&amp;quot; column=&amp;quot;booking_location&amp;quot; /&amp;gt;
  &amp;lt;association property=&amp;quot;client&amp;quot; column=&amp;quot;booking_client_id&amp;quot; javaType=&amp;quot;Client&amp;quot; resultMap=&amp;quot;clientResultMap&amp;quot;/&amp;gt;
  &amp;lt;association property=&amp;quot;model&amp;quot; column=&amp;quot;booking_model_id&amp;quot; javaType=&amp;quot;Model&amp;quot; resultMap=&amp;quot;modelResultMap&amp;quot;/&amp;gt;
 &amp;lt;/resultMap&amp;gt; 
  
 &amp;lt;select id=&amp;quot;allClients&amp;quot; parameterType=&amp;quot;string&amp;quot; resultMap=&amp;quot;clientResultMap&amp;quot;&amp;gt;
  select  * from client order by 1  
 &amp;lt;/select&amp;gt;
 
 &amp;lt;select id=&amp;quot;allModels&amp;quot; parameterType=&amp;quot;string&amp;quot; resultMap=&amp;quot;modelResultMap&amp;quot;&amp;gt;
  select  * from model order by 1
 &amp;lt;/select&amp;gt;
 
 &amp;lt;select id=&amp;quot;allBookings&amp;quot; parameterType=&amp;quot;string&amp;quot; resultMap=&amp;quot;bookingResultMap&amp;quot;&amp;gt;
  select * from client, model, booking
  where booking_client_id = client_id and booking_model_id = model_id
  order by 1
 &amp;lt;/select&amp;gt;
 
&amp;lt;/mapper&amp;gt;

&lt;/pre&gt;

&lt;h3&gt;Notes&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The name of each query is identified by the 'id' parameter&lt;/li&gt;
&lt;li&gt;&lt;b&gt;parameterType&lt;/b&gt; indicates what type of parameter you will pass into the sql statement (for simple queries there is only one parameter or none), for queries which require multiple parameters you can use the 'hashmap' parameterType to pass in name value pairs. In our case the parameter is not actually used.&lt;/li&gt;
&lt;li&gt;By defining &lt;b&gt;result maps&lt;/b&gt; for each of our objects, MyBatis knows how to automatically map a column from the database table directly to the javabean property name (in our case the Client,Model and Booking classes)&lt;/li&gt;
&lt;li&gt;If you don't use Result Maps in your mapper files, you can use the &lt;b&gt;resultType&lt;/b&gt; attribute instead which specifies the Java Bean class name we want to map, and which is defined in &lt;b&gt;mybatis.config.xml&lt;/b&gt;. Normally you have to put a fully qualified class name here like com.modelingagency.objects.Model, but because if you defined the &lt;b&gt;typeAliases&lt;/b&gt; element in the &lt;b&gt;mybatis.config.xml&lt;/b&gt; you can just use the short name&lt;/li&gt;
&lt;li&gt;The &lt;b&gt;allBookings&lt;/b&gt; query makes use of MyBatis associations. 
Associations are what MyBatis uses to retrieve one-to-many relationships from your database into java objects. To learn more about this read: &lt;a href="http://blog.idleworx.com/2010/10/mybatis-multiple-table-joins.html" target="_blank"&gt;Understanding MyBatis' Multiple Table Joins&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;To make sure the MyBatis configuration and mapper files are all setup correctly, before we create our DAO layer and use it from our Servlet, we need to do a quick test. &lt;h4&gt;&lt;a href="http://blog.idleworx.com/2011/02/part8-idleworx-monster-mybatis-tutorial.html"&gt;Part 8: Testing MyBatis with jUnit&lt;/a&gt;&lt;/h4&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/MgmR8qcuLlM/part7-idleworx-monster-mybatis-tutorial.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>1</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part7-idleworx-monster-mybatis-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-2303124149962791284</guid><pubDate>Fri, 04 Feb 2011 00:13:00 +0000</pubDate><atom:updated>2011-02-03T16:47:24.834-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">pojo</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Part 6: The Java POJO model</title><description>&lt;p&gt;If you've been following &lt;a href="http://blog.idleworx.com/2011/02/mybatis-3-tomcat-mysql-eclipse.html"&gt;IdleWorx's Tutorial on Building a Web Application with MyBatis,MySQL,Tomcat and Eclipse &lt;/a&gt;, you should be now ready to setup the database layer of the ModelingAgency webapp.&lt;/p&gt;

&lt;p&gt;In order to be able to interact with the database we setup in &lt;a href="http://blog.idleworx.com/2011/02/part2-idleworx-monster-mybatis-tutorial.html"&gt;Part 2&lt;/a&gt; we will create a simple java model made of POJOs (simple JavaBeans that is).&lt;/p&gt;

&lt;p&gt;These Plain Old Java Objects (POJOs) will serve as the model, or in other words, a way to transfer data between the database and the ModelingAgency webapp.&lt;/p&gt;

&lt;p&gt;At this point we're not concerned about how we'll actually retrieve the data from the database into our model classes, that is where MyBatis will come in later on and take care of that for us.&lt;/p&gt;

&lt;p&gt;If you remember our database layout from &lt;a href="http://blog.idleworx.com/2011/02/part2-idleworx-monster-mybatis-tutorial.html"&gt;Part 2&lt;/a&gt;, we have 3 business objects in our application that we need to model:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Client&lt;/li&gt;&lt;li&gt;Model&lt;/li&gt;&lt;li&gt;Booking&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;So let's add the following classes to the com.modelingagency.objects package:&lt;/p&gt;

&lt;p&gt;Client.java&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

package com.modelingagency.objects;

/**
 * Copyright 2011: http://blog.idleworx.com, http://www.idleworx.com   
 */
public class Client {
 private Integer id;
 private String name;
 
 public Client(){}
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "[client] " + "(" + id + ")" + " " + name;
 }
}

&lt;/pre&gt;

&lt;p&gt;Model.java&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;
package com.modelingagency.objects;

/**
 * Copyright 2011: http://blog.idleworx.com, http://www.idleworx.com   
 */
public class Model {

 private Integer id;
 private String name;
 
 public Model() {}
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "[model] " + "(" + id + ")" + " " + name;
 }
}
&lt;/pre&gt;

&lt;p&gt;Booking.java&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;
package com.modelingagency.objects;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Copyright 2011: http://blog.idleworx.com, http://www.idleworx.com   
 */
public class Booking {

 private Integer id;
 private Client client;
 private Model model;
 private Timestamp time;
 private String location;
 
 public Booking(){}
 
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public Client getClient() {
  return client;
 }
 public void setClient(Client client) {
  this.client = client;
 }
 public Model getModel() {
  return model;
 }
 public void setModel(Model model) {
  this.model = model;
 }
 public Timestamp getTime() {
  return time;
 }
 public void setTime(Timestamp time) {
  this.time = time;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 }
 
 @Override
 public String toString() {
  return "[booking] " + "(" + id + ")" + " where: " + location + " when: " + new Date(time.getTime());
 }
 
}

&lt;/pre&gt;

&lt;p&gt;Note that the Booking model class doesn't have getters for the booking_client_id and booking_model_id fields which are part of the booking table.&lt;/p&gt;

&lt;p&gt;We're not going to model each field directly into our java beans,
instead we're going to let MyBatis retrieve the actual Client and Model objects whenever a Booking object is returned from the database. And we're going to accomplish this through the use of MyBatis Result Maps, as you will shortly see.&lt;/p&gt; 

&lt;p&gt;Before we do that however, we need to setup MyBatis. &lt;h4&gt;&lt;a href="http://blog.idleworx.com/2011/02/part7-idleworx-monster-mybatis-tutorial.html"&gt;Part 7: Configuring MyBatis&lt;/a&gt;&lt;/h4&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/APmnzGZ3O6o/part6-idleworx-monster-mybatis-tutorial.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part6-idleworx-monster-mybatis-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4368777317649012081.post-7768779678312135211</guid><pubDate>Fri, 04 Feb 2011 00:10:00 +0000</pubDate><atom:updated>2011-02-03T16:46:46.744-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">mybatis</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><category domain="http://www.blogger.com/atom/ns#">logback</category><category domain="http://www.blogger.com/atom/ns#">tomcat</category><title>Part 5: Setting up Logging</title><description>&lt;p&gt;If you've followed &lt;a href="http://blog.idleworx.com/2011/02/part4-idleworx-monster-mybatis-tutorial.html"&gt;Part 4&lt;/a&gt; of this tutorial, we're about to setup logging for the ModelingAgency webapp in order to learn how to use MySQL, MyBatis and Tomcat togather.&lt;/p&gt;

&lt;p&gt;If you're familiar with Log4J you can use that library. However for the ModelingAgency webapp we're going to setup the Logback library, which is the new Log4J.&lt;/p&gt;

&lt;p&gt;You have probably already downloaded the Logback jars in &lt;a href="http://blog.idleworx.com/2011/02/part1-idleworx-monster-mybatis-tutorial.html"&gt;Part 1&lt;/a&gt;. If not, you can download all of the jars required for the ModelingAgency app &lt;a href="https://sites.google.com/site/idleworxblog/jar_files_modelingagency.zip?attredirects=0&amp;d=1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Configuring Logback&lt;/h2&gt;

&lt;p&gt;Configuring Logback with a Java web application is relatively simple. Once you have the required jars in &lt;b&gt;/WEB-INF/lib&lt;/b&gt;, create a file called &lt;b&gt;logback.xml&lt;/b&gt; and put it under your project's &lt;b&gt;/src&lt;/b&gt; folder.&lt;/p&gt;&lt;p&gt;Here is the &lt;b&gt;logback.xml&lt;/b&gt; file we we will use for this project:&lt;/p&gt;

&lt;pre class="brush: xml; wrap-lines:false;"&gt;
&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;
&amp;lt;configuration scan=&amp;quot;true&amp;quot; debug=&amp;quot;false&amp;quot; &amp;gt; &amp;lt;!-- set debug to true to dump logback status info when library is loaded --&amp;gt;
 
  &amp;lt;contextName&amp;gt;ModelingAgency&amp;lt;/contextName&amp;gt;
  
  &amp;lt;!-- APPENDERS --&amp;gt;
  
  &amp;lt;appender name=&amp;quot;STDOUT&amp;quot; class=&amp;quot;ch.qos.logback.core.ConsoleAppender&amp;quot;&amp;gt;    
    &amp;lt;!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --&amp;gt;
    &amp;lt;encoder&amp;gt; 
      &amp;lt;pattern&amp;gt;[%contextName] %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n&amp;lt;/pattern&amp;gt;
    &amp;lt;/encoder&amp;gt;
  &amp;lt;/appender&amp;gt;
  
  &amp;lt;appender name=&amp;quot;SQLDUMP&amp;quot; class=&amp;quot;ch.qos.logback.core.FileAppender&amp;quot;&amp;gt;
    &amp;lt;file&amp;gt;sql.log&amp;lt;/file&amp;gt;
    &amp;lt;append&amp;gt;false&amp;lt;/append&amp;gt;
    &amp;lt;encoder&amp;gt;
      &amp;lt;!-- &amp;lt;pattern&amp;gt;%date %level [%thread] %logger{10} [%file:%line] %msg%n&amp;lt;/pattern&amp;gt; --&amp;gt;
      &amp;lt;pattern&amp;gt;[%contextName] [%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n&amp;lt;/pattern&amp;gt;      
    &amp;lt;/encoder&amp;gt;
  &amp;lt;/appender&amp;gt;
  
  &amp;lt;appender name=&amp;quot;FILE&amp;quot; class=&amp;quot;ch.qos.logback.core.FileAppender&amp;quot;&amp;gt;
    &amp;lt;file&amp;gt;application.log&amp;lt;/file&amp;gt;
    &amp;lt;encoder&amp;gt;
      &amp;lt;!-- &amp;lt;pattern&amp;gt;%date %level [%thread] %logger{10} [%file:%line] %msg%n&amp;lt;/pattern&amp;gt; --&amp;gt;
      &amp;lt;pattern&amp;gt;[%contextName] [%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n&amp;lt;/pattern&amp;gt;      
    &amp;lt;/encoder&amp;gt;
  &amp;lt;/appender&amp;gt;
  
  &amp;lt;appender name=&amp;quot;OUT&amp;quot; class=&amp;quot;ch.qos.logback.core.ConsoleAppender&amp;quot;&amp;gt;    
    &amp;lt;encoder&amp;gt;
    &amp;lt;pattern&amp;gt;[%contextName] [%c] [%d{dd MMM yyyy - hh:mm:ss}] %5p - %m %n&amp;lt;/pattern&amp;gt;        
    &amp;lt;/encoder&amp;gt;
  &amp;lt;/appender&amp;gt;
  
   &amp;lt;appender name=&amp;quot;OUT_SIMPLE&amp;quot; class=&amp;quot;ch.qos.logback.core.ConsoleAppender&amp;quot;&amp;gt;    
    &amp;lt;encoder&amp;gt;
    &amp;lt;pattern&amp;gt;[%c] %5p - %m %n&amp;lt;/pattern&amp;gt;        
    &amp;lt;/encoder&amp;gt;
  &amp;lt;/appender&amp;gt;
        
  &amp;lt;!-- LOGGERS --&amp;gt;  
    
  &amp;lt;!--  loggers inherit the appender from their parent ( the root logger ) --&amp;gt;
  &amp;lt;logger name=&amp;quot;java.sql&amp;quot; level=&amp;quot;DEBUG&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt; 
   &amp;lt;appender-ref ref=&amp;quot;SQLDUMP&amp;quot; /&amp;gt; 
  &amp;lt;/logger&amp;gt;
   
  &amp;lt;logger name=&amp;quot;org.apache.ibatis.datasource&amp;quot; level=&amp;quot;WARN&amp;quot;/&amp;gt; &amp;lt;!-- removes datasoure debug info from logs --&amp;gt; 
       
  &amp;lt;root level=&amp;quot;debug&amp;quot;&amp;gt; &amp;lt;!-- can set level to OFF--&amp;gt;
    &amp;lt;appender-ref ref=&amp;quot;OUT&amp;quot; /&amp;gt;
  &amp;lt;/root&amp;gt;
  
&amp;lt;/configuration&amp;gt;

&lt;/pre&gt;



&lt;h2&gt;Adding logging to TestServlet&lt;/h2&gt;

&lt;p&gt;To make sure logback works properly, let's add some simple logging code to the TestServlet we created in &lt;a href="http://blog.idleworx.com/2011/02/part4-idleworx-monster-mybatis-tutorial.html"&gt;Part 4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the following logging code to the TestServlet.java file:&lt;/p&gt;

&lt;pre class="brush: java; wrap-lines:false;"&gt;

package com.modelingagency.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestServlet  extends HttpServlet{
 
 private static Logger log = LoggerFactory.getLogger(TestServlet.class);
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
  HttpSession session = request.getSession();   
  ServletContext sc = getServletContext();
  
  request.setAttribute("message","This is a message from the TestServlet. It works!");
  log.info("The message attribute has been set");
             
  RequestDispatcher view = request.getRequestDispatcher("index.jsp");
  view.forward(request, response); 
 }
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request,response);
 }
 
}

&lt;/pre&gt;


&lt;p&gt;Restart your project now, by right clicking on the &lt;b&gt;index.jsp&lt;/b&gt; file and selecting &lt;b&gt;Run on server&lt;/b&gt;.&lt;/p&gt;

&lt;p&gt;If you access the TestServlet's url on your local server now, &lt;a href="http://localhost:8080/ModelingAgency/test.do"&gt;http://localhost:8080/ModelingAgency/test.do&lt;/a&gt; you should get an output like this: &lt;/p&gt;

&lt;p&gt;&lt;b&gt;[ModelingAgency] [com.modelingagency.servlets.TestServlet] [03 Feb 2011 - 10:38:26]  INFO - The message attribute has been set &lt;/b&gt;&lt;/p&gt;


&lt;p&gt;If you got this far, it means you have setup Logback logging for the ModelingAgency webapp.&lt;/p&gt;

&lt;p&gt;Now the fun part, &lt;h4&gt;&lt;a href="http://blog.idleworx.com/2011/02/part6-idleworx-monster-mybatis-tutorial.html"&gt;Part 6: Creating the java pojo model&lt;/a&gt;&lt;/p&gt;&lt;/h4&gt;</description><link>http://feedproxy.google.com/~r/idleworxblog/~3/WB1xIAzzEnA/part5-idleworx-monster-mybatis-tutorial.html</link><author>noreply@blogger.com (IdleWorx)</author><thr:total>0</thr:total><feedburner:origLink>http://blog.idleworx.com/2011/02/part5-idleworx-monster-mybatis-tutorial.html</feedburner:origLink></item></channel></rss>
