如何实现webwork+spring+hibernate框架的结合应用

现在WebWork+Spring+Hibernate这种框架已经越来越得到人们地推荐使用,至于这种框架的优越性以及为什么选择这种框架的结合不在本文范畴;另外对于一些项目中基本的构建将不在本文中详细叙述;下面就开始正题,如何建立这种框架应用:
第一步:当然是首先要构建一个Java web project项目,建立好项目以后创建包的命名。在本例中建立了项目目录与包结构如下图:

如何实现webwork+spring+hibernate框架的结合应用如何实现webwork+spring+hibernate框架的结合应用

第二步:导入所有须要的资源类库:

如何实现webwork+spring+hibernate框架的结合应用


第三步:项目搭建好以后,开始正式的程序编写开发工作。
首先在com.etong.bookstore.vo包面分别建立以下几个类(Book、Catalog、Orderitem、Orders、User)。
由于受篇幅所限,本实例中只列出User类的相关代码。

User类文件代码内容如下:
package com.etong.bookstore.vo;

import java.util.HashSet;
import java.util.Set;

/***/ /**
*
*<p>Title:</p>
*<pre>Description:com.etong.bookstore.vo.User.java</pre>
*<p>Copyright:Copyright(c)2008</p>
*Jan16,2008
*
@authorAllen
*
@version1.0
*@hibernate.class
*/

@SuppressWarnings(
" serial " )
public class User implements java.io.Serializable ... {


//Fields

/***//**
*@hibernate.id
*generator-class="native"
*/

privateIntegeruserid;

/***//**
*@hibernate.propertynot-null="true"length="20"
*/

privateStringusername;

/***//**
*@hibernate.propertynot-null="true"length="20"
*/

privateStringpassword;

/***//**
*@hibernate.propertylength="4"
*/

privateStringsex;

/***//**
*@hibernate.property
*/

privateIntegerage;

/***//**
*@hibernate.setcascade="delete"inverse="true"
*@hibernate.keycolumn="userid"
*@hibernate.one-to-many
*class="com.etong.bookstore.vo.Orders"
*/

@SuppressWarnings(
"unchecked")
privateSetorderses=newHashSet(0);


//Constructors

/***//**defaultconstructor*/
publicUser()...{
}


/***//**minimalconstructor*/
publicUser(Stringusername,Stringpassword)...{
this.username=username;
this.password=password;
}


/***//**fullconstructor*/
@SuppressWarnings(
"unchecked")
publicUser(Stringusername,Stringpassword,Stringsex,Integerage,Setorderses)...{
this.username=username;
this.password=password;
this.sex=sex;
this.age=age;
this.orderses=orderses;
}



//Propertyaccessors

publicIntegergetUserid()...{
returnthis.userid;
}


publicvoidsetUserid(Integeruserid)...{
this.userid=userid;
}


publicStringgetUsername()...{
returnthis.username;
}


publicvoidsetUsername(Stringusername)...{
this.username=username;
}


publicStringgetPassword()...{
returnthis.password;
}


publicvoidsetPassword(Stringpassword)...{
this.password=password;
}


publicStringgetSex()...{
returnthis.sex;
}


publicvoidsetSex(Stringsex)...{
this.sex=sex;
}


publicIntegergetAge()...{
returnthis.age;
}


publicvoidsetAge(Integerage)...{
this.age=age;
}


@SuppressWarnings(
"unchecked")
publicSetgetOrderses()...{
returnthis.orderses;
}


@SuppressWarnings(
"unchecked")
publicvoidsetOrderses(Setorderses)...{
this.orderses=orderses;
}



}


将相关的所有数据库类文件建立好以后,就可以通过Ant来构建hbm.xml以及hibernate.cfg.xml文件了。

第四步:利用Ant来进行构建生成hbm.xml文件.其中Ant代码内容如下:

<? xmlversion="1.0"encoding="GBK" ?>
<!--
作者:Allen
createdate:2007-08-20
-->
< project name ="bookstore" default ="compile" basedir ="." >

<!-- 数据库相关信息 -->
< property name ="jdbcdriver" value ="com.mysql.jdbc.Driver" />
< property name ="jdbcurl" value ="jdbc:mysql://localhost:3306/bookstore" />
< property name ="dialect" value ="org.hibernate.dialect.MySQLDialect" />
< property name ="jdbcusername" value ="root" />
< property name ="jdbcpassword" value ="Allen" />
< property name ="jdbcpoolsize" value ="1" />
< property name ="showsql" value ="true" />

<!-- properies源文件夹 -->
< property name ="main.java.dir" value ="src/main/java" />
< property name ="main.resource.dir" value ="src/main/resources" />
< property name ="junitsrc.dir" value ="src/test/java" />
< property name ="junitsrc.resource.dir" value ="src/test/resources" />

<!-- temporarybuilddirectorynames编译及引用类库文件夹 -->
< property name ="classes.dir" value ="build/classes" />
< property name ="bin.dir" value ="bin" />
< property name ="dist.dir" value ="dist" />
< property name ="lib.dir" value ="WebRoot/WEB-INF/lib" />
< property name ="extend.lib.dir" value ="vendor/lib" />
< property name ="xdoclet2.dir" value ="vendor/xdoclet2" />

<!-- 指定输出SQL语句的位置和名称 -->
< property name ="output" value ="schema-export.sql" />
<!-- SystemInitinfo -->
< property name ="initClassName" value ="com.etong.bookstore.init.BookStoreSystemInit" />
< property name ="initSqlFile" value ="systeminit-mssql.sql" />

<!-- 定义web应用解压文件输出目录 -->
< property name ="war-output-file" value ="D:/software_tools/apache-tomcat-6.0.14/webapps" />
<!-- 定义classpath -->
< path id ="master-classpath" >
<!-- 替换如下方式
<filesetdir="${lib.dir}">
<includename="**/*.jar"/>
</fileset>
-->
<!--
也可以写成如下方式,此方式同上面方式基本一致,这种方式可以通过include来限定
导入的类库,如果不限定即可使用通配符**/*来替代!
-->
< fileset file ="${lib.dir}/*.jar" />
< fileset file ="${extend.lib.dir}/*.jar" />
< pathelement path ="${classes.dir}" />
</ path >

<!-- 初始化开始任务 -->

< target name ="init" >
< mkdir dir ="${main.java.dir}" />
< mkdir dir ="${main.resource.dir}" />
< mkdir dir ="${junitsrc.dir}" />
< mkdir dir ="${junitsrc.resource.dir}" />
< mkdir dir ="${bin.dir}" />
< mkdir dir ="${dist.dir}" />
< mkdir dir ="${classes.dir}" />
< mkdir dir ="${extend.lib.dir}" />
</ target >

<!-- 清除任务 -->
< target name ="clean" >
< delete dir ="${classes.dir}" />
</ target >

<!-- 清除所有任务 -->
< target name ="cleanAll" depends ="clean" >
< delete dir ="${main.java.dir}" />
< delete dir ="${bin.
分享到:
评论

你可能感兴趣的:(spring,框架,Hibernate,ant,Webwork)