HIBERNATE4+SPRING3+STRUTS2 Example of building frame

Source: Internet
Author: User

1. The Required jar package

2.web.xml configuration file, this is the same as the usual configuration

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <!--Spring--<context-param> <param-name>  Springconfiglocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!--Contentloader--><listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener> <!--struts-- > <filter> <filter-name>struts</filter-name> <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> < Filter> <filter-name>struts-cleanup</filter-name> <filter-class> org.apache.struts2.dispatcher.actioncontextcleanup</Filter-class> </filter> <filter-mapping><filter-name>struts</filter-name>< Url-pattern>*.do</url-pattern><dispatcher>request</dispatcher><dispatcher>forward </dispatcher></filter-mapping><filter-mapping><filter-name>struts-cleanup</ Filter-name><url-pattern>/*</url-pattern></filter-mapping> <welcome-file-list> < Welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

3.applicationcontex.xml file configuration, Sessionfactory's configuration class is: Org.springframework.orm.hibernate4.LocalSessionFactoryBean

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP /spring-aop-2.5.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sprin G-tx-2.5.xsd "><!--onload hibernate.properties file--><bean id=" Propertyconfig "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "><property name=" locations"> <value>/WEB-INF/jdbc.properties</value> </property></bean> <!--c3p0 Connection pool--><bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= " Close "><property name=" Driverclass "><value>${hibernate.connection.driver_class}</value> </property><property name= "Jdbcurl" ><value>${hibernate.connection.url}</value></ Property><property name= "User" > <value>${hibernate.connection.username}</value></ Property><property name= "Password" ><value>${hibernate.connection.password}</value></ property><!--The minimum number of connections that are kept in the connection pool. --><property name= "Minpoolsize" ><value>5</value></property><!--the maximum number of connections that are kept in the connection pool. Default:15--><property name= "maxpoolsize" > <value>100</value></property><!-- The number of connections obtained at initialization, and the value should be between Minpoolsize and Maxpoolsize. Default:3--><property name= "InitialpoolSize > <value>5</value></property><!--Maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. default:0--><property name= "MaxIdleTime" > <value>60</value></property><!-- When the connection in the connection pool runs out, c3p0 the number of connections that are obtained at the same time. Default:3--><property name= "Acquireincrement" > <value>3</value></property></bean ><!--Create Sessionfactory--><bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource " ></property><!--config hibernate Field--><property name= "Hibernateproperties" ><props> <prop key= "Hibernate.dialect" > ${hibernate.dialect}</prop><prop key= "hibernate.generate_statistics ">true</prop><prop key=" Hibernate.jdbc.fetch_size "> ${hibernate.jdbc.fetch_size}</prop>< Prop key= "Hibernate.jdbc.batch_size" > ${hibernate.jdbc.batch_size}</prop> <prop key= "hibernate. Current_sessioN_context_class ">thread</prop> <prop key=" Hibernate.bytecode.use_reflection_optimizer ">false </prop> <prop key= "hibernate.query.substitutions" >true</prop> <prop key = "Hibernate.show_sql" >${hibernate.show_sql}</prop> <prop key= "Hibernate.max_fetch_depth" &GT;3&L t;/prop> <prop key= "Hibernate.hibernate.use_outer_join" >true</prop> <pro P key= "Hibernate.jdbc.batch_size" >50</prop> </props></property><!--mapping Hibernate mode L PO class--><property name= "Mappingdirectorylocations" ><list><value>/web-inf/classes/com/ fit/core/pojo</value></list></property></bean><!--Config Transaction manager--> <bean id= "TransactionManager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >< Property Name= "Sessionfactory" ref= "Sessionfactory" ></property></bean> <bean id= "Basedao" class= "Com.fit.core.dao.impl.BaseDaoImpl" ><property name= " Sessionfactory "ref=" sessionfactory "></property></bean><bean id=" Basebean "class=" Com.fit.core.bean.impl.BaseBeanImpl "><property name=" Basedao "ref=" Basedao "></property></bean ></beans>

4.jdbc.properties configuration file

  

5.struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public    "-//apache software foundation//dtd struts Configuration 2.0//en"    "/http Struts.apache.org/dtds/struts-2.0.dtd "><struts>  <package name=" Default "extends=" Struts-default " ><action name= "finduserlist" class = "com.fit.core.action.UserAction" method= "finduserlist" ><result name = "Success" >/WEB-INF/jsp/list.jsp</result></action></package></struts>

6.daoimp.java

Package Com.fit.core.dao.impl;import Java.util.list;import Org.hibernate.query;import org.hibernate.Session;import Org.hibernate.sessionfactory;import Com.fit.core.dao.basedao;public class Basedaoimpl implements BaseDao {private Sessionfactory sessionfactory;public sessionfactory getsessionfactory () {return sessionfactory;} public void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = sessionfactory;} public boolean DeleteObject (Object object) {Boolean flag = false;try{session Session = sessionfactory.opensession (); Session.delete (object); flag = true;} catch (Exception e) {e.printstacktrace ();} return flag;} Public Object Findobject (Class c, int id) {Boolean flag = false;try{session session = Sessionfactory.opensession (); session . get (c, id); flag = true;} catch (Exception e) {e.printstacktrace ();} return flag;} Public List listquery (String hql) {//TODO auto-generated method Stublist List = null; try{session Session = Sessionfactor Y.opensession (); list = Session.createquery (hql). List ();} catch (Exception e) {e.printstacktrace ();} return list;} Public boolean Save (Object obj) {Boolean flag = false;try{session Session = sessionfactory.opensession (); Session.save ( obj); flag = true;} catch (Exception e) {e.printstacktrace ();} return flag;} public boolean updateobject (Object obj) {Boolean flag = false;try{session session = Sessionfactory.opensession (); session . Update (obj); flag = true;} catch (Exception e) {e.printstacktrace ();} return flag;} public boolean updateobject (String hql) {Boolean flag = false; Session session = Sessionfactory.opensession (); Query query = session.createquery (HQL), if (query! = NULL) {int a = Query.executeupdate (), if (a > 0) {flag = True;session.ge Ttransaction (). commit ();//Submit things}}return flag;}}

  

  

HIBERNATE4+SPRING3+STRUTS2 Example of building frame

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.