`

spring roo

阅读更多

Spring Roo 是一种 Spring 开发的辅助工具,当你感叹 Ruby on Rails,CakePHP,Seam Gen时,或许应该尝试一下Spring Roo。
Spring 官方博客的 Spring Roo 介绍

下载 Spring Roo ,当前最新版本是 1.0 Alpha 2。

解压至硬盘,设置环境变量 ROO_HOME ,指向 roo 所在的目录。并将 $ROO_HOME/bin 添加到系统的 Path 中。

在Linux 你可以写入当前用户的 .bashrc 文件。

export ROO_HOME=/home/hantsy/libs/java/roo-1.0.0.A2
export PATH=$PATH:$ROO_HOME/bin

执行 source ~/.bashrc 让它立即生效。

执行 roo.sh ,你可以进入 Spring Roo 命令行操作界面。

[hantsy@localhost samples]$ ../bin/roo.sh
    ____  ____  ____  
   / __ \/ __ \/ __ \
  / /_/ / / / / / / /
 / _, _/ /_/ / /_/ /  
/_/ |_|\____/\____/   


Welcome to Spring ROO. For assistance press TAB or type "hint" then hit ENTER.


roo>

你可以输 入 help,查看帮助信息。

roo> help
* */ - End of block comment
* /* - Start of block comment
* // - Inline comment markers
* ; - Inline comment markers
* create project - Creates a new project
* database properties - Shows database configuration details
* database remove - Removes a particular database property
* database set - Changes a particular database property
* date - Displays the local date and time
* development mode - Switches the system into development mode (greater diagnostic information)
* exit - Exits the shell
* help - Shows system help
* hint - Provides step-by-step hints and context-sensitive guidance
* metadata for id - Shows detailed information about the metadata item
* metadata for type - Shows detailed metadata for the indicated type
* metadata summary - Shows statistics on the metadata system
* metadata trace - Traces metadata event delivery notifications (0=none, 1=some, 2=all)
* new controller manual - Create a new manual Controller (ie where you write the methods)
* new dod - Creates a new data on demand for the specified entity
* new integration test - Creates a new data on demand for the specified entity
* property file details - Shows the details of a particular properties file
* property file remove - Removes a particular properties file property
* property file set - Changes a particular properties file property
* props - Shows the shell's properties
* quit - Exits the shell
* script - Parses the specified resource file and executes its commands
* version - Displays shell version

** Type 'hint' (without the quotes) and hit ENTER for step-by-step guidance **

Spring Roo 提供大量的代码生成的功能,包括就最基本的 CRUD 操作,还可以设置数据库,日志log4j 级别,Spring security 等。

Spring roo 中提供两个实例,他们是两个 .roo 文件,包括一些创建项目文件的脚本。
进入 sample 目录。
roo>script vote.roo
create project -topLevelPackage com.springsource.vote
Created /home/hantsy/libs/java/roo-1.0.0.A2/samples/pom.xml
Created SRC_MAIN_JAVA
Created SRC_MAIN_RESOURCES
Created SRC_TEST_JAVA
Created SRC_TEST_RESOURCES
Created SRC_MAIN_WEBAPP
Created SRC_MAIN_RESOURCES/applicationContext.xml
Created SRC_MAIN_WEBAPP/WEB-INF
Created SRC_MAIN_WEBAPP/WEB-INF/vote-servlet.xml
Created SRC_MAIN_WEBAPP/WEB-INF/web.xml
Created SRC_MAIN_WEBAPP/index.jsp

install jpa -provider HIBERNATE -database HYPERSONIC_PERSISTENT
Created SRC_MAIN_RESOURCES/META-INF
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Created SRC_MAIN_RESOURCES/database.properties
Managed SRC_MAIN_RESOURCES/applicationContext.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml

new persistent class jpa -name ~.domain.Choice -testAutomatically
Created SRC_MAIN_JAVA/com/springsource/vote/domain
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Choice.java
Created SRC_TEST_JAVA/com/springsource/vote/domain
Created SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceDataOnDemand.java
Created SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceIntegrationTest.java
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_Plural.aj
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_Entity.aj
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_ToString.aj
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_Configurable.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceIntegrationTest_Roo_IntegrationTest.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceDataOnDemand_Roo_Configurable.aj
add field string namingChoice -notNull -sizeMin 1 -sizeMax 30
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Choice.java
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_Java Bean.aj
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_ToString.aj
Managed SRC_TEST_JAVA/com/springsource/vote/domain/ChoiceDataOnDemand_Roo_DataOnDemand.aj
add field string description -sizeMax 80
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Choice.java
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_Java Bean.aj
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Choice_Roo_ToString.aj
new controller automatic ~.web.ChoiceController
Created SRC_MAIN_JAVA/com/springsource/vote/web
Created SRC_MAIN_JAVA/com/springsource/vote/web/ChoiceController.java
Created SRC_MAIN_JAVA/com/springsource/vote/web/ChoiceController_Roo_Controller.aj
Created SRC_MAIN_WEBAPP/images
Created SRC_MAIN_WEBAPP/images/banner-graphic.png
Created SRC_MAIN_WEBAPP/images/springsource-logo.png
Created SRC_MAIN_WEBAPP/images/list.png
Created SRC_MAIN_WEBAPP/images/show.png
Created SRC_MAIN_WEBAPP/images/create.png
Created SRC_MAIN_WEBAPP/images/update.png
Created SRC_MAIN_WEBAPP/images/delete.png
Created SRC_MAIN_WEBAPP/styles
Created SRC_MAIN_WEBAPP/styles/roo.css
Created SRC_MAIN_WEBAPP/WEB-INF/jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/menu.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/header.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/footer.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/includes.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/dataAccessFailure.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/uncaughtException.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/choice
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/choice/list.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/choice/show.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/choice/create.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/choice/update.jsp
Managed SRC_MAIN_WEBAPP/WEB-INF/jsp/menu.jsp

new persistent class jpa -name Vote -testAutomatically
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Vote.java
Created SRC_TEST_JAVA/com/springsource/vote/domain/VoteDataOnDemand.java
Created SRC_TEST_JAVA/com/springsource/vote/domain/VoteIntegrationTest.java
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_Plural.aj
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_Entity.aj
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_ToString.aj
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_Configurable.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/VoteDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/VoteDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/VoteIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/com/springsource/vote/domain/VoteIntegrationTest_Roo_IntegrationTest.aj
add field reference jpa choice -type Choice
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote.java
Created SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_JavaBean.aj
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_ToString.aj
add field string ip -notNull -sizeMin 7 -sizeMax 15
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote.java
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_JavaBean.aj
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_ToString.aj
Managed SRC_TEST_JAVA/com/springsource/vote/domain/VoteDataOnDemand_Roo_DataOnDemand.aj
add field date jpa registered -type java.util.Date -notNull -past
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote.java
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_JavaBean.aj
Managed SRC_MAIN_JAVA/com/springsource/vote/domain/Vote_Roo_ToString.aj
Managed SRC_TEST_JAVA/com/springsource/vote/domain/VoteDataOnDemand_Roo_DataOnDemand.aj
new controller automatic ~.web.VoteController
Created SRC_MAIN_JAVA/com/springsource/vote/web/VoteController.java
Created SRC_MAIN_JAVA/com/springsource/vote/domain/ChoiceEditor.java
Created SRC_MAIN_JAVA/com/springsource/vote/web/VoteController_Roo_Controller.aj
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/vote
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/vote/list.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/vote/show.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/vote/create.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/vote/update.jsp
Managed SRC_MAIN_WEBAPP/WEB-INF/jsp/menu.jsp
Created SRC_MAIN_JAVA/com/springsource/vote/domain/ChoiceEditor_Roo_Editor.aj

new controller manual ~.web.PublicVoteController
Created SRC_MAIN_JAVA/com/springsource/vote/web/PublicVoteController.java

configure logging -level DEBUG -package WEB
Created SRC_MAIN_RESOURCES/log4j.properties
Managed SRC_MAIN_WEBAPP/WEB-INF/web.xml

install security
Managed ROOT/pom.xml
Created SRC_MAIN_RESOURCES/applicationContext-security.xml
Created SRC_MAIN_WEBAPP/login.jsp
Managed SRC_MAIN_WEBAPP/WEB-INF/web.xml

list finders for -class com.springsource.vote.domain.Vote -depth 2 -filter reg,betw,IpEq
findVotesByIpEqualsAndRegisteredBetween(String ip, Date minRegistered, Date maxRegistered)
findVotesByIpEqualsOrRegisteredBetween(String ip, Date minRegistered, Date maxRegistered)
findVotesByRegisteredBetweenAndIpEquals(Date minRegistered, Date maxRegistered, String ip)
findVotesByRegisteredBetweenOrIpEquals(Date minRegistered, Date maxRegistered, String ip)
roo>

输入 quit 退出交互界面。
你会发现 Spring Roo 生成的是一个 Maven 项目,你可以将它导入 Eclipse 中,你需要安装 m2clipse AJDT 插件。

在项目的中目录中执行 mvn eclipse:eclipse ,maven 会帮你生成一个 eclipse 项目。
现 在你可以将它导入到 eclipse 中。

它是一个 web 项目,可以把它部署到服务器 上运行。

======================================================================

这里使用 Spring Roo 从零开始,一步步的创建一个简单的 Spring 程序。

SpringSource 刚刚发布最新版本 1.0.0m1 ,参见 Spring Roo 1.0.0.M1 Released
相对之前的 alpha 2 ,这一版本中带来不少的新特性,下面发行说明中的罗列的新特性。

  • Much easier installation
  • 31% better performance
  • Tomcat support (for embedded usage)
  • JMS support (including ActiveMQ setup)
  • Selenium support
  • Email support
  • Web Flow installation
  • Additional samples
  • Various other fixes and improvements


在 Spring Roo 提供完整的官方文档之前 ,SpringSource 网站博客上提供入门文章 便是不可多得的学习资料,我这里也是根据文章进行操作的。
从 SpringSource 网站上下载 Spring ROO 1.0.0 m1 ,解压到硬盘上。
将 <spring_roo_dir>/bin  加入到系统的PATH变量中。在 Linux 系统上,你可以直接创建一个链接到 ~/bin 或 /usr/bin/ 中。

ln -sf <spring_roo_dir>/bin/roo.sh /home/<user>/bin/roo

注意,之前的 alpha 中要求设置 ROO_HOME 环境变量,现在不需要了,请将其删除。

另外, Spring Roo 需要 Maven 支持,你必须安装一个 2.0.9 以上的版本,参考用Maven 构建项目

下 面就可以开始创建一个新的Spring 项目。

1. 创建一个项目。
[hantsy@localhost ~]$ mkdir roodemo
[hantsy@localhost ~]$ cd roodemo

[hantsy@localhost roodemo]$ roo
    ____  ____  ____  
   / __ \/ __ \/ __ \
  / /_/ / / / / / / /
 / _, _/ /_/ / /_/ /  
/_/ |_|\____/\____/    1.0.0.M1 [rev 64]


Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.

任何时候你可以输入hint 来获得一些操作建议。

roo> hint
Welcome to Roo! We hope you enjoy your stay!

Before you can use many features of Roo, you need to start a new project.

To do this, type 'create project' (without the quotes) and then hit TAB.

Enter a -topLevelPackage like 'com.mycompany.projectname' (no quotes).
When you've finished completing your -topLevelPackage, press ENTER.
Your new project will then be created in the current working directory.

Note that Roo frequently allows the use of TAB, so press TAB regularly.
Once your project is created, type 'hint' and ENTER for the next suggestion.
You're also welcome to visit http://forum.springframework.org for Roo help.

roo> cre            

这时按 Tab 键,你会发现 Spring Roo 会帮你自动补全命令 create project。

Linux 用户对 Tab 的使用再也熟悉不过了,在 Spring Roo 的命令行,你随时都使用 tab 来辅助命令的输入。

roo> create project

这里按下 enter ,会得到一条提示信息。

You must specify a default option (otherwise known as option 'topLevelPackage') for this command

指定一个 topLevelPackage 参数,它是项目的基础包名。再次提醒,这里你仍然可以 create project - 按 tab 来自动补全。

roo> create project -topLevelPackage roodemo
Created /home/hantsy/Projects/roodemo/pom.xml
Created SRC_MAIN_JAVA
Created SRC_MAIN_RESOURCES
Created SRC_TEST_JAVA
Created SRC_TEST_RESOURCES
Created SRC_MAIN_WEBAPP
Created SRC_MAIN_RESOURCES/applicationContext.xml
Created SRC_MAIN_WEBAPP/WEB-INF
Created SRC_MAIN_WEBAPP/WEB-INF/roodemo-servlet.xml
Created SRC_MAIN_WEBAPP/WEB-INF/web.xml
Created SRC_MAIN_WEBAPP/WEB-INF/jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/index.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/urlrewrite.xml

这里会生成一个标准 maven web 项目,目录结构如下。

.
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |   `-- applicationContext.xml
    |   `-- webapp
    |       `-- WEB-INF
    |           |-- jsp
    |           |   `-- index.jsp
    |           |-- roodemo-servlet.xml
    |           |-- urlrewrite.xml
    |           `-- web.xml
    `-- test
        |-- java
        `-- resources

