1. Create a datasource script similar to those found in Grails, place it at griffon-app/conf/DataSource.groovy
2. Create a database schema script, this will be used to generate tables and columns, place it at griffon-app/conf/DatabaseSchema.groovy. Mind the magic builder variable, it will be explained shortly
3. Create a script that contains data used to populate the database, place it at griffon-app/conf/Bootstrap.groovy. Mind the magic sql variable, it will also be explained in the following step
4. Create a DbService class under griffon-app/services/DbService.groovy with the following contents
A few things to notice here:
- DbService is implemented as a singleton, this task is dead simple thanks to @Singleton
- DbService.initDataSource() performs 3 tasks:
- initializes a database connection.
- generates the database schema.
- populates the database with bootstrap data.
5. We must instruct DbService to load the database, the earliest opportunity is after the application has bootstrapped itself, we can react to an event (BootstrapEnd) by placing the following code in griffon-app/conf/Events.groovy
Here is how step #1 joins the rest, parsing configuration files with Groovy is very simple. This script is executed outside of the EDT always.
6. Let's create the view, a basic form with 2 textFields (name & lastname) that will be used to add a new person to the list of available persons, plus a table view displaying all persons
Now it would be a good time to drop in additional dependencies required by this little application, place the following on your app's lib directory
- GSQL http://svn.codehaus.org/gmod/gsql
- glazedlists http://publicobject.com/glazedlists
- miglayout http://miglayout.com
8. Finally the last piece of the puzzle, the controller. Upon initialization this controller will query the database for all persons, iterate over the result set creating a new POGO (a Map actually) and insert it into the list of persons held by the model, GlazedLists takes care of the rest (keeping tables in sync with GlazedLists is that easy, really!). You will notice that while a new person is added to the in memory list it is not added to the database, the main reason being that the database is also an in memory one, changes are lost across sessions (this however can be tweaked easily too). Here are a few screenshots of the application in action
![]() |
![]() |
![]() |
![]() |
This is far from finished, for example there is no datasource pooling nor transaction support; the datasource definition is quite simple, it doesn't honor environments as Grails' datasources do; db calls are explicit, there are no domain classes. However this simplistic approach may work for small applications or even applets.
Full source code for this sample application is available at github, Griffon 0.1.1 is required to run this application.
Keep on Groovying!