使用Annotation並對DAO層封裝具有分頁功能的S2SH整合實例

來自:http://www.blogjava.net/lishunli/archive/2010/03/11/315055.html

使用 Annotation 並對 DAO 層封裝具有分頁功能的 S2SH 整合實例

 

李順利

2010 1 24

目錄

關鍵詞 ... 2

前言 ... 2

開發環境 ... 2

開發步驟 ... 2

環境的集成 ... 2

Struts2.1.8所需 Jar ... 2

Hibernate3.3所需 Jar ... 3

Spring3.0所需 Jar ... 3

基於 Annotation Struts配置 ... 4

基於 Annotation Hibernate配置 ... 5

基於 Annotation Spring配置 ... 10

DAO層封裝 ... 12

分頁 ... 19

業務邏輯 ... 21

測試 ... 22

實例結果 ... 23

參考網站 ... 23

源碼下載 ... 23

學習探討 ... 23

 

 


關鍵詞

使用 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

       clip_image002

其中下文會對 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

       clip_image004

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

 

clip_image005

還有一些其他的輔助 Jar 包:

mysql-connector-java-5.1.7-bin.jar aspectjweaver.jar commons-dbcp-1.2.2.jar commons-pool.jar junit-4.6.jar

clip_image006

上面的 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 框架的整合了。


基於AnnotationStruts 配置

使用 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 ;

    }

}

 

基於AnnotationHibernate 配置

Hibernate Annotation 方式很類似於 EJB 中的 JPA ,主要的 Annotation 註解是: @Entity @Table @Id @GeneratedValue @Column 。具體的註解請上網查看一下。這裏介紹一種簡便的方法生成基於 Annotation Hibernate 持久類。

1.  使用 Myeclipse 添加對 Hibernate 的支持

右鍵項目,選擇 MyEclipse 後選擇 Add hibernate Capabilities (一般都會先添加對 Spring 的支持後在添加對 Hibernate 的支持,這樣會使用 Spring 管理 Hibernate

clip_image008

clip_image010

這個時候就不需要選擇 Jar 包了, Hibernate Jar 已經加入進來, Next ,選擇 Spring 的配置文件,這樣可以使用 Spring 進行管理 Hibernate (這也是爲什麼要先添加對 Spirng 的支持) ,Next

clip_image012

選擇已經存在的 Spring 配置文件

clip_image014

後選擇已經創建好的 DateSource

clip_image016

clip_image018

即可,詳情請閱讀 Struts+Spring+Hibernate整合註冊登錄 —— 使用 Myeclipse 快速開發 ,裏面有詳細的介紹。

2.  進入生成 DB Brower 視圖(可以通過 windows->show View 找到)

clip_image020

找到後連接 MySql 數據庫後,即可生成 Hibernate 的持久類 clip_image022

clip_image024

生成的實例代碼

@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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章