Spring Roo 已经帮你添加最基本的Spring 配置。
你可以把项目导入到eclipse进行编辑。
在项目根目录中运行 mvn eclipse:eclipse ,会生成 Eclipse 相关的特殊文件,如.classpath , .project等。
现在你可以像导入一个普通项目那样将这个项目导入到你的workspace中。


2. 设置数据库。

在持久层技术 上选择,Roo 提供了多jpa实现的选择,和多种数据库选择。

这里如果输入hint,会得到以下信息。
roo> hint
Roo requires the installation of a JPA provider and associated database.

Type 'install jpa' and then hit TAB three times.
We suggest you type 'H' then TAB to complete "HIBERNATE".
After the -provider, press TAB twice for database choices.
For testing purposes, type (or TAB) HYPERSONIC_IN_MEMORY.
If you press TAB again, you'll see there are no more options.
As such, you're ready to press ENTER to execute the command.

Once JPA is installed, type 'hint' and ENTER for the next suggestion.

任何时候,不要忘记使用 hint 来获得提示。

roo> install jpa -     

按 tab 可以看到所提供的参数选项。

install jpa -database    install jpa -provider

输入 -p 按 tab 自动补全 -provider 。

roo> install jpa -provider

按 tab 可以看到几种可用的 jpa 实现。

