本文主要使用myeclipse来开发整合了struts和hibernate的应用,重点在于对二者myeclipse提供支持的工具的使用,如果对于struts和hibernate还没有了解的朋友,建议先了解一些struts和hibernate的相关概念。
1、首先新建一个j2ee的web project,具体的设置如图所示:

点击,完成,完成新建项目。
2、在mydb项目上点击右键,选择myeclipse选项,为该项目增加struts的特性。

弹出如下窗口,具体配置,如图所示:

只需修改包的名称,本例中修改为:com.xiebing.struts ,其他保持默认即可。点击完成,myeclipse就会自动将struts应用所需要的jar包和自定义标签文件,以及struts的配置文件和资源文件拷贝或创建到相应的目录。
3、创建数据库
在mysql的test的数据库中创建表:vipdata
drop table if exists vipdata;
create table vipdata (
vipid int(11) not null auto_increment,
vipname varchar(20) not null,
viptitle varchar(20) not null,
primary key (vipid))
type=myisam;
4、打开myeclipse database explorer视图,创建一个数据库的连接:
新建一个数据库连接:

在新的数据库连接的配置窗口中,填写如下配置:

其中driver的配置,通过 窗口-首选项 myeclipse database explorer drivers来配置,如下图所示:

点击edit按钮,对该选项进行配置,配置细节如下,extra class path 要选中一个mysql的jdbc驱动:

在db browser的窗口中连接数据库,连接成功后如图所示:

5、重新切换到myeclipse视图,在项目上点击右键选择myeclipse选项,为应用增加hibernate特性,如图所示:

在出现的配置窗口中,选中“add hibernate 2.1 libraries to project?”,然后设定存放hibernate库文件的目录为: /web-inf/lib 目录,默认会选择创建一个新的hibernate配置文件hibernate.cfg.xml。

点击下一步,进入下面的hibernate数据库连接配置界面,在connection profile选项中直接选择在myeclipse database explorer中配置的vipdata选项,然后就会自动生成其他的配置,可以选择“copy jdbc driver and add to classpath”,这样就会将jdbc驱动拷贝到web-inf/lib目录中。:

点击下一步,来创建hibernate的sessionfactory类,这是一个简单的集中管理hibernate会话的工厂类,填写类的全名称。

点击完成,然后myeclipse就会将hibernate相关的jar包拷贝到lib目录下,同时会生成hibernate的配置文件:hibernate.cfg.xml,和sessionfactory类(改类在下面我会给出简单说明)。
现在要利用myeclipse database explorer视图中的工具来生成,hibernate的映射文件。切换到myeclipse database explorer视图,在表vipdata上点击右键,选择create hibernate mapping,如图所示:

弹出如下配置界面,配置生成的持久化类和映射文件。

点击browse,选择生成的类和映射文件的包:com.xiebing.hibernate

id generator选项,选择native。(还有很多其他的选项可选,根据应用的需要具体选择),

点击完成,这样会生成持久化类vipdata和它的父类abstractvipdata(生成父类,有利于日后应用的扩展,工具会自动生成详细的equals方法和hashcode方法)以及映射文件vipdata.hbm.xml。同时会修改hibernate的配置文件hibernate.cfg.xml,会增加一行:
<mapping resource=“com/xiebing/hibernate/vipdata.hbm.xml”/>
6、使用myeclipse struts editor打开struts的配置文件:struts-config.xml,并切换到design面板。

选择面板上的add jsp page标签,单击,我们来新建一个jsp页面。然后到design面板上单击:

出现如下所示的界面,具体设置如图所示:

修改addvipdata.jsp的代码如下:
<%@ page language=“java” contenttype=“text/html; charset=gbk” %>
<%@ taglib uri=“http://jakarta.apache.org/struts/tags-bean” prefix=“bean” %>
<%@ taglib uri=“http://jakarta.apache.org/struts/tags-html” prefix=“html” %>
<%@ taglib uri=“http://jakarta.apache.org/struts/tags-logic” prefix=“logic” %>
<%@ taglib uri=“http://jakarta.apache.org/struts/tags-tiles” prefix=“tiles” %>
<%@ taglib uri=“http://jakarta.apache.org/struts/tags-template” prefix=“template” %>
<%@ taglib uri=“http://jakarta.apache.org/struts/tags-nested” prefix=“nested” %>
<%@ page import=“com.xiebing.hibernate.vipservice” %>
<%@ page import=“java.util.list” %>
<!doctype html public “-//w3c//dtd html 4.01 transitional//en”>
<html:html locale=“true”>
<head>
<title>vip example</title>
<html:base/>
</head>
<body bgcolor=“white”>
<h3>vip example</h3>
<html:errors/>
<%
/*
* 这里将产生一个vipdata的集合
*/
list viplist = vipservice.getinstance().getvipdatalist();
request.setattribute(“vipdatas”, viplist);
%>
<p>列出 <code>vipdata</code>表中的所有信息.</p>
<table border=1>
<logic:iterate id=“element” name=“vipdatas” scope=“request” type=“com.xiebing.hibernate.vipdata” >
<tr>
<td><bean:write name=“element” property=“vipname” /></td>
<td><bean:write name=“element” property=“viptitle” /></td>
</tr>
</logic:iterate>
</table>
<p>新增加一条数据:</p>
<html:form action=“addvipdata.do” method=“post”>
<table border=1>
<tr><td>name:</td><td><html:text property=“vipname” /></tr>
<tr><td>description:</td><td><html:text property=“viptitle” /></td></tr>
</table><br/>
<html:submit/>
</html:form>
</body>
</html:html>阿飞发送多幅发送给扶绥多给
此时加粗的部分会提示有问题(先不管他),因为还没有创建类:com.xiebing.hibernate.vipservice
7、接下来就创建类:com.xiebing.hibernate.vipservice。其主要就是完成我们的增删改查等具体业务逻辑的处理。
代码如下:
vipservice.java
package com.xiebing.hibernate;
import java.util.list;
import net.sf.hibernate.hibernateexception;
import net.sf.hibernate.objectnotfoundexception;
import net.sf.hibernate.query;
import net.sf.hibernate.session;
import com.xiebing.hibernate.vipdata;
/**
* 通过hibernate来操作数据库的业务逻辑类
*
*/
public class vipservice
{
private static vipservice instance = null;
private vipservice()
{
}
/**
* 得到vipservice的单态实例
* @return <code>vipservice</code> singleton.
*/
public static synchronized vipservice getinstance()
{
if (instance == null)
{
instance = new vipservice();
}
return instance;
}
/**
* 通过id来获得vipdata
* @param id
* @return vipdata
*/
public vipdata getvipdata(long id)
{
session session = null;
try
{
//通过sessionfactory创建session实例
session = sessionfactory.currentsession();
//调用load方法加载数据
return (vipdata) session.load(vipdata.class, id);
}
catch (objectnotfoundexception onfe)
{
return null;
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
finally
{
if (session != null)
{
try
{
//关闭session
session.close();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
}
}
}
/**
* 更新记录
*
* @param vipdata
*/
public void updatevipdata(vipdata vipdata)
{
session session = null;
try
{
session = sessionfactory.currentsession();
//执行update操作
session.update(vipdata);
session.flush();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
}
}
}
/**
* 得到所有的记录
*
* @return 记录的列表
*/
public list getvipdatalist()
{
session session = null;
try
{
session = sessionfactory.currentsession();
//创建一条hql查询
query query =
session.createquery(
“select vipdata from com.xiebing.hibernate.vipdata vipdata order by vipdata.vipname”);
return query.list();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
}
}
}
/**
* 添加一条新的记录
* @param data
*/
public void addvipdata(vipdata data)
{
session session = null;
try
{
session = sessionfactory.currentsession();
session.save(data);
session.flush();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (hibernateexception e)
{
system.err.println(“hibernate exception” + e.getmessage());
throw new runtimeexception(e);
}
}
}
}
}
8、顺便简单说一下工具自动生成的sessionfactory的代码,sessionfactory.java
package com.xiebing.hibernate;
import net.sf.hibernate.hibernateexception;
import net.sf.hibernate.session;
import net.sf.hibernate.cfg.configuration;
public class sessionfactory {
private static string config_file_location = “/hibernate.cfg.xml”;
/** 保持session对象为单态
* 初始化一个threadlocal对象
* */
private static final threadlocal threadlocal = new threadlocal();
private static final configuration cfg = new configuration();
private static net.sf.hibernate.sessionfactory sessionfactory;
/**
* returns the threadlocal session instance.
*
* @return session
* @throws hibernateexception
*/
public static session currentsession() throws hibernateexception {
//多线程情况下共享数据库链接是不安全的。threadlocal保证了每个线程都有自己的s(数据库连接)
session session = (session) threadlocal.get();
if (session == null) {
if (sessionfactory == null) {
try {
cfg.configure(config_file_location);
sessionfactory = cfg.buildsessionfactory();
}
catch (exception e) {
system.err.println(“%%%% error creating sessionfactory %%%%”);
e.printstacktrace();
}
}
//创建一个数据库连接实例
session = sessionfactory.opensession();
//保存该数据库连接s到threadlocal中
threadlocal.set(session);
}
return session;
}
/**
* close the single hibernate session instance.
*
* @throws hibernateexception
*/
public static void closesession() throws hibernateexception {
session session = (session) threadlocal.get();
threadlocal.set(null);
if (session != null) {
session.close();
}
}
/**
* default constructor.
*/
private sessionfactory() {
}
}
