grails 开发增删改查 on IntelliJ IDEA

来源:互联网 发布:英语网络兼职 编辑:程序博客网 时间:2024/06/16 11:15

1.安装grails

下载grails,然后配置:
GRAILS_HOME=”D:/JavaDev/grails”
path追加:%GRAILS_HOME%/bin

2.创建grails项目

这里写图片描述

这里写图片描述

这里写图片描述

3.配置数据源

打开conf/DataSource.groovy

dataSource {    dbCreate = "update"    url = "jdbc:mysql://192.168.58.11:3306/grailsdb?useUnicode=true&characterEncoding=UTF-8"    driverClassName = "com.mysql.jdbc.Driver"    username = "root"    password = "root"}hibernate {    cache.use_second_level_cache = true    cache.use_query_cache = false    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3//    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4}// environment specific settingsenvironments {    development {        dataSource {            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''            url = "jdbc:mysql://192.168.58.11:3306/grailsdb?useUnicode=true&characterEncoding=UTF-8"        }    }    test {        dataSource {            dbCreate = "update"            url = "jdbc:mysql://192.168.58.11:3306/grailsdb?useUnicode=true&characterEncoding=UTF-8"        }    }    production {        dataSource {            dbCreate = "update"            url = "jdbc:mysql://192.168.58.11:3306/grailsdb?useUnicode=true&characterEncoding=UTF-8"            properties {               maxActive = -1               minEvictableIdleTimeMillis=1800000               timeBetweenEvictionRunsMillis=1800000               numTestsPerEvictionRun=3               testOnBorrow=true               testWhileIdle=true               testOnReturn=false               validationQuery="SELECT 1"               jdbcInterceptors="ConnectionState"            }        }    }}

4.引入数据库驱动包

lib下添加mysql-connector-java-5.1.32.jar
用命令refresh-dependencies添加到依赖

5.新建Domain类。

两种方式:(1)通过命令:create-domain-class Book(2)右键-〉New-〉Domain Class
Book代码如下:

package hellograilsclass Book {    String title    String content    static constraints = {    }}

这里写图片描述

6.创建controller和views:两种方式

(1)打Book类,点击图标上面的Controller 和Views 就可以产生代码了
(2)命令grails generate-all hellograils.Book
grails会自动创建Controller(会自动生成增、删、改、查的相关方法)和View(会自动生成增、删、改、查的相关页面)
这里写图片描述

7.运行

点击像eclipse那样的小图标运行,打开地址
http://localhost:8080/hellograils/book/index

0 0