ECLIPSELINK    HIBERNATE      OPENJPA

接着输入首字母 H 按tab,补全 HIBERNATE,再输入-d 按 tab,补全-database。

roo> install jpa -provider HIBERNATE -database

H2_IN_MEMORY             HYPERSONIC_IN_MEMORY     HYPERSONIC_PERSISTENT
MSSQL                    MYSQL                    ORACLE
POSTGRESQL               SYBASE

最 后输入MY 按 tab 选择MySQL 数据库。

roo> install jpa -provider HIBERNATE -database MYSQL
Created SRC_MAIN_RESOURCES/META-INF
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Created SRC_MAIN_RESOURCES/database.properties
please enter your database details in src/main/resources/database.properties
Managed SRC_MAIN_RESOURCES/applicationContext.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml
Managed ROOT/pom.xml

现 在如果比较之前生成的代码,你会发现,Spring Roo 已经帮你在Spring配置文件 applicationContext.xml 中配置好了相应的数据库的 Data Source,并且生成了相的相应的数据库配置模板(src/main/resources/database.properties),在 pom.xml 文件中已经添加 jpa ,数据库驱动相关的依赖。
 
现在你可以查看默认生成的数据库配置。
roo> database properties
database.driverClassName = com.mysql.jdbc.Driver
database.password =
database.url = jdbc:mysql://localhost:3306
database.username =

