技术开发 频道

玩玩Spring之struts+hibernate+spring添删改查示例



第四步,商业逻辑层(数据库开发工程师以及主程序员等)
 
  本来还可以分成很多层的,笔者怕把新手给晕得一个不剩了,所以把N层先暂时合并到一层,其实这也是我们在一般的实际项目中经常使用的方式。
  本例中采用的是hibernate来做数据库访问中间件,因此,请新同学按以下步骤进行:

1、设计一个实现IUser接口的类,用来做VO(PO)。

User.java的内容全部如下:
package com.easyjf.example.business.hibernate;
import com.easyjf.example.business.IUser;
public class User  implements IUser,java.io.Serializable {
 private static final long serialVersionUID=10083494895498l;
    // Fields  
 private String cid;
 private String userName;
 private String password;
 private String email;
 private String tel;
 private String birthday;
 private String intro;
    // Constructors
    /** default constructor */
    public User() {
    }   
    /** constructor with id */
    public User(String cid) {
        this.cid = cid;
    }
 public String getCid() {
  return cid;
 }
 public void setCid(String cid) {
  this.cid = cid;
 }
  …….//更多的垃圾getter及setter代码省略
 
}
 
2、写hibernate的配置文件
 
  然后结合User.java及数据库管理员建的表结构,写hibernate映射配置文件User.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.easyjf.example.business.hibernate.User" table="User">
        <id name="cid" type="string">
            <column name="cid" length="16" />
             <generator class="com.easyjf.example.business.hibernate.HibIdGenerator">
            <param name="column">cid</param>
            <param name="length">16</param>
            </generator>
        </id>
        <property name="userName" type="string">
            <column name="username" length="16" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="16" not-null="true" />
        </property>
        <property name="email" type="string">
            <column name="Email" length="50" />
        </property>
        <property name="tel" type="string">
            <column name="tel" length="50" />
        </property>
         <property name="birthday" type="string">
            <column name="birthday" length="23" />
        </property>       
        <property name="intro" type="string">
            <column name="intro" length="500" />
        </property>
    </class>
</hibernate-mapping>
 
  注意其中有关主键生成的部份配置如下:
<generator class="com.easyjf.example.business.hibernate.HibIdGenerator">
  笔者这里使用的是自己的主键值生成器。你也可以使用hibernate自带的默认主键生成器,怎么用就不说了。大家可以从本示例完整代码下载中获得HibIdGenerator.java类的源代码,
 
3、 写IUserService的实现

  这里由于使用的是hibernate,而Spring中提供了hibernate DAO,可以让使用hibernate的朋友们更加简单使用hibernate访问数据库,该示例中把Service及DAO合并在一起。全部UserDao的代码如下:

package com.easyjf.example.business.hibernate;
import java.util.Collection;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.easyjf.example.business.IUser;
import com.easyjf.example.business.IUserService;
public class UserDao extends HibernateDaoSupport implements IUserService {
 public boolean del(IUser user) {
  boolean ret=true;
  try{
  this.getHibernateTemplate().delete(user);
  }
  catch(Exception e)
  {
   ret=false;
  }
  return ret;
 } 
 public List query(String scope,Collection paras) {
  return this.getHibernateTemplate().find("from User where "+scope,paras.toArray());  
 }
 public IUser login(String userName, String password, String ip) {
  IUser user=readByName(userName);
  if(user==null || user.getPassword()==null ||(!user.getPassword().equals(password)))//用户不存在或者密码不正确
  {
  return null;
  }  
  return user; 
 }
 public IUser newUser() {  
  return new User();
 }
 public IUser read(String cid) {  
  return (IUser)this.getHibernateTemplate().get(User.class,cid);
 }
 public IUser readByName(String userName) {  
  List list= this.getHibernateTemplate().find("from User where userName='"+userName+"'");
  if(list!=null)return (IUser)list.get(0);
  else
   return null;
 }
 public boolean save(IUser user) {  
  boolean ret=true;
  try{
  this.getHibernateTemplate().save(user);
  }
  catch(Exception e)
  {
   ret=false;
  }
  return ret; 
 } 
 public boolean update(IUser user) {
  boolean ret=true;
  try{
  this.getHibernateTemplate().update(user);
  }
  catch(Exception e)
  {
   e.printStackTrace();
   ret=false;
  }
  return ret; 
 } 
}
 
  从代码中我们看到,UserDao类继承于Spring的HibernateDaoSupport类,目的在于方便通过hibernate操作数据库,同时实现了系统设计师所设计的IUserService接口中的相关功能。
  是对是错,可以自己单元测试一下。
 
  完了吗?还没有呢。下面是最后一步,也是本文所要介绍的重点,主角Spring正式登场,大家鼓掌。(咦,刚才在Struts即hibernate部分不是都提到Spring的吗?哦,那只是客串!)

 
0
相关文章