06 August 2013

GRAILS - Dynamic Scaffolding and MYSQL Set Up

Part of my development learning, I put together some notes on Grails. In this post, it covers mostly the use of dynamic scaffolding and the connection to a mysql server, as well as the use of environments. 

Scaffolding is the MVC format seem in Rails and Grails. It allows a developer to set up CRUD (Create/Update/Delete) actions super fast.   Dynamic Scaffolding is interesting because it doesn't need actual templates. You write very little code and Grails actual creates the pages and linkage for you... Grails will even create the dynamic db tables and do all the updates/deletes for you!


    Goals
        1. create a new grails app
        2. learn to use dynamic scaffolding
        3. use environments
        4. set up a mysql db
        5. connect to the mysql db
        6. run app in prod mode.

1. Create a new grails app

    Easy: grails create-app [name]
   

2. Dynamic Scaffolding

    A. First Create the Domain Class
        grails create-domain-class com.blog.Post
    B. Next Create the Scaffold controller
        grails create-scaffold-controller com.blog.Post

3. Environments

    By default we run the app in "dev" which uses H2 memory db.
    We can run other db's per environment, or have env. settings.
    For example, we can set up MYSQL for "prod" and leave H2 for dev
    To run with a spec env you do a grails prod run-app

4. Set up a MYSQL db

    First make sure MYSQL server is running
    In the MYSQL Workbench, create a new connection if you like
    Then create a new Schema - this is your db name.

5. Connect to the MYSQL db

    Back in Grails, open the file:
        /grails-app/conf/BuildConfig.groovy
        and uncomment:
        runtime 'mysql:mysql-connector-java:5.1.22'
    Next open the file:
        /grails-app/conf/DataSource.groovy
        update the url to be your msql server...
        ie "jdbc:mysql://localhost/blog"
        add
            username = [your mysql user]
        add
            password = [your mysql pass]

6. Run the App In Prod mode

    grails prod run-app

You can monitor the db you created, and watch as you
    create
    edit
    delete

via the app, the db will reflect the changes.

No comments:

Post a Comment