你可以手动修改数据库配置,也可以在 Spring Roo 命令行中完成。

设置数据库用户名。

roo> database set -key database.username -value root
Managed SRC_MAIN_RESOURCES/database.properties

设置数据库密码。

roo> database set -key database.password -value root
Managed SRC_MAIN_RESOURCES/database.properties

设置数据库url连接地址。

roo> database set -key database.url -value jdbc:mysql://localhost:3306/posts?useUnicode=true&characterEncoding=UTF-8
Managed SRC_MAIN_RESOURCES/database.properties

在MySQL 数据库创建创建数据库posts,在Terminal 使用mysqladmin 命令,你也可以使用自己熟悉的 MySQL 客户端工具来完成。

mysqladmin create posts --default-character-set=utf8  -uroot -p

输入密码,一个 posts数据库就创建好了。

现在我们回到 roo shell中,查看一下数据库配置。

roo> database properties
database.driverClassName = com.mysql.jdbc.Driver
database.password = root
database.url = jdbc:mysql://localhost:3306/posts?useUnicode=true&characterEncoding=UTF-8
database.username = root

所有的数据库配置选项已经修改了。

3. 创建 Domain Class
现在如果输入 hint ,可以看到以下信息。

roo> hint                                         
You can create entities either via Roo or your IDE.
Using the Roo shell is fast and easy, especially thanks to the TAB completion.

