您的位置:首页 > 编程语言 > Java开发

struts+spring+hibernate的web应用 Dao层代码编写

2008-02-25 05:52 537 查看
导读:
  JAVA涂鸦
  posts - 46, comments - 429, trackbacks - 0
  前一篇文章让我们打好了架子,接下来就来编写代码了。在编码之前,我们需要先自行了解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"?>  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
  ?????????????
  注意这里的ID不是数据库自动生成的,而是根据需要由程序生成,一般项目中的主键ID都是采取这种方式。
  然后在这个包中再新建Products类,代码如下:
  package?com.game.products.model;public?class?Products?{
  //????Fields?????private?String?gameId;//编号????private?String?gameNameCn;//中文名称????private?String?gameNameEn;//英文名称????private?String?gameCapacity;//碟数????private?String?gameVersion;//版本????private?String?gameMedia;//介质????private?String?gameCopyright;//版权????private?String?gamePrice;//价格????private?String?gameContent;//攻略
  
  //????Constructors????public?Products(){}????
  //????Property?accessors????public?String?getGameCapacity()?{
  return?gameCapacity;
  }????public?void?setGameCapacity(String?gameCapacity)?{
  this.gameCapacity?=?gameCapacity;
  }????public?String?getGameId()?{
  return?gameId;
  }????public?void?setGameId(String?gameId)?{
  this.gameId?=?gameId;
  }????public?String?getGameNameCn()?{
  return?gameNameCn;
  }????public?void?setGameNameCn(String?gameNameCn)?{
  this.gameNameCn?=?gameNameCn;
  }????public?String?getGameNameEn()?{
  return?gameNameEn;
  }????public?void?setGameNameEn(String?gameNameEn)?{
  this.gameNameEn?=?gameNameEn;
  }????public?String?getGameVersion()?{
  return?gameVersion;
  }????public?void?setGameVersion(String?gameVersion)?{
  this.gameVersion?=?gameVersion;
  }????public?String?getGameMedia()?{
  return?gameMedia;
  }????public?void?setGameMedia(String?gameMedia)?{
  this.gameMedia?=?gameMedia;
  }????public?String?getGameCopyright()?{
  return?gameCopyright;
  }????public?void?setGameCopyright(String?gameCopyright)?{
  this.gameCopyright?=?gameCopyright;
  }????public?String?getGameContent()?{
  return?gameContent;
  }????public?void?setGameContent(String?gameContent)?{
  this.gameContent?=?gameContent;
  }????public?String?getGamePrice()?{
  return?gamePrice;
  }????public?void?setGamePrice(String?gamePrice)?{
  this.gamePrice?=?gamePrice;
  }}
  需要注意的是,我这里都是采用了string类型,因为在项目中传递数据,用string类型最为方便,同时也便于代码的编写。只是在前台需要编写验证代码,免得有字符数据插入整数字段而造成数据库异常。
  在com.game.products.dao.iface包中新建ProductsDao接口。
  代码如下所示:
  package?com.game.products.dao.iface;import?java.util.List;import?com.game.products.model.Products;public?interface?ProductsDao?{
  List?getProducts();//获得所有记录????List?getProducts(int?pageSize,?int?startRow);//获得一段记录????int?getRows();//获得总行数????int?getRows(String?fieldname,String?value);//获得总行数????List?queryProducts(String?fieldname,String?value);//根据条件查询的所有记录????List?queryProducts(String?fieldname,String?value,int?pageSize,?int?startRow);//根据条件查询的一段记录????Products?getProduct(String?gameId);//根据ID获得记录????String?getMaxID();//获得最大ID值????void?addProduct(Products?pd);//添加记录????void?updateProductd(Products?pd);//修改记录????void?deleteProduct(Products?pd);//删除记录????}
  在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"?>  "-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">????????????org.hibernate.dialect.SQLServerDialect????????true????????????
  至此,DAO层的代码已经编写完成。下一篇,将编写service层代码。
  千山鸟飞绝评论(4)编辑收藏Web开发
  FeedBack:
  # re: struts+spring+hibernate的web应用 Dao层代码编写
  支持一下,
  赞一个 回复 更多评论
  
  # re: struts+spring+hibernate的web应用 Dao层代码编写[未登录] 阿蜜果
  对异常的处理呢?
  这个问题我一直觉得头大,没找到我满意的方法
  可是这个web应用里面我好像根本没见到对异常的处理哦。
  嘻嘻,写得很详细,不错,支持! 回复 更多评论
  
  # re: struts+spring+hibernate的web应用 Dao层代码编写
  你好,我对struts+spring+hibernate是初次接触,看了你的例子,我就模仿你的文章写了一个程序,可是在TOMCAT-5.5下运行,却报告错误:
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/getProducts' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'productsService' while setting bean property 'productsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productsService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'productsDao' while setting bean property 'productsDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productsDao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
  仔细对照了一下代码,没找到错误,可否致电一下,看看到此可能是那里的错误,应该怎么改!!
  先谢谢了,我现在急需要搞定一个例子,以便好好研究一下!
  我的QQ是:410125014,如果方便的话,可以QQ聊一下,希望支持一下,谢谢!!! 回复 更多评论
  
  # re: struts+spring+hibernate的web应用 Dao层代码编写
  good 回复 更多评论
  

本文转自
http://www.blogjava.net/rickhunter/articles/103517.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息