Make your SQL Maps Groovy!

myndian.de

Samstag, 15. Dezember 2007

Make your SQL Maps Groovy!

At work we mainly use Java as general purpose language, but we also pay attention to groovy. There are other scripting languages which are more mature and with a much better syntax, but for our purpose - scripting some Java applications and frameworks - it’s best suited. (You may read a nice article from Fowler about that.)


In one case we use IBatis in the persistence layer and startet to write services which use our SqlMapDaos in Groovy. So I thought about an generic DAO for SQL-Maps using groovy. I had implemented a DAO using Spring Aspects before, like in this article about a generic Hibernate DAO. But this requires at least to define an interface for the DAO. But because Groovy it uses dynamic typing, we can even drop that also.


I played around with different implementations and here’s an excerpt of a very simple one using only groovy. I shows very much the power of groovy over java, when using dynamic typing.

public class GroovyIBatisDao extends SqlMapClientDaoSupport {

  def methodMapping = [
      // findAll must be before find
      “findAll” : “queryForList”,
      “find”    : “queryForObject”,
      “insert”  : “insert”,
      “update”  : “update”,
      “delete”  : “delete”
  ]
  
  Object invokeMethod(String method, Object arguments) {
    String useMethod = methodMapping.find{entry ->
        method.startsWith(entry.key)
    }.value

    getSqlMapClientTemplate().“${useMethod}”(method, arguments[0])
  }

}

Short, eh!? You can use it that way:

public class ExampleService {

  def dao;

  def doSomething(foo, bar) {
    def thing = dao.findThingByFoo foo
    def some = dao.findAllThingsByBar bar
    thing.doItWith some
    dao.updateThing thing
  }

}

You only have to implement the three statements “findThingByFoo”, “findAllThingsByBar” and “updateThing” in the SQL Map and add a little configuration to your Spring context. You could even define your own “methodMapping” in the configuration.


As mentioned this is an trivial implementation. I added also added support for SQL Map namespaces. For performance reasons it’s good to implement missingMethod instead of invokeMethod and to create a new method for the DAO object. The main overhead will only be executed once per dynamic method. Also the mapping could be better - perhaps based on regular expressions - and we could support more SQL Map features. Perhaps more next week.

Geschrieben von Jörg in Softwaretechnik um 13:37 | Kommentare (0) | Trackback (1)
Tags für diesen Artikel: dynamic typing, groovy, ibatis, java, spring framework, sql maps
Artikel mit ähnlichen Themen:
  • Grails tutorials and a free e-book
  • Runnig Grails
  • Simpler implementation of ApplicationContextAware
  • Premature optimization and object orientation
  • POJOs, POGOs und POCOs
  • View technologys for generated web layers
  • More about Groovy SQL Maps
  • Groovy again
  • Blogroll / Springify
  • last language war language trolling

Trackbacks
Trackback für spezifische URI dieses Eintrags

More about Groovy SQL Maps.
A week ago I posted about a GroovyIBatisDao. Here I like to tell you about a few details as I announced. First I added Support for SQL Map namespaces which are mandatory in our architecture. To avoid to much configuration I used a naming convention: Fo Kommentar (1)
Weblog: Myndian.de
Aufgenommen: Dez 22, 18:40

Kommentare
Ansicht der Kommentare: (Linear | Verschachtelt)

Noch keine Kommentare

Kommentar schreiben

Umschließende Sterne heben ein Wort hervor (*wort*), per _wort_ kann ein Wort unterstrichen werden.
Standard-Text Smilies wie :-) und ;-) werden zu Bildern konvertiert.
Die angegebene E-Mail-Adresse wird nicht dargestellt, sondern nur für eventuelle Benachrichtigungen verwendet.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss um dieses Verfahren anzuwenden.
CAPTCHA

Sie können [geshi lang=LANG][/lang] Tags verwenden um Quellcode abhängig von der gewählten Programmiersprache einzubinden
 
 

Suche

Statische Seiten

Startseite
Galerien
Impressum

Kategorien

  • XML Alltag
  • XML Internet
  • XML Musik
  • XML Politik
  • XML Reise
  • XML Softwaretechnik
  • XML Sonstiges
  • XML Visuelles

Alle Kategorien

Archive

Mai 2013
April 2013
März 2013
Das Neueste ...
Älteres ...

Blog abonnieren

XML RSS 2.0 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Kommentare

Login

Verwaltung des Blogs

Login

Aktuelle Einträge

Netzwerkkultur verändert die Gesellschaft
Dienstag, 17. November

Absolute and relative date and time
Sonntag, 18. Oktober

Oren Lavie - Her Morning Elegance
Dienstag, 6. Oktober

Twitter & Blogroll
Samstag, 8. August

Read It Later: Round-Trip-Integration mit Firefox und Google-Reader
Montag, 3. August

Blogroll

* Jörg bei Twitter
* Jens bei Twitter
* Nils bei Twitter

* Beetlebum
* a life less ordinary?
* Martin Fowler's Bliki
* Springify
* BILDblog
* Plazeboalarm
* LawBlog
* ADOM Blog
* Being busy
* Dr. Gero Presser

Links

* Heise
* The Scala Programming Language
Nils' Fotos bei fotocommunity.de
Jogi auf Qype
Get Firefox!
Use OpenOffice.org

Heise News

* Nokia erhebt neue Patentklagen gegen HTC

* Sicherheitsexperte: Uraltes WLAN-Einfallstor noch immer offen

* Aufklärungs- und Kampf-Drohnen: Debatte über Technik und Einsatz

* Google Maps weist Radfahrern in Deutschland den Weg

* Citrix Synergy: Xen Desktop 7 soll Windows aus der Cloud liefern

kostenloser Counter