Start by typing 'new p' and then hitting TAB twice.
Enter the -name in the form '~.domain.MyEntityClassName'
In Roo, '~' means the -topLevelPackage you specified via 'create project'.

Afer specify a -name argument, press SPACE then TAB. Note nothing appears.
Because nothing appears, it means you've entered all mandatory arguments.
However, optional arguments do exist for this command (and most others in Roo).
To see the optional arguments, type '-' and then hit TAB. Mostly you won't
need any optional arguments, but let's select the -testAutomatically option
and hit ENTER. You can always use this approach to view optional arguments.

After creating an entity, use 'hint' for the next suggestion.

创建一个 Post 类。
roo> new persistent class jpa -name ~.domain.Post -

按 下 Tab ,你有三种选择,你可以创建一个抽象类,或者从其它类继承。

new persistent class jpa -name ~.domain.Post -abstract
new persistent class jpa -name ~.domain.Post -extends
new persistent class jpa -name ~.domain.Post -testAutomatically

这里输入 t 按 Tab 键选择 -testAutomatically 同时创建一个测试类。

roo> new persistent class jpa -name ~.domain.Post -testAutomatically
Created SRC_MAIN_JAVA/roodemo/domain
Created SRC_MAIN_JAVA/roodemo/domain/Post.java
Created SRC_TEST_JAVA/roodemo/domain
Created SRC_TEST_JAVA/roodemo/domain/PostDataOnDemand.java
Created SRC_TEST_JAVA/roodemo/domain/PostIntegrationTest.java
Created SRC_MAIN_JAVA/roodemo/domain/Post_Roo_Plural.aj
Created SRC_MAIN_JAVA/roodemo/domain/Post_Roo_Entity.aj
Created SRC_MAIN_JAVA/roodemo/domain/Post_Roo_ToString.aj
Created SRC_MAIN_JAVA/roodemo/domain/Post_Roo_Configurable.aj
Created SRC_TEST_JAVA/roodemo/domain/PostIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA/roodemo/domain/PostDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA/roodemo/domain/PostIntegrationTest_Roo_IntegrationTest.aj
Created SRC_TEST_JAVA/roodemo/domain/PostDataOnDemand_Roo_Configurable.aj

这 里 ~ 代表创建项目指定的 topLevelPackage 值。所有的 .aj 文件都是 AspectJ 文件 ,对于不熟悉AspectJ 的人来讲,可能对生成的代码有些令人费解。
不过,我们完全没有必要关心 .aj 的内容,可以以此为基础添加自己的代码。
下面往 Post 中添加一些字段,这里全部略去了Roo shell 的输出。

roo> add field string -fieldName title -notNull -sizeMax 200 
roo> add field string -fieldName content -notNull -sizeMax 2000
roo> add field date jpa -fieldName createdDate -type java.util.Date
roo> add field date jpa -fieldName modifiedDate -type java.util.Date 

创建另外一个类 Comment。

roo> new persistent class jpa -name ~.domain.Comment -testAutomatically
roo> add field string -fieldName name -notNull -sizeMax 200
roo> add field string -fieldName email -notNull -sizeMax 50
roo> add field string -fieldName content
roo> add field string -fieldName url
roo> add field date jpa -fieldName createdDate -type java.util.Date

添加关联关系。
roo> add field set jpa -fieldName comments -element ~.domain.Comment -class ~.domain.Post -mappedBy post
roo> add field reference jpa -fieldName post -type ~.domain.Post -class ~.domain.Comment

如果细心的 话你可以注意,这里我们指定了一个 -class 参数,在默认情况下,不添加 -class 参数会修改刚刚创建类,而这里通过 -class 参数可以指定将内容添加到哪个类中。


4. 创建 Web Controller类

Spring 可以为我们生成最基本的CURD页面。

