1. Create a class and put hibernateplugin under 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. Add in struts-config.xml
3. Place hibernate. CFX. XML in the src directory and set the property to copy.
Http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
False True Net. SF. hibernate. dialect. oracledialect Oracle. JDBC. Driver. oracledriver JDBC: oracle: thin: @ 192.168.100.242: 1521: ECM Mydb_user Mydb_pass 20 Hibernate/session_factory
In this way, you can call it anywhere.
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 ");
}
}