Grails read from existing DB

寵の児 提交于 2019-12-11 12:17:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!