但是,总有一些东西要提高。在本教程中,我们将展示如何整合struts2+hibernate,并使用struts2一个名为“full hibernate plugin“的插件。
看看下面的关系:
struts 2 <-- (full hibernate plugin) ---> hibernate <-----> database
customer表结构
create table `customer` ( `customer_id` bigint(20) unsigned not null auto_increment, `name` varchar(45) not null, `address` varchar(255) not null, `create_date` datetime not null, primary key (`customer_id`) ) engine=innodb auto_increment=2 default charset=utf8;
customer.java – 为customer 表创建一个类。
package com.h3.customer.model;
import java.util.date;
public class customer implements java.io.serializable {
private long customerid;
private string name;
private string address;
private date createddate;
//getter and setter methods
}
customer.hbm.xml – hibernate 的 customer 表映射。
<?xml version="1.0"?>
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- generated 20 julai 2010 11:40:18 am by hibernate tools 3.2.5.beta -->
<hibernate-mapping>
<class name="com.h3.customer.model.customer"
table="customer" catalog="h3">
<id name="customerid" type="java.lang.long">
<column name="customer_id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="45" not-null="true" />
</property>
<property name="address" type="string">
<column name="address" not-null="true" />
</property>
<property name="createddate" type="timestamp">
<column name="created_date" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>
文件: hibernate.cfg.xml, hibernate 数据库配置
<?xml version="1.0" encoding="utf-8"?>
<!doctype hibernate-configuration public
"-//hibernate/hibernate configuration dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/h3</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">false</property>
<mapping resource="com/h3/customer/hibernate/customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
实现dao设计模式执行数据库操作。在 customerdaoimpl 类, 声明hibernate会话和事务为类成员。在struts 2的项目初始化, “full hibernate plugin” 使用 @sessiontarget 和 @transactiontarget 分别标注将注入相应的 hibernate 会话和事务成为类成员。
customerdao.java
package com.h3.customer.dao;
import java.util.list;
import com.h3.customer.model.customer;
public interface customerdao{
void addcustomer(customer customer);
list<customer> listcustomer();
}
customerdaoimpl.java
package com.h3.customer.dao.impl;
import java.util.list;
import org.hibernate.session;
import org.hibernate.transaction;
import com.googlecode.s2hibernate.struts2.plugin.annotations.sessiontarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.transactiontarget;
import com.h3.customer.dao.customerdao;
import com.h3.customer.model.customer;
public class customerdaoimpl implements customerdao{
@sessiontarget
session session;
@transactiontarget
transaction transaction;
//add the customer
public void addcustomer(customer customer){
session.save(customer);
}
//return all the customers in list
public list<customer> listcustomer(){
return session.createquery("from customer").list();
}
}
customeraction.java
package com.h3.customer.action;
import java.util.arraylist;
import java.util.date;
import java.util.list;
import com.h3.customer.dao.customerdao;
import com.h3.customer.dao.impl.customerdaoimpl;
import com.h3.customer.model.customer;
import com.opensymphony.xwork2.actionsupport;
import com.opensymphony.xwork2.modeldriven;
public class customeraction extends actionsupport
implements modeldriven{
customer customer = new customer();
list<customer> customerlist = new arraylist<customer>();
customerdao customerdao = new customerdaoimpl();
public string execute() throws exception {
return success;
}
public object getmodel() {
return customer;
}
public list<customer> getcustomerlist() {
return customerlist;
}
public void setcustomerlist(list<customer> customerlist) {
this.customerlist = customerlist;
}
//save customer
public string addcustomer() throws exception{
//save it
customer.setcreateddate(new date());
customerdao.addcustomer(customer);
//reload the customer list
customerlist = null;
customerlist = customerdao.listcustomer();
return success;
}
//list all customers
public string listcustomer() throws exception{
customerlist = customerdao.listcustomer();
return success;
}
}
customer.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>struts 2 full hibernate plugin example</h1> <h2>add customer</h2> <s:form action="addcustomeraction" > <s:textfield name="name" label="name" value="" /> <s:textarea name="address" label="address" value="" cols="50" rows="5" /> <s:submit /> </s:form> <h2>all customers</h2> <s:if test="customerlist.size() > 0"> <table border="1px" cellpadding="8px"> <tr> <th>customer id</th> <th>name</th> <th>address</th> <th>created date</th> </tr> <s:iterator value="customerlist" status="userstatus"> <tr> <td><s:property value="customerid" /></td> <td><s:property value="name" /></td> <td><s:property value="address" /></td> <td><s:date name="createddate" format="dd/mm/yyyy" /></td> </tr> </s:iterator> </table> </s:if> <br/> <br/> </body> </html>
链接所有〜让包扩展 “hibernate-default” 来代替 “struts-default“.
<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public
"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devmode" value="true" />
<package name="default" namespace="/" extends="hibernate-default">
<action name="addcustomeraction"
class="com.h3.customer.action.customeraction" method="addcustomer" >
<result name="success">pages/customer.jsp</result>
</action>
<action name="listcustomeraction"
class="com.h3.customer.action.customeraction" method="listcustomer" >
<result name="success">pages/customer.jsp</result>
</action>
</package>
</struts>
访问以下网址 : http://localhost:8080/full-hibernate/addcustomeraction.action


