來自:http://www.blogjava.net/lishunli/archive/2010/03/11/315055.html
使用 Annotation 並對 DAO 層封裝具有分頁功能的 S2SH 整合實例
李順利
2010 年 1 月 24 日
目錄
基於 Annotation的 Hibernate配置 ... 5
基於 Annotation的 Spring配置 ... 10
關鍵詞
使用 Annotation 並對 DAO 層封裝具有分頁功能的 S2SH 整合實例,李順利, Annotation , DAO 層封裝,分頁, SSH 整合,實例,黎活明,傳智播客,巴巴 運動網
前言
現在 Annotation 越來越流行,最近一段時間也學了一些, EJB3.0 、 Hibernate 、 Spring 等都很好地支持 Annotation ,而且熟悉了 Annotation 的話整個項目開發時間會縮短,代碼封裝比較好,但是封裝帶來的就是代碼閱讀就比較困難了。 Annotation 也是一門知識,可以選 擇使用 Annotation 還是其他。個人認爲由 Sun 官方支持的 EJB 規範會越來越流行,此時如果使用基於 Annotation 的 SSH 框架很容易轉移到 Struts+EJB+Spring 的項目中,而且使用 Annotation ,很容易實現 0 配置,像在這個實例中就一個配置,這樣避免了配置文件多而不知所措的情況。
開發環境
Jdk1.5+Struts2.1.8+Hibernate3.3+Spring3.0+MySql5.0+MyEclipse8.0
開發步驟
環境的集成
到官網下 載上面開發環境中的框架和工具,安裝完成後。在 Myeclipse 中新建名爲 SSHWithAnnotationDemo 的 web project ,添加 SSH 整合所需要的包,這裏詳細說一下需要哪些包?
Struts2.1.8 所需 Jar 包
xwork-core-2.1.6.jar 、 aopalliance-1.0.jar 、 commons-logging-1.0.4.jar 、 commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar 、 freemarker-2.3.15.jar 、 ognl-2.7.3.jar 、 struts2-convention-plugin-2.1.8.1.jar 、 struts2-core-2.1.8.1.jar 、 struts2-spring-plugin-2.1.8.jar
其中下文會對 struts2-convention-plugin 插件進行詳細講解。
Hibernate3.3 所需 Jar 包
slf4j-log4j12.jar 、 antlr-2.7.6.jar 、 commons-collections-3.1.jar 、 dom4j-1.6.1.jar 、 ejb3-persistence.jar 、 hibernate3.jar 、 hibernate-annotations.jar 、 hibernate-commons-annotations.jar 、 javassist-3.9.0.GA.jar 、 jta-1.1.jarlog4j.jar 、 slf4j-api-1.5.8.jar
Spring3.0 所需 Jar 包
org.springframework.web-3.0.0.RC1.jar 、 org.springframework.aop-3.0.0.RC1.jar 、 org.springframework.asm-3.0.0.RC1.jarorg.springframework.beans-3.0.0.RC1.jar 、
org.springframework.context-3.0.0.RC1.jar 、 org.springframework.core-3.0.0.RC1.jar 、 org.springframework.expression-3.0.0.RC1.jar 、 org.springframework.jdbc-3.0.0.RC1.jar 、 org.springframework.orm-3.0.0.RC1.jar 、 org.springframework.test-3.0.0.RC1.jar 、 org.springframework.transaction-3.0.0.RC1.jar
還有一些其他的輔助 Jar 包:
mysql-connector-java-5.1.7-bin.jar 、 aspectjweaver.jar 、 commons-dbcp-1.2.2.jar 、 commons-pool.jar 、 junit-4.6.jar
上面的 Jar 包都可以在相應的框架 Jar 文件夾裏面找到,具體的 Jar 包作用如果不清楚的話請 Google 一下。爲了方便,順利提供所有 這些 Jar 包下 載。
順利提供下載:
文 件 名:Struts2.1.8+Hibernate3.3+Spring3.0整合所需Jar包.rar
下載地址:http://usc.googlecode.com/files
/Struts2.1.8%2BHibernate3.3%2BSpring3.0%E6%95%B4%E5%90%88%E6%89%80%E9%9C%80Jar%E5%8C%85.rar
加入 Jar 包後,就可以進行 SSH 框架的整合了。
基於Annotation 的Struts 配置
使用 Annotation 對 Struts 配置可以實現 0 配置,這個時候就不需要對 Struts.xml 進行任何的配置, 0 配置的實現主要是使用 struts2-convention-plugin 插件。大致 介紹下 convention-plugin 插件。
1. 默 認所有的結果頁面都存儲在 WEB-INF/content 下
2. 命 名規則:去掉類名的 Action 部分。然後將將每個分部的首字母轉爲小寫,用 ’-’ 分割。
a) 舉 例: TestAction ,那麼訪問的時候就是 test.action;
b) 舉 例: StudentListAction ,那麼訪問的時候就是 student-list.action 。
3. 常 用的 Annotation
a) @Controller : 申明 Struts 爲控制層;
b) @Scope ( "prototype" ) : 申明 Struts 的範圍爲原型;
c) @Results :全局的結果集,可以配置 execute 執行後跳轉的頁面。
像 @Results ({ @Result (name = "success" , location = "teacher/teacherList.jsp" ), @Result (name = "input" , location = "/index.jsp" ) })
通過 convention-plugin 插件就可以很簡單的配置 Struts 的配置, 如果想 更好地瞭解 convention-plugin 插件請查看下面的網站或者自己 Google 一下。
http://cwiki.apache.org/WW/convention-plugin.html
http://javeye.iteye.com/blog/358744
http://yzl45.iteye.com/blog/346632
代碼舉例:
import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport; /** * 測試 convention - plugin 插件 */
@Controller @Scope ( "prototype" ) @Results ( { @Result (name = "success" , location = "test/test.jsp" ), @Result (name = "input" , location = "/index.jsp" ) }) // 成功的頁面 會跳到 WebRoot/WEB-INF/content/test/test.jsp, 失敗會跳 到 WebRoot/index.jsp public class TestAction extends ActionSupport { /* * @see com.opensymphony.xwork2.ActionSupport#execute() */ @Override public String execute() throws Exception { System. out .println( " 測試 convention-plugin 插件 " ); return INPUT ; } }
|
基於Annotation 的Hibernate 配置
Hibernate 的 Annotation 方式很類似於 EJB 中的 JPA ,主要的 Annotation 註解是: @Entity 、 @Table 、 @Id 、 @GeneratedValue 、 @Column 。具體的註解請上網查看一下。這裏介紹一種簡便的方法生成基於 Annotation 的 Hibernate 持久類。
1. 使用 Myeclipse 添加對 Hibernate 的支持
右鍵項目,選擇 MyEclipse 後選擇 Add hibernate Capabilities (一般都會先添加對 Spring 的支持後在添加對 Hibernate 的支持,這樣會使用 Spring 管理 Hibernate )
這個時候就不需要選擇 Jar 包了, Hibernate 的 Jar 已經加入進來, Next ,選擇 Spring 的配置文件,這樣可以使用 Spring 進行管理 Hibernate (這也是爲什麼要先添加對 Spirng 的支持) ,Next
選擇已經存在的 Spring 配置文件
後選擇已經創建好的 DateSource 。
即可,詳情請閱讀 Struts+Spring+Hibernate整合註冊登錄 —— 使用 Myeclipse 快速開發 ,裏面有詳細的介紹。
2. 進入生成 DB Brower 視圖(可以通過 windows->show View 找到)
找到後連接 MySql 數據庫後,即可生成 Hibernate 的持久類
生成的實例代碼
@Entity @Table (name = "student" , catalog = "test" ) public class Student implements java.io.Serializable {
// Fields
private Integer no ; private String name ; private String sex ; private Integer age ; private Double score ; private Date eduTime ;
// Constructors
/** default constructor */ public Student() { }
/** minimal constructor */ public Student(String name, String sex) { this . name = name; this . sex = sex; }
/** full constructor */ public |