前一篇文章
				(struts+spring+hibernate的web应用<一>  架构搭建)
				让我们打好了架子,接下来就来编写代码了。在编码之前,我们需要先自行了解
				strust,spring,hibernate
				基础知识,后面的文章将不会过多的介绍这些框架的基础知识。整个项目由
				Dao,Services,Web
				三层组成,
				Dao
				层主要通过
				hibernate
				来操作数据库,
				Service
				层主要体现了业务,事务的处理,
				Web
				层由
				struts
				来控制。整个项目的控制交由
				spring
				管理。
		
		
				
						
						 
				
		
		
				现在的这个小项目除了完成基本的添删改查,还有一个简单的分页功能。这个分页功能不仅前台分页,而且在后台数据库也进行了分页处理。
		
		
				
						 
				
		
		
				现在就来编写
				Dao
				层的代码。
		
		
				首先写好
				pojo
				的代码:
		
		
				在
				com.game.products.model
				中新建
				products.hbm.xml
				类,代码如下:
		
		
				
						
						
				
		
		
		
		
				 <?
				xml version="1.0" encoding="GB2312"
				?>
				<?
				xml version="1.0" encoding="GB2312"
				?>
				
						
						 <!
				DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
				
				<!
				DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
				>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
				>
				
						
						 
						
						 <
				hibernate-mapping
				>
				
				<
				hibernate-mapping
				>
				
						
						 <
				class 
				name
				="com.game.products.model.Products"
				 table
				="products"
				 
				>
     
				<
				class 
				name
				="com.game.products.model.Products"
				 table
				="products"
				 
				>
				
						
						 <
				id 
				name
				="gameId"
				 type
				="string"
				>
         
				<
				id 
				name
				="gameId"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_id"
				 length
				="5"
				 
				/>
            
				<
				column 
				name
				="game_id"
				 length
				="5"
				 
				/>
				
						
						 <
				generator 
				class
				="assigned"
				 
				/>
            
				<
				generator 
				class
				="assigned"
				 
				/>
				
						
						 </
				id
				>
        
				</
				id
				>
				
						
						 <
				property 
				name
				="gameNameCn"
				 type
				="string"
				>
        
				<
				property 
				name
				="gameNameCn"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_name_cn"
				 length
				="100"
				 
				/>
            
				<
				column 
				name
				="game_name_cn"
				 length
				="100"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 <
				property 
				name
				="gameNameEn"
				 type
				="string"
				>
         
				<
				property 
				name
				="gameNameEn"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_name_en"
				 length
				="100"
				 
				/>
            
				<
				column 
				name
				="game_name_en"
				 length
				="100"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 <
				property 
				name
				="gameCapacity"
				 type
				="string"
				>
        
				<
				property 
				name
				="gameCapacity"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_capacity"
				 length
				="4"
				 
				/>
            
				<
				column 
				name
				="game_capacity"
				 length
				="4"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 <
				property 
				name
				="gameVersion"
				 type
				="string"
				>
         
				<
				property 
				name
				="gameVersion"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_version"
				 length
				="4"
				 
				/>
            
				<
				column 
				name
				="game_version"
				 length
				="4"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 <
				property 
				name
				="gameMedia"
				 type
				="string"
				>
          
				<
				property 
				name
				="gameMedia"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_media"
				 length
				="4"
				 
				/>
            
				<
				column 
				name
				="game_media"
				 length
				="4"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 <
				property 
				name
				="gameCopyright"
				 type
				="string"
				>
        
				<
				property 
				name
				="gameCopyright"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_copyright"
				 length
				="4"
				 
				/>
            
				<
				column 
				name
				="game_copyright"
				 length
				="4"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 <
				property 
				name
				="gamePrice"
				 type
				="string"
				>
        
				<
				property 
				name
				="gamePrice"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_price"
				 length
				="4"
				 
				/>
            
				<
				column 
				name
				="game_price"
				 length
				="4"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				 
 <
				property 
				name
				="gameContent"
				 type
				="string"
				>
         
				<
				property 
				name
				="gameContent"
				 type
				="string"
				>
				
						
						 <
				column 
				name
				="game_content"
				 length
				="100"
				 
				/>
            
				<
				column 
				name
				="game_content"
				 length
				="100"
				 
				/>
				
						
						 </
				property
				>
        
				</
				property
				>
				
						
						 </
				class
				>
     
				</
				class
				>
				
						
						 </
				hibernate-mapping
				>
				
				</
				hibernate-mapping
				>
		 
		
				
 
		
				注意这里的
				ID
				不是数据库自动生成的,而是根据需要由程序生成,一般项目中的主键
				ID
				都是采取这种方式。
		
		
				然后在这个包中再新建
				Products
				类,代码如下:
		
		
				
						
						
				
		
		
				 package
				 com.game.products.model;
				package
				 com.game.products.model;


 public
				 
				class
				 Products
				public
				 
				class
				 Products 
				
						 {
				
				
						{
 //
						    Fields
    
						//
						    Fields 
						
								
								 private
						 String gameId;
						//
						编号
						
						    
						private
						 String gameId;
						//
						编号
						
								
								 private
						 String gameNameCn;
						//
						中文名称
						
						    
						private
						 String gameNameCn;
						//
						中文名称
						
								
								 private
						 String gameNameEn;
						//
						英文名称
						
						    
						private
						 String gameNameEn;
						//
						英文名称
						
								
								 private
						 String gameCapacity;
						//
						碟数
						
						    
						private
						 String gameCapacity;
						//
						碟数
						
								
								 private
						 String gameVersion;
						//
						版本
						
						    
						private
						 String gameVersion;
						//
						版本
						
								
								 private
						 String gameMedia;
						//
						介质
						
						    
						private
						 String gameMedia;
						//
						介质
						
								
								 private
						 String gameCopyright;
						//
						版权
						
						    
						private
						 String gameCopyright;
						//
						版权
						
								
								 private
						 String gamePrice;
						//
						价格
						
						    
						private
						 String gamePrice;
						//
						价格
						
								
								 private
						 String gameContent;
						//
						攻略
						
						    
						private
						 String gameContent;
						//
						攻略
 
    
 //
						    Constructors
    
						//
						    Constructors
						
								
								 
								 public
						 Products()
						
						    
						public
						 Products()
						
								 {}
						
						
								{}
						
						
								
								 
    
 //
						    Property accessors
    
						//
						    Property accessors
						
								
								 
								 public
						 String getGameCapacity()
						
						    
						public
						 String getGameCapacity() 
						
								 {
						
						
								{
 return
								 gameCapacity;
        
								return
								 gameCapacity;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameCapacity(String gameCapacity)
    
						public
						 
						void
						 setGameCapacity(String gameCapacity) 
						
								 {
						
						
								{
 this
								.gameCapacity 
								=
								 gameCapacity;
        
								this
								.gameCapacity 
								=
								 gameCapacity;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameId()
    
						public
						 String getGameId() 
						
								 {
						
						
								{
 return
								 gameId;
        
								return
								 gameId;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameId(String gameId)
    
						public
						 
						void
						 setGameId(String gameId) 
						
								 {
						
						
								{
 this
								.gameId 
								=
								 gameId;
        
								this
								.gameId 
								=
								 gameId;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameNameCn()
    
						public
						 String getGameNameCn() 
						
								 {
						
						
								{
 return
								 gameNameCn;
        
								return
								 gameNameCn;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameNameCn(String gameNameCn)
    
						public
						 
						void
						 setGameNameCn(String gameNameCn) 
						
								 {
						
						
								{
 this
								.gameNameCn 
								=
								 gameNameCn;
        
								this
								.gameNameCn 
								=
								 gameNameCn;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameNameEn()
    
						public
						 String getGameNameEn() 
						
								 {
						
						
								{
 return
								 gameNameEn;
        
								return
								 gameNameEn;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameNameEn(String gameNameEn)
    
						public
						 
						void
						 setGameNameEn(String gameNameEn) 
						
								 {
						
						
								{
 this
								.gameNameEn 
								=
								 gameNameEn;
        
								this
								.gameNameEn 
								=
								 gameNameEn;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameVersion()
    
						public
						 String getGameVersion() 
						
								 {
						
						
								{
 return
								 gameVersion;
        
								return
								 gameVersion;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameVersion(String gameVersion)
    
						public
						 
						void
						 setGameVersion(String gameVersion) 
						
								 {
						
						
								{
 this
								.gameVersion 
								=
								 gameVersion;
        
								this
								.gameVersion 
								=
								 gameVersion;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameMedia()
    
						public
						 String getGameMedia() 
						
								 {
						
						
								{
 return
								 gameMedia;
        
								return
								 gameMedia;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameMedia(String gameMedia)
    
						public
						 
						void
						 setGameMedia(String gameMedia) 
						
								 {
						
						
								{
 this
								.gameMedia 
								=
								 gameMedia;
        
								this
								.gameMedia 
								=
								 gameMedia;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameCopyright()
    
						public
						 String getGameCopyright() 
						
								 {
						
						
								{
 return
								 gameCopyright;
        
								return
								 gameCopyright;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameCopyright(String gameCopyright)
    
						public
						 
						void
						 setGameCopyright(String gameCopyright) 
						
								 {
						
						
								{
 this
								.gameCopyright 
								=
								 gameCopyright;
        
								this
								.gameCopyright 
								=
								 gameCopyright;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGameContent()
    
						public
						 String getGameContent() 
						
								 {
						
						
								{
 return
								 gameContent;
        
								return
								 gameContent;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGameContent(String gameContent)
    
						public
						 
						void
						 setGameContent(String gameContent) 
						
								 {
						
						
								{
 this
								.gameContent 
								=
								 gameContent;
        
								this
								.gameContent 
								=
								 gameContent;
 }
    }
						
						
								
								 
								
								 
								 public
						 String getGamePrice()
    
						public
						 String getGamePrice() 
						
								 {
						
						
								{
 return
								 gamePrice;
        
								return
								 gamePrice;
 }
    }
						
						
								
								 
								
								 
								 public
						 
						void
						 setGamePrice(String gamePrice)
    
						public
						 
						void
						 setGamePrice(String gamePrice) 
						
								 {
						
						
								{
 this
								.gamePrice 
								=
								 gamePrice;
        
								this
								.gamePrice 
								=
								 gamePrice;
 }
    }
						
						
								
								 
								
								 }
}
				
				
						
						 
				
		 
		
				
 
		
				需要注意的是,我这里都是采用了
				string
				类型,因为在项目中传递数据,用
				string
				类型最为方便,同时也便于代码的编写。只是在前台需要编写验证代码,免得有字符数据插入整数字段而造成数据库异常。
		
		
				
						 
				
		
		
				在
				com.game.products.dao.iface
				
						包中新建ProductsDao接口。
				
		
		
				
						代码如下所示:
				
		
		
				
						
						
				
		
		
				 package
				 com.game.products.dao.iface;
				package
				 com.game.products.dao.iface;

 import
				 java.util.List;
				import
				 java.util.List;

 import
				 com.game.products.model.Products;
				import
				 com.game.products.model.Products;


 public
				 
				interface
				 ProductsDao
				public
				 
				interface
				 ProductsDao 
				
						 {
				
				
						{
 List getProducts();
						//
						获得所有记录
    List getProducts();
						//
						获得所有记录
						
								
								 List getProducts(
						int
						 pageSize, 
						int
						 startRow);
						//
						获得一段记录
						
						    List getProducts(
						int
						 pageSize, 
						int
						 startRow);
						//
						获得一段记录
						
								
								 int
						 getRows();
						//
						获得总行数
						
						    
						int
						 getRows();
						//
						获得总行数
						
								
								 int
						 getRows(String fieldname,String value);
						//
						获得总行数
						
						    
						int
						 getRows(String fieldname,String value);
						//
						获得总行数
						
								
								 List queryProducts(String fieldname,String value);
						//
						根据条件查询的所有记录
						
						    List queryProducts(String fieldname,String value);
						//
						根据条件查询的所有记录
						
								
								 List queryProducts(String fieldname,String value,
						int
						 pageSize, 
						int
						 startRow);
						//
						根据条件查询的一段记录
						
						    List queryProducts(String fieldname,String value,
						int
						 pageSize, 
						int
						 startRow);
						//
						根据条件查询的一段记录
						
								
								 Products getProduct(String gameId);
						//
						根据ID获得记录
						
						    Products getProduct(String gameId);
						//
						根据ID获得记录
						
								
								 String getMaxID();
						//
						获得最大ID值
						
						    String getMaxID();
						//
						获得最大ID值
						
								
								 void
						 addProduct(Products pd);
						//
						添加记录
						
						    
						void
						 addProduct(Products pd);
						//
						添加记录
						
								
								 void
						 updateProductd(Products pd);
						//
						修改记录
						
						    
						void
						 updateProductd(Products pd);
						//
						修改记录
						
								
								 void
						 deleteProduct(Products pd);
						//
						删除记录
						
						    
						void
						 deleteProduct(Products pd);
						//
						删除记录    
						
								
								 }
						
						}
				
				
						
						 
				
		 
		
				
				 
		
		
				
						在com.game.products.dao.hibernate包中新建继承HibernateDaoSupport的ProductsMapDao类,并实现了ProductsDao接口。
				
		
		
				
						代码如下:
				
		
		
				 package
				 com.game.products.dao.hibernate;
				package
				 com.game.products.dao.hibernate;

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

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

 import
				 com.game.products.dao.iface.ProductsDao;
				import
				 com.game.products.dao.iface.ProductsDao;
 import
				 com.game.products.model.Products;
				import
				 com.game.products.model.Products;



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


 public
						 ProductsMapDao()
    
						public
						 ProductsMapDao()
						
								 {}
						
						
								{}
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:添加信息
     * 函数说明:添加信息
 * 参数说明:对象
     * 参数说明:对象 
 * 返回值:
     * 返回值:
 */
     
								*/
						
						
								
								 
								 public
						 
						void
						 addProduct(Products pd)
    
						public
						 
						void
						 addProduct(Products pd) 
						
								 {
						
						
								{
 this
								.getHibernateTemplate().save(pd);
        
								this
								.getHibernateTemplate().save(pd);
 }
    }
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:删除信息
     * 函数说明:删除信息
 * 参数说明: 对象
     * 参数说明: 对象
 * 返回值:
     * 返回值:
 */
     
								*/
						
						
								
								 
								 public
						 
						void
						 deleteProduct(Products pd)
    
						public
						 
						void
						 deleteProduct(Products pd) 
						
								 {
						
						
								{
 this
								.getHibernateTemplate().delete(pd);
        
								this
								.getHibernateTemplate().delete(pd);
 }
    }
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:获得所有的信息
     * 函数说明:获得所有的信息
 * 参数说明:
     * 参数说明: 
 * 返回值:信息的集合
     * 返回值:信息的集合
 */
     
								*/
						
						
								
								 
								 public
						 List getProducts()
    
						public
						 List getProducts() 
						
								 {
						
						
								{
 String sql
								=
								"
								FROM Products ORDER BY gameNameCn
								"
								;
        String sql
								=
								"
								FROM Products ORDER BY gameNameCn
								"
								;
 return
								 
								this
								.getHibernateTemplate().find(sql);
        
								return
								 
								this
								.getHibernateTemplate().find(sql);
 }
    }
						
						
								
								 
    

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:获得总行数
     * 函数说明:获得总行数
 * 参数说明:
     * 参数说明: 
 * 返回值:总行数
     * 返回值:总行数
 */
     
								*/
						
						
								
								 
								 public
						 
						int
						 getRows()
    
						public
						 
						int
						 getRows() 
						
								 {
						
						
								{
 String sql
								=
								"
								FROM Products ORDER BY gameNameCn
								"
								;
        String sql
								=
								"
								FROM Products ORDER BY gameNameCn
								"
								;
 List list
								=
								this
								.getHibernateTemplate().find(sql);
        List list
								=
								this
								.getHibernateTemplate().find(sql);
 return
								 list.size();
        
								return
								 list.size();
 }
    }
						
						
								
								 
    

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:获得一段记录信息
     * 函数说明:获得一段记录信息
 * 参数说明:
     * 参数说明: 
 * 返回值:信息的集合
     * 返回值:信息的集合
 */
     
								*/
						
						
								
								 
								 public
						 List getProducts(
						int
						 pageSize, 
						int
						 startRow) 
						throws
						 HibernateException
    
						public
						 List getProducts(
						int
						 pageSize, 
						int
						 startRow) 
						throws
						 HibernateException 
						
								 {
						
						
								{
 final
								 
								int
								 pageSize1
								=
								pageSize;
        
								final
								 
								int
								 pageSize1
								=
								pageSize;
 final
								 
								int
								 startRow1
								=
								startRow;
        
								final
								 
								int
								 startRow1
								=
								startRow;

 return
								 
								this
								.getHibernateTemplate().executeFind(
								new
								 HibernateCallback()
        
								return
								 
								this
								.getHibernateTemplate().executeFind(
								new
								 HibernateCallback()
								
										 {
								
								
										{


 public
										 List doInHibernate(Session session) 
										throws
										 HibernateException, SQLException
            
										public
										 List doInHibernate(Session session) 
										throws
										 HibernateException, SQLException 
										
												 {
										
										
												{
 Query query
												=
												session.createQuery(
												"
												FROM Products ORDER BY gameNameCn
												"
												);
                Query query
												=
												session.createQuery(
												"
												FROM Products ORDER BY gameNameCn
												"
												);
 query.setFirstResult(startRow1);
                query.setFirstResult(startRow1);
 query.setMaxResults(pageSize1);
                query.setMaxResults(pageSize1);
 return
												 query.list();
                
												return
												 query.list();
 }
            }
										
										
												
												 }
								
								);
        }
								
								);
 }
    }
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:获得一条的信息
     * 函数说明:获得一条的信息
 * 参数说明: ID
     * 参数说明: ID
 * 返回值:对象
     * 返回值:对象
 */
     
								*/
						
						
								
								 
								 public
						 Products getProduct(String gameId)
    
						public
						 Products getProduct(String gameId) 
						
								 {
						
						
								{
 return
								 (Products)
								this
								.getHibernateTemplate().get(Products.
								class
								,gameId);
        
								return
								 (Products)
								this
								.getHibernateTemplate().get(Products.
								class
								,gameId);
 }
    }
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:获得最大ID
     * 函数说明:获得最大ID
 * 参数说明:
     * 参数说明: 
 * 返回值:最大ID
     * 返回值:最大ID
 */
     
								*/
						
						
								
								 
								 public
						 String getMaxID()
    
						public
						 String getMaxID() 
						
								 {
						
						
								{
 String sql
								=
								"
								SELECT MAX(gameId)+1 FROM Products  
								"
								;
        String sql
								=
								"
								SELECT MAX(gameId)+1 FROM Products  
								"
								;
 String noStr 
								=
								 
								null
								;
        String noStr 
								=
								 
								null
								;
 List ll 
								=
								 (List) 
								this
								.getHibernateTemplate().find(sql);
        List ll 
								=
								 (List) 
								this
								.getHibernateTemplate().find(sql);
 Iterator itr 
								=
								 ll.iterator();
        Iterator itr 
								=
								 ll.iterator();

 if
								 (itr.hasNext())
        
								if
								 (itr.hasNext()) 
								
										 {
								
								
										{
 Object noint 
										=
										 itr.next();
            Object noint 
										=
										 itr.next();

 if
										(noint 
										==
										 
										null
										)
            
										if
										(noint 
										==
										 
										null
										)
										
												 {
										
										
												{
 noStr 
												=
												 
												"
												1
												"
												;
                noStr 
												=
												 
												"
												1
												"
												;                

 }
										
										else
            }
										
										else
										
												 {
										
										
												{
 noStr 
												=
												 noint.toString();
                noStr 
												=
												 noint.toString();
 }
            }
										
										
												
												 }
        }
								
								
										
										 
        

 if
								(noStr.length()
								==
								1
								)
        
								if
								(noStr.length()
								==
								1
								)
								
										 {
								
								
										{
 noStr
										=
										"
										000
										"
										+
										noStr;
            noStr
										=
										"
										000
										"
										+
										noStr;

 }
								
								else
								 
								if
								(noStr.length()
								==
								2
								)
        }
								
								else
								 
								if
								(noStr.length()
								==
								2
								)
								
										 {
								
								
										{
 noStr
										=
										"
										00
										"
										+
										noStr;
            noStr
										=
										"
										00
										"
										+
										noStr;

 }
								
								else
								 
								if
								(noStr.length()
								==
								3
								)
        }
								
								else
								 
								if
								(noStr.length()
								==
								3
								)
								
										 {
								
								
										{
 noStr
										=
										"
										0
										"
										+
										noStr;
            noStr
										=
										"
										0
										"
										+
										noStr;

 }
								
								else
        }
								
								else
								
										 {
								
								
										{
 noStr
										=
										noStr;
            noStr
										=
										noStr;
 }
        }
								
								
										
										 return
								 noStr;
        
								return
								 noStr;
 }
    }
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:修改信息
     * 函数说明:修改信息
 * 参数说明: 对象
     * 参数说明: 对象
 * 返回值:
     * 返回值:
 */
     
								*/
						
						
								
								 
								 public
						 
						void
						 updateProductd(Products pd)
    
						public
						 
						void
						 updateProductd(Products pd) 
						
								 {
						
						
								{
 this
								.getHibernateTemplate().update(pd);
        
								this
								.getHibernateTemplate().update(pd);
 }
    }
						
						
								
								 
								
								 
								 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:查询的所有信息
     * 函数说明:查询的所有信息
 * 参数说明: 集合
     * 参数说明: 集合
 * 返回值:
     * 返回值:
 */
     
								*/
						
						
								
								 
								 public
						 List queryProducts(String fieldname,String value)
    
						public
						 List queryProducts(String fieldname,String value) 
						
								 {
						
						
								{
 System.out.println(
								"
								value: 
								"
								+
								value);
        System.out.println(
								"
								value: 
								"
								+
								value);
 String sql
								=
								"
								FROM Products where 
								"
								+
								fieldname
								+
								"
								 like '%
								"
								+
								value
								+
								"
								%'
								"
								+
								"
								ORDER BY gameNameCn
								"
								;
        String sql
								=
								"
								FROM Products where 
								"
								+
								fieldname
								+
								"
								 like '%
								"
								+
								value
								+
								"
								%'
								"
								+
								"
								ORDER BY gameNameCn
								"
								;
 return
								 
								this
								.getHibernateTemplate().find(sql);
        
								return
								 
								this
								.getHibernateTemplate().find(sql);
 }
    }
						
						
								
								 
    

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:获得总行数
     * 函数说明:获得总行数
 * 参数说明:
     * 参数说明: 
 * 返回值:总行数
     * 返回值:总行数
 */
     
								*/
						
						
								
								 
								 public
						 
						int
						 getRows(String fieldname,String value)
    
						public
						 
						int
						 getRows(String fieldname,String value) 
						
								 {
						
						
								{
 String sql
								=
								"
								FROM Products where 
								"
								+
								fieldname
								+
								"
								 like '%
								"
								+
								value
								+
								"
								%'
								"
								+
								"
								ORDER BY gameNameCn
								"
								;
        String sql
								=
								"
								FROM Products where 
								"
								+
								fieldname
								+
								"
								 like '%
								"
								+
								value
								+
								"
								%'
								"
								+
								"
								ORDER BY gameNameCn
								"
								;
 List list
								=
								this
								.getHibernateTemplate().find(sql);
        List list
								=
								this
								.getHibernateTemplate().find(sql);
 return
								 list.size();
        
								return
								 list.size();
 }
    }
						
						
								
								 
    

 /** */
						
								/**
    
						/** */
						
								/**
								
										
										 * 函数说明:查询的一段信息
     * 函数说明:查询的一段信息
 * 参数说明: 集合
     * 参数说明: 集合
 * 返回值:
     * 返回值:
 */
     
								*/
						
						
								
								 
								 public
						 List queryProducts(String fieldname,String value,
						int
						 pageSize, 
						int
						 startRow)
    
						public
						 List queryProducts(String fieldname,String value,
						int
						 pageSize, 
						int
						 startRow) 
						
								 {
						
						
								{
 final
								 
								int
								 pageSize1
								=
								pageSize;
        
								final
								 
								int
								 pageSize1
								=
								pageSize;
 final
								 
								int
								 startRow1
								=
								startRow;
        
								final
								 
								int
								 startRow1
								=
								startRow;
 final
								 String sql
								=
								"
								FROM Products where 
								"
								+
								fieldname
								+
								"
								 like '%
								"
								+
								value
								+
								"
								%'
								"
								+
								"
								ORDER BY gameNameCn
								"
								;
        
								final
								 String sql
								=
								"
								FROM Products where 
								"
								+
								fieldname
								+
								"
								 like '%
								"
								+
								value
								+
								"
								%'
								"
								+
								"
								ORDER BY gameNameCn
								"
								;

 return
								 
								this
								.getHibernateTemplate().executeFind(
								new
								 HibernateCallback()
        
								return
								 
								this
								.getHibernateTemplate().executeFind(
								new
								 HibernateCallback()
								
										 {
								
								
										{


 public
										 List doInHibernate(Session session) 
										throws
										 HibernateException, SQLException
            
										public
										 List doInHibernate(Session session) 
										throws
										 HibernateException, SQLException 
										
												 {
										
										
												{
 Query query
												=
												session.createQuery(sql);
                Query query
												=
												session.createQuery(sql);
 query.setFirstResult(startRow1);
                query.setFirstResult(startRow1);
 query.setMaxResults(pageSize1);
                query.setMaxResults(pageSize1);
 return
												 query.list();
                
												return
												 query.list();
 }
            }
										
										
												
												 }
								
								);
        }
								
								);
 }
    }
						
						
								
								 
								
								 }
}
				
				
						
						 
				
		 
		
				
				
						
						
				
		
		
				
						
								
								 
						
				
		
		
				
						在com.game.bean.hibernate包中新建hibernate.cfg.xml,代码如下:
				
		
		
				
						
								
								
						
				
		
		
				 <?
				xml version="1.0" encoding="GB2312"
				?>
				<?
				xml version="1.0" encoding="GB2312"
				?>
				
						
						 <!
				DOCTYPE hibernate-configuration PUBLIC
				
				<!
				DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
				>
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
				>
				
						
						 <
				hibernate-configuration
				>
				
				<
				hibernate-configuration
				>
				
						
						 <
				session-factory
				>
    
				<
				session-factory
				>
				
						
						 <
				property 
				name
				="dialect"
				>
				org.hibernate.dialect.SQLServerDialect
				</
				property
				>
        
				<
				property 
				name
				="dialect"
				>
				org.hibernate.dialect.SQLServerDialect
				</
				property
				>
				
						
						 <
				property 
				name
				="show_sql"
				>
				true
				</
				property
				>
        
				<
				property 
				name
				="show_sql"
				>
				true
				</
				property
				>
				
						
						 
						
						 <
				mapping 
				resource
				="com/game/products/model/products.hbm.xml"
				></
				mapping
				>
        
				<
				mapping 
				resource
				="com/game/products/model/products.hbm.xml"
				></
				mapping
				>
				
						
						 </
				session-factory
				>
    
				</
				session-factory
				>
				
						
						 </
				hibernate-configuration
				>
				
				</
				hibernate-configuration
				>
		 
		
				
 
		
				
						至此,DAO层的代码已经编写完成。下一篇,将编写service层代码。
						
						
				
		
		
				
						
								
										struts+spring+hibernate
								
								的
								
										web
								
								应用
								
										<
								
								三
								
										> Service
								
								层代码编写
						
				
		
	posted on 2007-03-13 13:57 
千山鸟飞绝 阅读(19844) 
评论(11)  编辑  收藏  所属分类: 
Web开发