小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用 Spring Roo 從零開始,一步步的創(chuàng)建一個簡單的 Spring 程序

 richsky 2012-04-23

使用 Spring Roo 從零開始,一步步的創(chuàng)建一個簡單的 Spring 程序

 
使用 Spring Roo 從零開始,一步步的創(chuàng)建一個簡單的 Spring 程序。

SpringSource 剛剛發(fā)布最新版本 1.0.0m1 ,參見 Spring Roo 1.0.0.M1 Released。
相對之前的 alpha 2 ,這一版本中帶來不少的新特性,下面發(fā)行說明中的羅列的新特性。

    * 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 網(wǎng)站博客上提供入門文章 便是不可多得的學習資料,我這里也是根據(jù)文章進行操作的。
從 SpringSource 網(wǎng)站上下載 Spring ROO 1.0.0 m1 ,解壓到硬盤上。
將 <spring_roo_dir>/bin  加入到系統(tǒng)的PATH變量中。在 Linux 系統(tǒng)上,你可以直接創(chuàng)建一個鏈接到 ~/bin 或 /usr/bin/ 中。

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

注意,之前的 alpha 中要求設(shè)置 ROO_HOME 環(huán)境變量,現(xiàn)在不需要了,請將其刪除。

另外, Spring Roo 需要 Maven 支持,你必須安裝一個 2.0.9 以上的版本,參考用Maven 構(gòu)建項目。

下面就可以開始創(chuàng)建一個新的Spring 項目。

1. 創(chuàng)建一個項目。
[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. for Roo help.

roo> cre           

這時按 Tab 鍵,你會發(fā)現(xiàn) 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 參數(shù),它是項目的基礎(chǔ)包名。再次提醒,這里你仍然可以 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 項目,目錄結(jié)構(gòu)如下。

.
|-- 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 已經(jīng)幫你添加最基本的Spring 配置。
你可以把項目導入到eclipse進行編輯。
在項目根目錄中運行 mvn eclipse:eclipse ,會生成 Eclipse 相關(guān)的特殊文件,如.classpath , .project等。
現(xiàn)在你可以像導入一個普通項目那樣將這個項目導入到你的workspace中。


2. 設(shè)置數(shù)據(jù)庫。

在持久層技術(shù)上選擇,Roo 提供了多jpa實現(xiàn)的選擇,和多種數(shù)據(jù)庫選擇。

這里如果輸入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 可以看到所提供的參數(shù)選項。

install jpa -database    install jpa -provider

輸入 -p 按 tab 自動補全 -provider 。

roo> install jpa -provider

按 tab 可以看到幾種可用的 jpa 實現(xiàn)。

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 數(shù)據(jù)庫。

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

現(xiàn)在如果比較之前生成的代碼,你會發(fā)現(xiàn),Spring Roo 已經(jīng)幫你在Spring配置文件 applicationContext.xml 中配置好了相應(yīng)的數(shù)據(jù)庫的 Data Source,并且生成了相的相應(yīng)的數(shù)據(jù)庫配置模板(src/main/resources/database.properties),在 pom.xml 文件中已經(jīng)添加 jpa ,數(shù)據(jù)庫驅(qū)動相關(guān)的依賴。

現(xiàn)在你可以查看默認生成的數(shù)據(jù)庫配置。
roo> database properties
database.driverClassName = com.mysql.jdbc.Driver
database.password =
database.url = jdbc:mysql://localhost:3306
database.username =

你可以手動修改數(shù)據(jù)庫配置,也可以在 Spring Roo 命令行中完成。

設(shè)置數(shù)據(jù)庫用戶名。

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

設(shè)置數(shù)據(jù)庫密碼。

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

設(shè)置數(shù)據(jù)庫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 數(shù)據(jù)庫創(chuàng)建創(chuàng)建數(shù)據(jù)庫posts,在Terminal 使用mysqladmin 命令,你也可以使用自己熟悉的 MySQL 客戶端工具來完成。

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

輸入密碼,一個posts數(shù)據(jù)庫就創(chuàng)建好了。

現(xiàn)在我們回到 roo shell中,查看一下數(shù)據(jù)庫配置。

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

所有的數(shù)據(jù)庫配置選項已經(jīng)修改了。

3. 創(chuàng)建 Domain Class
現(xiàn)在如果輸入 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.

創(chuàng)建一個 Post 類。
roo> new persistent class jpa -name ~.domain.Post -

按下 Tab ,你有三種選擇,你可以創(chuàng)建一個抽象類,或者從其它類繼承。

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 同時創(chuàng)建一個測試類。

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

這里 ~ 代表創(chuàng)建項目指定的 topLevelPackage 值。所有的 .aj 文件都是 AspectJ 文件 ,對于不熟悉AspectJ 的人來講,可能對生成的代碼有些令人費解。
不過,我們完全沒有必要關(guān)心 .aj 的內(nèi)容,可以以此為基礎(chǔ)添加自己的代碼。
下面往 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

創(chuàng)建另外一個類 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

添加關(guān)聯(lián)關(guān)系。
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 參數(shù),在默認情況下,不添加 -class 參數(shù)會修改剛剛創(chuàng)建類,而這里通過 -class 參數(shù)可以指定將內(nèi)容添加到哪個類中。


4. 創(chuàng)建 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/ 。


接下的一些博文會關(guān)注如何添加一些其它功能,如安全,郵件等。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    來自: richsky > 《Spring》

    舉報/認領(lǐng)

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多
    喜歡該文的人也喜歡 更多
    熱門閱讀 換一換