这是个用JSP+Hibernate编写的非常简单的留言系统例子,是个准三层结构(因为为了节省时间着重介绍hibernate部分,省去的service层,转而使用jsp页面进行业务处理,但并不是在jsp页面上过多的编写java代码,而是使用专门的jsp作为业务处理层)
开发工具:MyEclipse+Mysql
目录结构如下:
config(配置类)
BeanConfig.java
MessageConfig.java
constants(常量类)
Constants.java
dao(数据访问类)
hibernate
MessageDAO.java
UserDAO.java
IMessageDAO.java
IUserDAO.java
exception(异常类)
MessageAuthorityException.java
MessageDAOException.java
MessageException.java
model(模型类)
Message.hbm.xml
Message.java
User.hbm.xml
User.java
sql (数据库脚本)
scrip.sql
util(工具类)
DaoFactory.java
HibernateDAO.java
HibernateFilter.java
HibernateUtil.java
BeanConfig.java
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import cn.hxex.message.exception.MessageException;
public class BeanConfig
{
private String id;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this .type = type;
}
private Object instance;
public Object getInstance()
{
if ( null != instance ) return instance;
try
{
Class cls = Class.forName( getType() );
instance = cls.newInstance();
return instance;
}
catch ( Exception ex )
{
throw new MessageException( " Couldn't find class: " + getType() );
}
}
public String toString()
{
return ReflectionToStringBuilder.toString( this );
}
}
MessageConfig
import java.util.Hashtable;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import cn.hxex.message.exception.MessageException;
/**
* 系统配置信息类
*/
public class MessageConfig
{
public static Hashtable beans;
/**
* 构造函数
*/
public MessageConfig()
{
beans = new Hashtable();
}
/**
* 增加一个BeanConfig对象
* @param bean
*/
public void addBean(BeanConfig bean)
{
beans.put(bean.getId(), bean);
}
/**
* 得到一个DAO接口对象的实例
* @param name DAO接口对象的名称
* @return 指定DAO接口的实现类的实例
*/
public Object getBean(String name)
{
BeanConfig config = (BeanConfig) beans.get(name);
if (config == null )
{
throw new MessageException( " Couldn't find the bean: " + name);
}
return config.getInstance();
}
public String toString()
{
return ReflectionToStringBuilder.toString( this );
}
}
Constants
public class Constants {
public static final String SESSION_USER = " cn.hxex.message.user " ;
}
IMessageDAO
import java.util.List;
import cn.hxex.message.model.Message;
public interface IMessageDAO
{
public void saveMessage( Message message );
public void updateMessage( Message message );
public List getMessages( );
public void deleteMessage( String id, String userId );
public Message getMessage( String id );
}
IUserDAO
import cn.hxex.message.model.User;
public interface IUserDAO
{
public void saveUser( User user );
public User getUser( String username );
public User getUserById( String id );
}
MessageDAO
import java.util.List;
import cn.hxex.message.dao.IMessageDAO;
import cn.hxex.message.exception.MessageDAOException;
import cn.hxex.message.model.Message;
import cn.hxex.message.util.HibernateDAO;
/**
* IMessageDAO接口的Hibernate实现
*/
public class MessageDAO extends HibernateDAO implements IMessageDAO
{
/**
* 保存留言信息
*
* @param message
* 被保存的留言对象
*/
public void saveMessage(Message message)
{
super .saveObject(message);
}
/**
* 得到所有的留言信息
*
* @return 返回所有的