我正在整合hibernate和struts2。请告知哪个是最好的方法,我认为在Struts2中,没有官方插件来集成Hibernate框架。但是,您可以通过以下步骤解决问题:集成Struts 2和休眠的最佳方法
- 注册自定义ServletContextListener。
- 在ServletContextListener类中,初始化Hibernate会话并将其存储到servlet上下文中。
- 在action类中,从servlet上下文获取Hibernate会话,并像平常一样执行Hibernate任务。
请指出,我的servlet上下文的初始化休眠会话工作室的方法是好的,也可以有其他最好的方法。这是该项目的快照。
这里是一段代码..
模型类...
package com.mkyong.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
}
的HBM映射文件..
<hibernate-mapping>
<class name="com.mkyong.customer.model.Customer"
table="customer" catalog="mkyong">
<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-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/mkyong</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/mkyong/customer/hibernate/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
听众类...
package com.mkyong.listener;
import java.net.URL;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateListener implements ServletContextListener{
private Configuration config;
private SessionFactory factory;
private String path = "/hibernate.cfg.xml";
private static Class clazz = HibernateListener.class;
public static final String KEY_NAME = clazz.getName();
public void contextDestroyed(ServletContextEvent event) {
//
}
public void contextInitialized(ServletContextEvent event) {
try {
URL url = HibernateListener.class.getResource(path);
config = new Configuration().configure(url);
factory = config.buildSessionFactory();
//save the Hibernate session factory into serlvet context
event.getServletContext().setAttribute(KEY_NAME, factory);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
最后动作类..
ackage com.mkyong.customer.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.mkyong.customer.model.Customer;
import com.mkyong.listener.HibernateListener;
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>();
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{
//get hibernate session from the servlet context
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
Session session = sessionFactory.openSession();
//save it
customer.setCreatedDate(new Date());
session.beginTransaction();
session.save(customer);
session.getTransaction().commit();
//reload the customer list
customerList = null;
customerList = session.createQuery("from Customer").list();
return SUCCESS;
}
//list all customers
public String listCustomer() throws Exception{
//get hibernate session from the servlet context
SessionFactory sessionFactory =
(SessionFactory) ServletActionContext.getServletContext()
.getAttribute(HibernateListener.KEY_NAME);
Session session = sessionFactory.openSession();
customerList = session.createQuery("from Customer").list();
return SUCCESS;
}
}
家伙请发布更新后的代码非常感谢,我坚持了这条.. !!
请仅发布您的代码和配置的相关部分。这太多了。 – 2012-07-24 16:43:41
@Tichodroma做到了。 – user1538526 2012-07-24 16:47:29