Groovy / Grails





Groovy





Grails


1. Edit grails-app/conf/DataSource.groovy
dataSource {
    pooled = true
//    driverClassName = "org.hsqldb.jdbcDriver"
      driverClassName = "org.postgresql.Driver"
//    username = "sa"
      username = "chaoyi"
//    password = ""
      password = "123456"
}
hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true

    cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
            // url = "jdbc:hsqldb:mem:devDB"
               url = "jdbc:postgresql://localhost:5432/grailsdb"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            // url = "jdbc:hsqldb:mem:testDb"
            url = "jdbc:postgresql://localhost:5432/grailsdb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            // url = "jdbc:hsqldb:file:prodDb;shutdown=true"
            url = "jdbc:postgresql://localhost:5432/grailsdb"
        }
    }
}



Grails - Auto Convention


$ grails create-domain-class studentRoom



Controller Name <-> DBMS - Table name

StudentRoom <-> student_room
Product <-> product


States of Controller <-> Field name

Auto add:
  1. id
  2. "version" bigint
id bigint NOT NULL,
"version" bigint NOT NULL,



String teacherName <-> teacher_name
Date lastDate = new Date() <-> last_date
String teacherNameNumber <-> teacher_name_number


$ grails generate-all studentRoom


CREATE TABLE product
(
  id bigint NOT NULL,
  "version" bigint NOT NULL,
  last_date timestamp without time zone NOT NULL,
  sutdent_name character varying(255) NOT NULL,
  CONSTRAINT product_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);


Views

id -> Id
String teacherName <-> Teacher Name
Date lastDate = new Date() <-> Last Date
String teacherNameNumber <-> Teacher Name Number
Comments