grails 自动生成crud代码和前端页面

创建项目

使用 grails 命令 创建项目:

grails create-app my-project

项目目录

%PROJECT_HOME%
    + grails-app
       + conf                 ---> location of configuration artifacts 
           + hibernate        ---> optional hibernate config
           + spring           ---> optional spring config
       + controllers          ---> location of controller artifacts
       + domain               ---> location of domain classes
       + i18n                 ---> location of message bundles for i18n
       + services             ---> location of services
       + taglib               ---> location of tag libraries
       + util                 ---> location of special utility classes 
       + views                ---> location of views
           + layouts          ---> location of layouts
   + lib
   + scripts                  ---> scripts
   + src
       + groovy               ---> optional; location for Groovy source files
                                   (of types other than those in grails-app/*)
       + java                 ---> optional; location for Java source files
   + test                     ---> generated test classes
   + web-app
       + WEB-INF

创建领域类

cd my-project
grails create-domain-class org.example.Book

编辑类 添加一些属性

package org.example
class Book {
    String title
    String author
 
    static constraints = {
        title(blank: false)
        author(blank: false)
    }
}

constraints函数中可以添加一些约束

创建控制器

grails create-controller org.example.Book 
 

定义了scaffold grails 会给自动创建 CUDR 的逻辑

package org.example
class BookController {
    def scaffold = Book // Note the capital "B"
}

连接数据库

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = "com.mysql.cj.jdbc.Driver"
    username = "root"
    password = "123456"
}

grails 修改布局

所以需要一行命令

 
grails generate-all org.example.Book 
 

这句命令会重新生成 controller 和 view 这样 view 目录下代码和之前的功能一致 我们就可以进行布局样式修改了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值