先创建好相应的domain模型
package demo
class User {
String username
String password
String realname = '匿名'
String rolename = '普通用户'
Date dateCreated
Date lastUpdated
// 属性约束
static constraints = {
// id(nullable: false,blank:false,unique: true,)
username(nullable: false,blank: false,unique: true)
realname(nullable: false,blank: true,unique: false)
rolename(nullable: false,blank: false,unique: false,inList: ['普通用户','管理员'])
}
// 映射关系
static mapping = {
table "user"
id column: 'id'
version column: 'version'
username column: 'username'
password column: 'password'
realname column: 'realname'
rolename column: 'rolename'
sort id:'desc'
}
}
执行命令generate-all 包名.类名,比如我这里执行generate-all demo.User即可
可以在控制台看到类似的
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:CICompilerCount=3 -Djline.WindowsTerminal.directConsole=false -Dfile.encoding=UTF-8 -classpath C:\Users\inateck\AppData\Local\Temp\classpath278784961.jar org.grails.cli.GrailsCli generate-all demo.User --plain-output
|Rendered template Controller.groovy to destination grails-app\controllers\demo\UserController.groovy
|Rendered template Service.groovy to destination grails-app\services\demo\UserService.groovy
|Rendered template Spec.groovy to destination src\test\groovy\demo\UserControllerSpec.groovy
|Rendered template ServiceSpec.groovy to destination src\integration-test\groovy\demo\UserServiceSpec.groovy
|Scaffolding completed for grails-app\domain\demo\User.groovy
|Rendered template create.gsp to destination grails-app\views\user\create.gsp
|Rendered template edit.gsp to destination grails-app\views\user\edit.gsp
|Rendered template index.gsp to destination grails-app\views\user\index.gsp
|Rendered template show.gsp to destination grails-app\views\user\show.gsp
|Views generated for grails-app\domain\demo\User.groovy
到这里,控制器和视图就生成好了
本文详细介绍了在Grails框架中创建User域模型的过程,并演示了如何通过执行特定命令自动生成控制器、服务层、测试用例及视图代码,简化开发流程。

257

被折叠的 条评论
为什么被折叠?



