Saturday, September 08, 2007

Fast Rail with Grails in just 2 minutes

  1. Install Groovy


  2. Install Grails


  3. Start a new Application

    • C:\grails-apps>grails create-app appname (appname is optional, if not given, it will ask anyway)
    • Check the directory appname and all sub-directories it creates under. The application is eclipse ready and ANT ready with .project and .classpath and build.xml files (it would have been more nicer if maven had been considered instead of ANT :) Hope to see MAVEN support added in coming releases).
    • Import the project into eclipse.


  4. Setup new application for MySql
    • Explode the zip and copy the jar to /lib directory under the newly created application

    • Create databases (dev, test, prod) in MySql database. E.g. gspin_dev, gspin_test and gspin_prod

    • Edit DataSource.groovy under /conf for MySql

    E.g.


    dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = "root"
    }
    // environment specific settings
    environments {
    development {
    dataSource {
    dbCreate = "update" // one of 'create', 'create-drop','update'
    url = "jdbc:mysql://localhost/gspin_dev"
    }
    }
    test {
    dataSource {
    dbCreate = "update"
    url = "jdbc:mysql://localhost/gspin_test"
    }
    }
    production {
    dataSource {
    dbCreate = "update"
    url = "jdbc:mysql://localhost/gspin_prod"
    }
    }
    }

    • Create a domain class (.groovy)
      E.g. User as follows

    class User {
    String userId
    String password

    static constraints = {
    userId()
    password()
    }

    String toString() { this.userId }
    }

  5. Run the application

  6. E.g.

    C:\my-grails-app\gspin>grails run-app

    • Check the gspin_dev and notice the table User created.

  7. Check the gspin_dev and notice the table User created

  8. Generate source
    • C:\my-grails-app\gspin>grails generate-all

  9. Run the application and notice that you have got nice gui for all CRUD operations of your User domain object
    • C:\my-grails-app\gspin>grails run-app

  10. Test the application

No comments:

Post a Comment