`

struts+spring+hibernate的web应用【2】

    博客分类:
  • ssh
阅读更多

第九部分:com.game.products.dao.hibernate包中新建继承HibernateDaoSupport

        ProductsMapDao类,并实现了ProductsDao接口。 代码如下:

package  com.game.products.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.game.products.dao.iface.ProductsDao;
 import  com.game.products.model.Products;


 /** */ /** 
 *  @author  cwf
 *
  */ 
  public   class  ProductsMapDao  extends  HibernateDaoSupport  implements  ProductsDao   {

     public  ProductsMapDao()  {} 
 
      /** */ /** 
     * 函数说明:添加信息
     * 参数说明:对象 
     * 返回值:
      */ 
      public   void  addProduct(Products pd)   {
         this .getHibernateTemplate().save(pd);
    } 
 
      /** */ /** 
     * 函数说明:删除信息
     * 参数说明: 对象
     * 返回值:
      */ 
      public   void  deleteProduct(Products pd)   {
         this .getHibernateTemplate().delete(pd);
    } 
 
      /** */ /** 
     * 函数说明:获得所有的信息
     * 参数说明: 
     * 返回值:信息的集合
      */ 
      public  List getProducts()   {
        String sql = " FROM Products ORDER BY gameNameCn " ;
         return   this .getHibernateTemplate().find(sql);
    } 
    
     /** */ /** 
     * 函数说明:获得总行数
     * 参数说明: 
     * 返回值:总行数
      */ 
      public   int  getRows()   {
        String sql = " FROM Products ORDER BY gameNameCn " ;
        List list = this .getHibernateTemplate().find(sql);
         return  list.size();
    } 
    
     /** */ /** 
     * 函数说明:获得一段记录信息
     * 参数说明: 
     * 返回值:信息的集合
      */ 
      public  List getProducts( 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   {
                Query query = session.createQuery( " FROM Products ORDER BY gameNameCn " );
                query.setFirstResult(startRow1);
                query.setMaxResults(pageSize1);
                 return  query.list();
            } 
        } );
    } 
 
      /** */ /** 
     * 函数说明:获得一条的信息
     * 参数说明: ID
     * 返回值:对象
      */ 
      public  Products getProduct(String gameId)   {
         return  (Products) this .getHibernateTemplate().get(Products. class ,gameId);
    } 
 
      /** */ /** 
     * 函数说明:获得最大ID
     * 参数说明: 
     * 返回值:最大ID
      */ 
      public  String getMaxID()   {
        String sql = " SELECT MAX(gameId)+1 FROM Products   " ;
        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;
    } 
 
      /** */ /** 
     * 函数说明:修改信息
     * 参数说明: 对象
     * 返回值:
      */ 
      public   void  updateProductd(Products pd)   {
         this .getHibernateTemplate().update(pd);
    } 
 
      /** */ /** 
     * 函数说明:查询的所有信息
     * 参数说明: 集合
     * 返回值:
      */ 
      public  List queryProducts(String fieldname,String value)   {
        System.out.println( " value:  " + value);
        String sql = " FROM Products where  " + fieldname + "  like '% " + value + " %' " + " ORDER BY gameNameCn " ;
         return   this .getHibernateTemplate().find(sql);
    } 
    
     /** */ /** 
     * 函数说明:获得总行数
     * 参数说明: 
     * 返回值:总行数
      */ 
      public   int  getRows(String fieldname,String value)   {
        String sql = " FROM Products where  " + fieldname + "  like '% " + value + " %' " + " ORDER BY gameNameCn " ;
        List list = this .getHibernateTemplate().find(sql);
         return  list.size();
    } 
    
     /** */ /** 
     * 函数说明:查询的一段信息
     * 参数说明: 集合
     * 返回值:
      */ 
      public  List queryProducts(String fieldname,String value, int  pageSize,  int  startRow)   {
         final   int  pageSize1 = pageSize;
         final   int  startRow1 = startRow;
         final  String sql = " FROM Products where  " + fieldname + "  like '% " + value + " %' " + " ORDER BY gameNameCn " ;
         return   this .getHibernateTemplate().executeFind( new  HibernateCallback()  {

             public  List doInHibernate(Session session)  throws  HibernateException, SQLException   {
                Query query = session.createQuery(sql);
                query.setFirstResult(startRow1);
                query.setMaxResults(pageSize1);
                 return  query.list();
            } 
        } 
    } 
 
} 

 

第十部分:com.game.bean.hibernate包中新建hibernate.cfg.xml,代码如下:

 <? xml version="1.0" encoding="GB2312" ?> 
 <! 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 ="dialect" > org.hibernate.dialect.SQLServerDialect </ property > 
         < property  name ="show_sql" > true </ property > 
 
         < mapping  resource ="com/game/products/model/products.hbm.xml" ></ mapping > 
     </ session-factory > 
 </ hibernate-configuration > 

 

至此,DAO层的代码已经编写完成。下一篇,将编写service层代码。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics