下面能够通过功能的实现来说明Struts和Hibernate的整合过程,并以此来说明整个系统的是实现过程。
(1)Hibernate的实现
1,建立Address表的持久化类Address.java,类中的属性与Address表中的字段相对应,并为之添加相应的Getter和Setter方法:
private Integer id;//定义与Address表中字段对应的属性(主键)
private String username://定义用户名字
public Integer getId(){//添加Getter方法
return this id:
}
public void setId(Integer id){//添加Setter方法
this.id=id:
}
public void getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}
2,建立于Address表与持久化类的描述文件Address.hbm.xml:
<hibernate-mapping>
<!-关联Address持久化类和Address持久化表->
<class name="peng.liang.oa.hibernate.beans.Address"
table="address"catalog="oa">
<!=指定持久化类的属性和数据库表中的对应关系->
<!=指定主健-->
<id name="id "type="java.lang.lnteger">
<column name="ID"/>
<generator class="native"/>
</id>
<!—映射类中的Username与表中的Username—>
<property name="username"type="java.lang.String"
<column name="username"length="50"not-null="true"/>
</property>
</class>
</hibernate-mapping>
3.建立对应的DAO类Address DAO;
public class AddressDAO extends HibernateBaseDAO{
public static final String USERNAME=“username“;
//插入记录
public void save(Address transientInstance){
Session=null;
Transaction tx=null;
try{
session=getSession();
tx=session.beginTransaction();
session.save(transientInstance);;
tx.commit();
}catch (Exception re){
tx.rollback();
re.printStackTrace();
}finally{
session.close();
}
}
//根据指定ID删除记录
public void delete(Integer id){
Session session=null;
Transaction tx=null;
try{
session=getSession();
tx=session.beginTransanction();
//取得对应的ID的记录
Address persistentInstance=(Address)
session.get(Address.class,id)
//删除记录
session.delete(persistentInstance);
tx.commit();
}
catch(RuntimeException re){
//操作失败事务回滚
tx.rollback;
}
}
//更新记录
public void update(Address addr){
System.out.printIn("this is in"+this.getClass().getName());
Session session=null;
Transaction tx=null;
try{
session=getSession();//创建会话
tx=session.beginTranaction();//开始事务处理
Address ins=(Address)session.get(Address.class,addr.getId());//持久化类
ins.setUsername(addr.getUsername());
session.flush();//强制更新记录数据库
tx.commit();//提交事务
}catch(Exception e){
tx.rollback();//异常回滚
e.printStackTrace();
}finally{
session.close();//关闭会话
}
}
//按用户名查询
public ListfindByUsername(Address addr) {
Session session = null;
Transaction tx = null;
List list = null;
try {
session =getSession();
tx = session.beginTransaction();
//定义SQL语句
String queryString= "from Address as model where model."
+ USERNAME + "=?";
Query queryObject =session.createQuery(queryString);
queryObject.setString(0,addr.getUsername());
//取得所要取得的对应的记录
queryObject.setFirstResult(Pageinfo.startResult);
queryObject.setMaxResults(Pageinfo.pageSize);
//取得记录列表
list = queryObject.list();
tx.commit();
} catch(RuntimeException re) {
//事务回滚
tx.rollback();
throw re;
}finally{
session.close();
}
returnlist;
}
}
4.将持久化的描述文件添加到Hibernate的配置文件hibernate.cfg.xml中:
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库时使用的用户名 -->
<propertyname="connection.username">root</property>
<!-- 配置数据库连接的URL信息-->
<propertyname="connection.url">jdbc:mysql://127.0.0.1:3306/oa?useUnicode=true&characterEncoding=utf8</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 指定连接数据库时使用的密码 -->
<propertyname="connection.password">root</property>
<!-- 指定数据库驱动包-->
<propertyname="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 指定对应的持久化描述文件 -->
<mapping resource="peng/liang/oa/hibernate/beans/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2) Struts的实现
1. 建立对应的FormBean:
public class AddressForm extendsActionForm {
//建立JSP页面Form中对应字段的属性
privateString name;
//建立表单验证函数
publicActionErrors validate(ActionMapping mapping,
HttpServletRequestrequest) {
returnnull;
}
//创建对应的Getter和Setter方法
publicString getSex() {
returnsex;
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
}
2.建立对应FormBean的Action:
public class AddAddressAction extends Action {
public ActionForwardexecute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response) {
AddressFormaddressForm = (AddressForm)form;
Address address = new Address();
//调用对应的DAO层的方法对数据库进行操作
AddressDAOaddressDAO = new AddressDAO();
//取得当前用户的用户名
String username =(String)request.getSession().getAttribute(“username”);
address.setName(addressForm.getName());
//防止重复提交的代码(Struts的Token令牌)
if(this.isTokenValid(request,true)){//调用持久层的DAO将数据存入数据库
addressDAO.save(address);
addressForm.setName("");
addressForm.setSex("");
addressForm.setMobile("");
addressForm.setEmail("");
addressForm.setQq("");
addressForm.setAddress("");
addressForm.setCompany("");
} else {
}
//插入成功返回相应的ActionMapping对象,中转到相应的页面
returnmapping.findForward("success");
}
}