This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab("org.grails:grails-datastore-gorm-hibernate4:3.0.0.RELEASE") | |
@Grab("org.grails:grails-spring:2.3.6") | |
@Grab("com.h2database:h2:1.3.164") | |
import grails.orm.bootstrap.* | |
import grails.persistence.* | |
import org.springframework.jdbc.datasource.DriverManagerDataSource | |
import org.h2.Driver | |
init = new HibernateDatastoreSpringInitializer(Person) | |
def dataSource = new DriverManagerDataSource(Driver.name, "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE", 'sa', '') | |
init.configureForDataSource(dataSource) | |
println "Total people = " + Person.count() | |
@Entity | |
class Person { | |
String name | |
static constraints = { | |
name blank:false | |
} | |
} |
El código completo esta en el siguiente repo (https://github.com/ivanarrizabalaga/BookGorm), y las partes relevantes son:
BuildConfig.groovy:
repositories {griffonHome()mavenLocal()mavenCentral()mavenRepo "http://snapshots.repository.codehaus.org"mavenRepo "http://repository.codehaus.org"mavenRepo "http://download.java.net/maven/2/"mavenRepo "http://repository.jboss.com/maven2/"mavenRepo "http://repo.spring.io/milestone"mavenRepo "http://repo.spring.io/release"}dependencies {runtime "org.grails:grails-datastore-gorm-hibernate4:3.0.0.RELEASE"runtime "org.grails:grails-spring:2.3.6"runtime "com.h2database:h2:1.3.164"compile "org.springframework:spring-expression:3.2.4.RELEASE"}
Initialize.groovy:
//TODO Should be a Spring beannew HibernateDatastoreSpringInitializer("bookgorm").configureForDataSource(new DriverManagerDataSource(Driver.name,"jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE", 'sa', ''))
src/main/bookgorm/Book.groovy:
package bookgormimport grails.persistence.Entity
@Entityclass Book{String titleString authorstatic constraints = {title blank:falseauthor blank:false}}
BookController.groovy:
void loadData(){List books=Book.list()edt{model.books.addAll(books)}}def addBook= {evt=null->Book b=new Book(title:view.titleField.text,author:view.authorField.text)b.save()edt{model.books.add(b)view.titleField.text=""view.authorField.text=""}}def clearAll= {evt=null->Book.executeUpdate("delete Book")edt{model.books.clear()}}
Mola, no?
¡A cuidarse!
No hay comentarios:
Publicar un comentario