Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

iDao - a New Way of Data Access Object Development

September 27, 2009 AT 9:52 AM
  • submit to reddit
Subheadline: 
Writing data access layer in Java interface

I developed a new tiny library, iDao, to help developing data access object.
Here's what the code looks like by using iDao.

public interface TestDao {
    @Update("INSERT INTO TEST_TABLE (COL_ONE, COL_TWO) VALUES (?<1>?, ?<2>?)")
    int insertDataWithIndexParameter(int intValue, String varcharValue);

    @Update("INSERT INTO TEST_TABLE (COL_ONE, COL_TWO) VALUES (?<fieldone>?, ?<fieldtwo>?)")
    void insertDataWithBeanParameter(TestBean bean);
    @Query("select COL_ONE from TEST_TABLE")
    List<integer> findAllIntColumns();

    @Query("select col_two from test_table where COL_ONE=?<1>? and COL_TWO=?<2>?")
    List<string> findStringColumnsWithIndexParameter(int intValue, String varcharValue);

    @Query("select COL_ONE fieldOne, COL_TWO fieldTwo from TEST_TABLE where COL_ONE = ?<fieldone>?")
    List<testbean> findIntoBeanWithBeanParameter(TestBean bean);

    @Query("select COL_TWO fieldTwo from TEST_TABLE where COL_ONE = ?<1.fieldOne>?")
    String findSimpleValueWithIndexedBeanParameter(TestBean bean);
}
</testbean></fieldone></string></integer></fieldtwo></fieldone>

Yes, just
  1. declare the DAO interfaces that are called by Service layer, and
  2. methods used to access database, and
  3. SQL statement to be executed by each method and arguments if any.
That's it, no implementation, iDao will generate one at runtime and takes care of any other things.

Please take a look at the Getting Started guide on the project's website and tell me if this is useful. If it is I'd be more than happy and will give me motivation to keep improving it, to make it better.

Thanks a lot!

0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Edvin Syse replied on Sun, 2009/09/27 - 3:42pm

You should have a look at iBATIS 3.0, which does this in a neater way, and also does full blown/very mature O/R mapping :)

Also, given the shortcomings of string handling in Java, this approach becomes tedious when the SQL sentence is long and there are multiple joins, or when the executed SQL should change based on certain conditions. In these cases, you can seamlessly switch to the XML variant in iBATIS to describe your queries and mappings, or even use the criteria API.

http://ibatis.apache.org

hantsy bai replied on Mon, 2009/09/28 - 9:38am

There are many projects do similar works.

The first time I noticed is java 6 beta( with jdbc 4 draft), but in the java 6 final release it has been removed.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.