roo> new controller automatic -formBackingObject roodemo.domain.Post -name roode roodemo.web.PostController  
Created SRC_MAIN_JAVA/roodemo/web
Created SRC_MAIN_JAVA/roodemo/web/PostController.java
Created SRC_MAIN_JAVA/roodemo/domain/CommentEditor.java
Created SRC_MAIN_JAVA/roodemo/web/PostController_Roo_Controller.aj
Created SRC_MAIN_WEBAPP/images
Created SRC_MAIN_WEBAPP/images/banner-graphic.png
Created SRC_MAIN_WEBAPP/images/springsource-logo.png
Created SRC_MAIN_WEBAPP/images/list.png
Created SRC_MAIN_WEBAPP/images/show.png
Created SRC_MAIN_WEBAPP/images/create.png
Created SRC_MAIN_WEBAPP/images/update.png
Created SRC_MAIN_WEBAPP/images/delete.png
Created SRC_MAIN_WEBAPP/styles
Created SRC_MAIN_WEBAPP/styles/roo.css
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/header.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/footer.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/includes.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/dataAccessFailure.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/uncaughtException.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/post
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/post/list.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/post/show.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/post/create.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/post/update.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/menu.jsp
Managed SRC_MAIN_WEBAPP/WEB-INF/jsp/menu.jsp
Created SRC_MAIN_JAVA/roodemo/domain/CommentEditor_Roo_Editor.aj

roo> new controller automatic -formBackingObject ~.domain.Comment -name ~.web.CommentController
Created SRC_MAIN_JAVA/roodemo/web/CommentController.java
Created SRC_MAIN_JAVA/roodemo/domain/PostEditor.java
Created SRC_MAIN_JAVA/roodemo/web/CommentController_Roo_Controller.aj
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/comment
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/comment/list.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/comment/show.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/comment/create.jsp
Created SRC_MAIN_WEBAPP/WEB-INF/jsp/comment/update.jsp
Managed SRC_MAIN_WEBAPP/WEB-INF/jsp/menu.jsp
Created SRC_MAIN_JAVA/roodemo/domain/PostEditor_Roo_Editor.aj

5. 运行程序
退出roo shell,在项目根目录中运行 mvn tomcat:run ,以嵌入式方式运行 tomcat。
浏览器打开 http://localhost:8080/ 。

 

 

=======================================================================
内容摘自官方文档
类似代码生成器,应该说更高级一些,与Rails,Grails的命令台功能类似
1:下载Release版的压缩包,解压缩后在环境变量中设置对应的home与path
2:在cmd命令台中,运行roo执行操作
可以通过安装基于Eclipse的SpringSource Tool Suite's工具集,为roo开发提供便利

十分钟内创建Web应用
hint命令.提供了类似向导的工具,提供了step by step的方式,指导创建一个应用.也可以加入指定的类型参数,获取指定的帮助信息,如hint controllers

project命令,用于在当前目录创建一个roo project,
persistence命令,用于创建数据库相关文件,
    persistence setup --provider HIBERNATE --database MSSQL //这里为选择的数据类型
    然后根据提示修改对应的src/main/resources/database.properties,完成数据库的细节设置
entity命令,用于创建基础的POJO对象,
entity --class ~.Timer --testAutomatically //TImer为类名
field命令,用于为该Pojo对象添加属性
field string --fieldName message --notNull
controller命令,为pojo对象提供基于Spring MVC的功能,采用最新的RESTful风格
    controller all --package ~.web //表示将会在当前目录下创建web子目录,存放controller文件
selenium test命令,用于为类创建对应的测试代码
selenium test --controller ~.web.TimerController
perform命令,用于执行不同的操作,如测试.打包,以及转换成能够使用IDE导入的方式,
通过执行perform,也可以自动下载所需的依赖jar包

roo> perform tests
roo> perform package
roo> perform eclipse
如果出现jar缺失,比如对应的sql数据库jdbc包,可以通过手动在pom.xml下加入依赖的方式,提供扩展    
打包后,就可以直接将war包放入到tomcat中运行,其中注意访问的url与war包名字一致

Roo Samples Script 一些Roo目录下的脚本
应该使用用于将一系列的命令,保存在脚本中,然后通过命令进行批量执行这些命令
script --file filename.roo
clinic.roo:  -- 包含了一个完整的宠物商店的例子
vote.roo: 包含了Spring Security的例子
RSVP: 详细的使用了除去Spring本身功能的,Spring roo中的其他设置选项,(其实也没有那么多东西)

安装数据库时,选择会根据JPA标准显示三个,比较常用的应该就是hibernate,比较传统的hibernate使用,生成的代码更偏向 JPA标准模式
开发过程中,可以使用内置的内存数据库HYPERSONIC_IN_MEMORY,由maven提供插件支持

创建类时,可以通过~.根据工程的top package目录进行子目录的设置,而类名为最后的一个字符串(注意)
如entity --class ~.domain.Topping 设置类名为Topping

