原创作者: zhenglinxi   阅读:1935次   评论:0条   更新时间:2011-06-01    
我用的是jbuilder9调试的

1、建立一个Class,HibernatePlugIn 放在src下
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class HibernatePlugIn implements PlugIn {
    public void destroy(); {
    }

    public void init(ActionServlet servlet, ModuleConfig config); throws ServletException {
      try{
        ServletContext context = null;
        context = servlet.getServletContext();;
        SessionFactory sf = new Configuration();.configure();.
            buildSessionFactory();;
        context.setAttribute("net.sf.hibernate.SessionFactory", sf);;
      }
      catch(Exception e);{
        e.printStackTrace();;
      }
    }
}


2、在struts-config.xml中添加
<plug-in className="HibernatePlugIn">
  <set-property property="configFilePath" value="/hibernate.cfg.xml" />
  <set-property property="storeInServletContext" value="true" />
</plug-in>


3、将hibernate.cfx.xml放在src目录下,并且属性设为copy
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="show_sql">false</property>
        <property name="use_outer_join">true</property>
        <property name="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.100.242:1521:ecm</property>
        <property name="hibernate.connection.username">mydb_user</property>
        <property name="hibernate.connection.password">mydb_pass</property>
        <property name="hibernate.connection.pool.size">20</property>
	<property name="session_factory_name">hibernate/session_factory</property>
        <!-- Mapping files -->
        <mapping resource="Content_folder.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


这样以后就可以在任何地方调用了
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import javax.naming.InitialContext;
import javax.naming.Context;


public class MyService {
  private SessionFactory sf;

  public LoginService(); throws Exception {
    Context ctx = new InitialContext();;
    sf = (SessionFactory); ctx.lookup("hibernate/session_factory");;
  }
}


我经验少,是个hibernate的菜鸟,有不对的地方,大家帮忙指出来!
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • letle在2003-09-18创建
  • letle在2011-06-01更新
Global site tag (gtag.js) - Google Analytics