问题
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.
回答1:
Use the Reverse Engineer plugin to create domain classes from your existing database: http://grails.org/plugin/db-reverse-engineer
回答2:
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.
回答3:
@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").
回答4:
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.
回答5:
for creating domain classes you can use [GARG]:http://grag.sourceforge.net/index.html
来源:https://stackoverflow.com/questions/17240097/grails-read-from-existing-db