Struts + Hibernate example (original)

xiaoxiao2021-03-06  67

hibernate struts combination of simple examples: Umbrellas (geezer) 2004/11/12 introduced the basics hibernate, and now we use it in the actual project development, with struts and we do it in conjunction with a simple application. I didn't put it in the LIB file you need, because it is too big (nearly 10m after compression) The forum will not be uploaded, huh, huh, you put the struts with Hibernate's JAR files. Configuration environment: Hibernate 2.1 (last time I met a friend for half a day because the version was different, this payment) Struts 1.1JBuilder XMysql 4.1.7 In fact, Hibernate is combined with Struts is very good choice, and uses and configured Simple, the step is completed. Example There are 5 Java class files (beanitem.java, h_bean.java, h_connection.java, init.java, h_connection.java, init.java, mainservlet.java), beanitem.java is a bean that created the corresponding total record after the result is taken out from the database. The beanitem array, then press the Request object, and finally use the RequestDispatcher (the FindForward method with the actionMapping object can be used, as long as the Struts-Config adds the tag) to the JSP page (Ok.jsp). h_bean.java is a class that Hibernate and database table mapping, because the last time I have already introduced the basic knowledge of Hibernate, so I don't explain here. h_connection.java is a class that manages hibernate. Take the sessionFactory with a getConnection method, note that there is a Static block inside, and query the JNDI tree get the SESSIONFACTORY instance of the binding. Init.java is an extension class of Struts, inherits from Plugin, add this class to Struts configuration file ( in struts-config.xml This line can be only), and every time you start the server, you will perform this class first. Of course, only once, because Hibernate takes the sessionFactory, it takes a little time, so do not regenerate sessionFactory every time you use. We change it into Plugin, then tied to the directory tree so that it can be directly LOOKUP when you need it elsewhere. There is init with the destroy method, which means that it can be automatically deleted in the Destroy method when the container is stopped. (It seems a bit of a little more than a little) MainServlet.java is the Action subclass of Struts. The operations in the example are completed here, including sessionFactory, data, steering, and more. In order to use Struts, it is necessary to add a hibernate.cfg.xml file (there are several ways, here only this is common), put it in the classes directory, pay attention to The mapping of each table must be added to this file, and the new line can be added. Example download: http://www.phpx.com/happy/attachment.php? S = & postid = 656452

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

New Post(0)