Integrate Struts with hibernate

Source: Internet
Author: User
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 ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.