posts - 310, comments - 6939, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Struts2+Spring2+Hibernate3 web應用示例(二)

          Posted on 2007-09-30 09:47 詩特林 閱讀(19013) 評論(4)  編輯  收藏 所屬分類: Struts
              Struts2+Spring2+Hibernate3 web應用示例(二)

        1. Struts2+Spring2+Hibernate3 web應用示例------源代碼
        2. Struts2+Spring2+Hibernate3 web應用示例(七)
        3. Struts2+Spring2+Hibernate3 web應用示例(六)
        4. Struts2+Spring2+Hibernate3 web應用示例(五)
        5. Struts2+Spring2+Hibernate3 web應用示例(四)
        6. Struts2+Spring2+Hibernate3 web應用示例(三)
        7. Struts2+Spring2+Hibernate3 web應用示例(二)
        8. Struts2+Spring2+Hibernate3 web應用示例(一)

        9.  

          三、       建立數(shù)據(jù)持久化層

          1、編寫實體類Booksbooks.hbm.xml映射文件。

          package com.sterning.books.model;

          import java.util.Date;

          public class Books {
              
          //    Fields 
              private String bookId;//編號
              private String bookName;//書名
              private String bookAuthor;//作者
              private String bookPublish;//出版社
              private Date bookDate;//出版日期
              private String bookIsbn;//ISBN
              private String bookPage;//頁數(shù)
              private String bookPrice;//價格
              private String bookContent;//內(nèi)容提要
              
              
          //    Constructors
              public Books(){}
              
              
          //    Property accessors

              
          public String getBookId() {
                  
          return bookId;
              }


              
          public void setBookId(String bookId) {
                  
          this.bookId = bookId;
              }


              
          public String getBookName() {
                  
          return bookName;
              }


              
          public void setBookName(String bookName) {
                  
          this.bookName = bookName;
              }


              
          public String getBookAuthor() {
                  
          return bookAuthor;
              }


              
          public void setBookAuthor(String bookAuthor) {
                  
          this.bookAuthor = bookAuthor;
              }


              
          public String getBookContent() {
                  
          return bookContent;
              }


              
          public void setBookContent(String bookContent) {
                  
          this.bookContent = bookContent;
              }


              
          public Date getBookDate() {
                  
          return bookDate;
              }


              
          public void setBookDate(Date bookDate) {
                  
          this.bookDate = bookDate;
              }


              
          public String getBookIsbn() {
                  
          return bookIsbn;
              }


              
          public void setBookIsbn(String bookIsbn) {
                  
          this.bookIsbn = bookIsbn;
              }


              
          public String getBookPage() {
                  
          return bookPage;
              }


              
          public void setBookPage(String bookPage) {
                  
          this.bookPage = bookPage;
              }


              
          public String getBookPrice() {
                  
          return bookPrice;
              }


              
          public void setBookPrice(String bookPrice) {
                  
          this.bookPrice = bookPrice;
              }


              
          public String getBookPublish() {
                  
          return bookPublish;
              }


              
          public void setBookPublish(String bookPublish) {
                  
          this.bookPublish = bookPublish;
              }

          }

            com.sterning.books.model.Books.java

                 接下來要把實體類Books的屬性映射到books表,編寫下面的books.hbm.xml文件:

          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >

          <hibernate-mapping>
               
          <class name="com.sterning.books.model.Books" table="books" >
                   
          <id name="bookId" type="string">
                      
          <column name="book_id" length="5" />
                      
          <generator class="assigned" />
                  
          </id>
                  
          <property name="bookName" type="string">
                      
          <column name="book_name" length="100" />
                  
          </property>
                   
          <property name="bookAuthor" type="string">
                      
          <column name="book_author" length="100" />
                  
          </property>
                  
          <property name="bookPublish" type="string">
                      
          <column name="book_publish" length="100" />
                  
          </property>
                   
          <property name="bookDate" type="java.sql.Timestamp">
                      
          <column name="book_date" length="7" />
                  
          </property>
                    
          <property name="bookIsbn" type="string">
                      
          <column name="book_isbn" length="20" />
                  
          </property>
                  
          <property name="bookPage" type="string">
                      
          <column name="book_page" length="11" />
                  
          </property>
                  
          <property name="bookPrice" type="string">
                      
          <column name="book_price" length="4" />
                  
          </property> 
                   
          <property name="bookContent" type="string">
                      
          <column name="book_content" length="100" />
                  
          </property>
               
          </class>
          </hibernate-mapping>
                  com.sterning.books.model.books.hbm.xml

          2hibernate.cfg.xml配置文件如下:(注意它的位置在scr/hibernate.cfg.xml

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
          >
          <hibernate-configuration>
          <session-factory>
              
          <property name="show_sql">true</property>

              
          <mapping resource="com/sterning/books/model/books.hbm.xml"></mapping>
          </session-factory>
          </hibernate-configuration>
            Com.sterning.bean.hibernate.hibernate.cfg.xml

          四、       建立DAO 

          DAO訪問層負責封裝底層的數(shù)據(jù)訪問細節(jié),不僅可以使概念清晰,而且可以提高開發(fā)效率。

          1、建立DAO的接口類:BooksDao

          package com.sterning.books.dao.iface;

          import java.util.List;

          import com.sterning.books.model.Books;

          public interface BooksDao {
              List getAll();
          //獲得所有記錄
              List getBooks(int pageSize, int startRow);//獲得所有記錄
              int getRows();//獲得總行數(shù)
              int getRows(String fieldname,String value);//獲得總行數(shù)
              List queryBooks(String fieldname,String value);//根據(jù)條件查詢
              List getBooks(String fieldname,String value,int pageSize, int startRow);//根據(jù)條件查詢
              Books getBook(String bookId);//根據(jù)ID獲得記錄
              String getMaxID();//獲得最大ID值
              void addBook(Books book);//添加記錄
              void updateBook(Books book);//修改記錄
              void deleteBook(Books book);//刪除記錄    
          }

            com.sterning.books.dao.iface.BooksDao.java

           

          2、實現(xiàn)此接口的類文件,BooksMapDao

          package com.sterning.books.dao.hibernate;

          import java.sql.SQLException;
          import java.util.Iterator;
          import java.util.List;

          import org.hibernate.HibernateException;
          import org.hibernate.Query;
          import org.hibernate.Session;
          import org.springframework.orm.hibernate3.HibernateCallback;
          import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

          import com.sterning.books.dao.iface.BooksDao;
          import com.sterning.books.model.Books;
          import com.sterning.commons.PublicUtil;


          /**
           * 
          @author cwf
           *
           
          */

          public class BooksMapDao extends HibernateDaoSupport implements BooksDao {

              
          public BooksMapDao(){}

              
          /**
               * 函數(shù)說明:添加信息
               * 參數(shù)說明:對象 
               * 返回值:
               
          */

              
          public void addBook(Books book) {
                  
          this.getHibernateTemplate().save(book);
              }


              
          /**
               * 函數(shù)說明:刪除信息
               * 參數(shù)說明: 對象
               * 返回值:
               
          */

              
          public void deleteBook(Books book) {
                  
          this.getHibernateTemplate().delete(book);
              }


              
          /**
               * 函數(shù)說明:獲得所有的信息
               * 參數(shù)說明: 
               * 返回值:信息的集合
               
          */

              
          public List getAll() {
                  String sql
          ="FROM Books ORDER BY bookName";
                  
          return this.getHibernateTemplate().find(sql);
              }

              
              
          /**
               * 函數(shù)說明:獲得總行數(shù)
               * 參數(shù)說明: 
               * 返回值:總行數(shù)
               
          */

              
          public int getRows() {
                  String sql
          ="FROM Books ORDER BY bookName";
                  List list
          =this.getHibernateTemplate().find(sql);
                  
          return list.size();
              }

              
              
          /**
               * 函數(shù)說明:獲得所有的信息
               * 參數(shù)說明: 
               * 返回值:信息的集合
               
          */

              
          public List getBooks(int pageSize, int startRow) throws HibernateException {
                  
          final int pageSize1=pageSize;
                  
          final int startRow1=startRow;
                  
          return this.getHibernateTemplate().executeFind(new HibernateCallback(){

                      
          public List doInHibernate(Session session) throws HibernateException, SQLException {
                          
          // TODO 自動生成方法存根
                          Query query=session.createQuery("FROM Books ORDER BY bookName");
                          query.setFirstResult(startRow1);
                          query.setMaxResults(pageSize1);
                          
          return query.list();
                      }

                  }
          );
              }


              
          /**
               * 函數(shù)說明:獲得一條的信息
               * 參數(shù)說明: ID
               * 返回值:對象
               
          */

              
          public Books getBook(String bookId) {
                  
          return (Books)this.getHibernateTemplate().get(Books.class,bookId);
              }


              
          /**
               * 函數(shù)說明:獲得最大ID
               * 參數(shù)說明: 
               * 返回值:最大ID
               
          */

              
          public String getMaxID() {
                  String date
          =PublicUtil.getStrNowDate();
                  String sql
          ="SELECT MAX(bookId)+1 FROM Books  ";
                  String noStr 
          = null;
                  List ll 
          = (List) this.getHibernateTemplate().find(sql);
                  Iterator itr 
          = ll.iterator();
                  
          if (itr.hasNext()) {
                      Object noint 
          = itr.next();
                      
          if(noint == null){
                          noStr 
          = "1";                
                      }
          else{
                          noStr 
          = noint.toString();
                      }

                  }

                  
                  
          if(noStr.length()==1){
                      noStr
          ="000"+noStr;
                  }
          else if(noStr.length()==2){
                      noStr
          ="00"+noStr;
                  }
          else if(noStr.length()==3){
                      noStr
          ="0"+noStr;
                  }
          else{
                      noStr
          =noStr;
                  }

                  
          return noStr;
              }


              
          /**
               * 函數(shù)說明:修改信息
               * 參數(shù)說明: 對象
               * 返回值:
               
          */

              
          public void updateBook(Books pd) {
                  
          this.getHibernateTemplate().update(pd);
              }


              
          /**
               * 函數(shù)說明:查詢信息
               * 參數(shù)說明: 集合
               * 返回值:
               
          */

              
          public List queryBooks(String fieldname,String value) {
                  System.out.println(
          "value: "+value);
                  String sql
          ="FROM Books where "+fieldname+" like '%"+value+"%'"+"ORDER BY bookName";
                  
          return this.getHibernateTemplate().find(sql);
              }

              
              
          /**
               * 函數(shù)說明:獲得總行數(shù)
               * 參數(shù)說明: 
               * 返回值:總行數(shù)
               
          */

              
          public int getRows(String fieldname,String value) {
                  String sql
          ="";
                  
          if(fieldname==null||fieldname.equals("")||fieldname==null||fieldname.equals(""))
                      sql
          ="FROM Books ORDER BY bookName";
                  
          else    
                      sql
          ="FROM Books where "+fieldname+" like '%"+value+"%'"+"ORDER BY bookName";
                  List list
          =this.getHibernateTemplate().find(sql);
                  
          return list.size();
              }

              
              
          /**
               * 函數(shù)說明:查詢信息
               * 參數(shù)說明: 集合
               * 返回值:
               
          */

              
          public List getBooks(String fieldname,String value,int pageSize, int startRow) {
                  
          final int pageSize1=pageSize;
                  
          final int startRow1=startRow;
                  
          final String queryName=fieldname;
                  
          final String queryValue=value;
                  String sql
          ="";
                  
                  
          if(queryName==null||queryName.equals("")||queryValue==null||queryValue.equals(""))
                      sql
          ="FROM Books ORDER BY bookName";
                  
          else    
                      sql
          ="FROM Books where "+fieldname+" like '%"+value+"%'"+"ORDER BY bookName";
                  
                  
          final String sql1=sql;
                  
          return this.getHibernateTemplate().executeFind(new HibernateCallback(){

                      
          public List doInHibernate(Session session) throws HibernateException, SQLException {
                          
          // TODO 自動生成方法存根
                          Query query=session.createQuery(sql1);
                          query.setFirstResult(startRow1);
                          query.setMaxResults(pageSize1);
                          
          return query.list();
                      }

                  }
          );
              }


          }

           com.sterning.books.dao.hibernate.BooksMapDao.java
          未完待續(xù).下篇寫業(yè)務邏輯層
          。。。。。。

          評論

          # re: Struts2+Spring2+Hibernate3 web應用示例(二)  回復  更多評論   

          2008-03-17 22:26 by 瞬間愛情
          謝謝你了額...要咋樣才可以聯(lián)系得上你額?

          # re: Struts2+Spring2+Hibernate3 web應用示例(二)  回復  更多評論   

          2009-01-15 17:29 by monarch
          獲取總行數(shù)的效率太低了
          樓主能不能弄高效的方法
          如:select count(*) from (searchSql)
          但貌似在jpa規(guī)范中沒有容易實現(xiàn)的方法

          # re: Struts2+Spring2+Hibernate3 web應用示例(二)  回復  更多評論   

          2009-06-18 16:37 by way
          謝謝樓主啦。。讓我了解了了SSH的架構(gòu)

          # re: Struts2+Spring2+Hibernate3 web應用示例(二)  回復  更多評論   

          2012-08-23 15:28 by 打算
          胡錦濤今天去文濤閣了
          主站蜘蛛池模板: 科技| 当雄县| 康定县| 彝良县| 万源市| 屏东县| 郁南县| 芦山县| 故城县| 革吉县| 通海县| 嘉峪关市| 杭锦旗| 日照市| 饶阳县| 常宁市| 昌都县| 阿荣旗| 阳曲县| 定州市| 乌什县| 扬中市| 嘉定区| 旅游| 富蕴县| 浑源县| 美姑县| 定日县| 永康市| 鹿泉市| 临武县| 滨海县| 页游| 安达市| 勐海县| 纳雍县| 二连浩特市| 砚山县| 阜宁县| 周宁县| 易门县|