Struts and Hibernate integration (Hibernate as a struts plugin)

xiaoxiao2021-03-05  22

Put all the bags used by Hibernate into the lib application's lib, then define hibernate.cfg.xml, which is the configuration information of the plugin, as follows

false true net.sf.hibernate.dialect.MySQLDialect org.gjt.mm.mysql.Driver jdbc: mysql: /// Test root 20 hibernate / session_factory

The information is clear, it is to configure the database and other information, we use JNDI to find sessionFactory, below is the plug-in, HibernatePlugin.java

import org.apache.struts.action.PlugIn; import org.apache.struts.action.ActionServlet; import org.apache.struts.config.ModuleConfig; import javax.servlet.ServletException; import javax.servlet.ServletContext; import net. sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; public class HibernatePlugIn implements PlugIn {public void destroy () {} public void init (ActionServlet servlet, ModuleConfig config) throws ServletException {try {ServletContext context = servlet. getServletContext ();.. SessionFactory sf = new Configuration () configure () buildSessionFactory (); context.setAttribute ( "net.sf.hibernate.SessionFactory", sf);} catch (Exception ex) {ex.printStackTrace (); }}} The corresponding, to configure the plugin information in struts-config.xml, approximately the path

You can use java.naming.context, java.naming.initiacontext, java.naming.initiacontext.

Context ct = new initialContext (); sessions = (sessionFactory) ct.lookup ("Hibernate / session_factory); session = sessions.opensesis ();

Of course, there are other two kinds of integration, I think this method is more flexible, it is more likely to master

转载请注明原文地址:https://www.9cbs.com/read-38578.html

New Post(0)