除了hint向导外,也提供了help命令,用于完整的command帮助
project 目录下的log.roo文件,将会记录所执行过的command
在生成Java代码时,roo应该是通过Java类与AspectJ类的方式,同时生成最终功能代码,使用AOP的方式编程

在创建pojo属性的同时,可以提供为其添加基于jsr303的 annotation的验证
在对class操作后,roo会自动跳转后续都为其进行操作,可以通过在操作时,设置不同的--class属性进行选择
如 class --class Base 就可以跳转到对应的Base类

建立实体类的一对多关系
field set --fieldName toppings --element ~.domain.Topping
为当前类添加一个属性toppings,表示拥有多个的Topping对象
主要为set的使用,以及后续的element属性

在roo project中,同样也可以使用maven进行测试,打包等操作

创建web层
controller命令的使用
controller all --package ~.web //创建所有entity的controller,并且放置在子web目录下,并且会自动生成所需的web.xml等配置文件,以及添加spring MVC和URL rewrite,Tiles

可以使用mavan的命令,直接对roo项目进行部署测试
'mvn tomcat:run, 或者 mvn jetty:run

为程序添加安全支持 spring Securing
security setup命令:进行安装,注意必须在创建Web layer后,才可以进行添加.
会添加对应的applicationContext-security.xml文件
在页面中,meun.jspx可以使用<sec:authorize ifAllGranted="ROLE_ADMIN"> 对菜单进行配置
在controller中,可以通过注解进行配置权限
@RooWebScaffold(path = "pizzaorder",
automaticallyMaintainView = true,
formBackingObject = PizzaOrder.class,
delete=false,
update=false)
用于配置roo移除该controller的删除和更新的方法
http://localhost:8080/pazzashop/login 用于提供用户登录


定义UI界面
一般情况下,如果对entity对象进行了修改,会自动印象对应的controller与jsp界面,可以通过 @RooWebScaffold(automaticallyMaintainView=false)配置,用于取消自动关联

Selenium Tests  Selenium测试
自动生成对应的脚本,使用如下命令执行测试
selenium test --controller ~.web.ToppingController

也可以通过执行mvn selenium:selenese,使用mvn进行测试,区别在于.会打开firefox查看测试结果

Backups and Deployment 备份与部署
backup命令,用于创建一个保存了当前项目代码,日志和脚本记录的备份zip文件
perform package命令,用于生成部署所需的war包

区别于原来的dao service controller层次架构,roo的项目中,只有entity(类似dao)以及web层,通过使用jpa的底层,解决了在云计算平台上扩展的问题

Critical Technologies
roo peoject 中的两个重要技术, AspectJ与Spring

roo项目中,会针对class生成对应的*_Roo_*.aj文件,一个或多个, 成为AspectJ ITD文件,由Roo进行自动管理
所生成的.java类中,代码一般都比较简单,不过都带有许多注解,其中roo开头的注解将不会被编译到class代码中

当你重写了java文件的toString方法后,会自动删除对应的entity_Roo_ToString.aj文件,如果要重新使用自动生成 的toString方法,可以通过annotation进行配置
@RooToString(toStringMethod="generatedToString"),但是注意这里生成的新的toString 方法名为
generatedToString

Entity Layer 类(Domain层)
可以通过jpa的annotation对其进行修改,比如对其添加非空验证,修改其映射的表名等

@RooJavaBean 用于提供pojo对象的get和set方法,如果手动编写了get和set方法,那将会自动跳过
@RooEntity 为entity提供对应的JPA操作类

Web Layer
使用spring mvc提供了web的基本脚手架,包含了REST风格的URL rewrite,Apache Tiles,Spring JavaScript.以及一键式的命令为其添加Spring Security

Usage and Conventions 用法与公约

Add-On 插件安装和删除
下载后放置在$ROO_HOME/add-ons目录下,使用addon cleanup command,还包括$ROO_HOME/work的使用..(文档有些纠结)

在spring roo的project中,将一个class进行了分解,如entity类,通过修改分离get/set
,toString,以及对应的Dao方法,分别放在不同的类中,这样便于代码的阅读,在编译后,生成对应的代码....这个需要研究一下
同理,在controller中,分解成两个类,一个包含了传统的controller方法,则另外一个可以用于扩展

Base Add-Ons
目前插件开发的文档并没有写出,可能还未开发,不过如果要学习,就必须先开始了目前Roo
project中所使用的框架

目前roo中集成了 Jms email,jpa,spring security,selenium.spring web flow

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics