Question

I want to fetch data from already existing database from another project into my Grails project and list the data. Should I be creating a domain controller for the already existing db? I know how to create domain-controller and use data migration plugin to update db but none of the books I read had any information on how to setup and read from an existing database. I'm using MySQL for my database.

Was it helpful?

Solution

Use the Reverse Engineer plugin to create domain classes from your existing database: https://plugins.grails.org/plugin/grails/db-reverse-engineer

OTHER TIPS

As per my understandings, please do the following :

Suppose you have an existing DB "TestDB" with a table "domain_model" and it has columns "column_a", "column_b" and "column_c".

In Grail, create one domain Class and add the following in mapping block :

static mapping = {
    table "domain_model"
            version false
            id column: "primary_key_column"
            columnA column: "column_a"
            columnB column: "column_b"
            columnC column: "column_c"
}
     String id
     String columnA
     Date columnB
     Integer columnC

and in DataSource.groovy, keep dbCreate property to be set as "update".

dbCreate="update"

Hope this helps !

Also, make sure to add "id" and "version" column to your existing table where "id" column is autoincremented, and you can have "version" column as "1" to all records.

def defaultDataFileStream = this.class.getResourceAsStream("defaultData.xml")
def allData = new XmlSlurper().parse(defaultDataFileStream)

Try this in bootstrap.groovy. You'll put the xml file in grails-app/conf.

@codeBarer You can use xml as a datasource in Grails by putting the xml file in grails-app/conf. You can reference this file in BootStrap.groovy with:

class.getResource("yourData.xml")

for creating domain classes you can use [GARG]:http://grag.sourceforge.net/index.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top