Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2+Hibernate使用Full Hibernate Plugin集成
作者:--    发布时间:2019-11-20 10:18:13
在上篇 struts2 + hibernate集成 实例中, 它使用 servlet 上下文监听 hibernate 的 session,而且把struts2和hibernate框架集成。

但是,总有一些东西要提高。在本教程中,我们将展示如何整合struts2+hibernate,并使用struts2一个名为“full hibernate plugin“的插件。

见下面的集成步骤:
  1. 把 “full hibernate plugin” jar 放入到工程类路径。
  2. 使用 “@sessiontarget” 注释来注入到 hibernate session; 当“@transactiontarget” 注释注入到hibernate 事务。
  3. 在 struts.xml, 让包扩展 “hibernate-default“,而不是默认的堆栈。

看看下面的关系:

struts 2 <-- (full hibernate plugin) ---> hibernate <-----> database
注,
本教程是从以前的 struts2 + hibernate集成 实例(servlet context listener)更新版本。因此,jsp 和 hibernate 配置基本相同,只是整合的部分是有点不同,尝试比较既能发现不同。

1. 工程结构

在节教程,我们创建一个工程名为 full-hibernate 的web工程。看看这个项目文件夹的完整结构。

2. mysql创建表脚本

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;

3. hibernate相关配置

所有 hibernate 的模型和配置的东西。

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>

5. dao

实现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();
		
	}
	
}

6. action

在action类,调用dao类来执行数据库操作。

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;
	
	}
	
}

7. jsp 页面

jsp页面添加并列出客户。

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>

8. struts.xml

链接所有〜让包扩展 “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>

9. 实例

访问以下网址 : http://localhost:8080/full-hibernate/addcustomeraction.action

参考

  1. struts2 full hibernate插件文档
  2. struts2 + hibernate集成实例
  3. 安装库到maven本地资源库

下载代码

下载所有源代码 – http://pan.baidu.com/s/1o6tjsam

滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号