Integration of the SSH framework (Struts2 + Spring + Hibernate)

Source: Internet
Author: User
Tags aop ssh

(i) Integration of spring and hibernate frameworks

key points of integration:

(1) The data source is configured in the spring configuration file for use by the spring and hibernate frameworks;

(2) The Hibernate.hbm.xml configuration file is no longer needed, and the hibernate run-time parameters and mapped XML file resources in the corresponding entity classes are downloaded in the spring configuration file;

(3) Application and Hibernate interactions are done primarily through sessionfactory, and the spring Framework provides Localsessionfactorybean classes, Help create Sessionfactory instances in the Hibernate framework, inject the Sessionfactory factory into the DAO (Database access Layer), through Sessionfactory factory getcurrentsession () method to get the session instance and perform the database operation;

(4) Spring is responsible for managing transactions, and the transaction manager uses the Hibernatetransactionmanager class, which is responsible for binding session objects and threads, as well as starting transaction operations, andCurrent_ Session_context_class need to use org.springframework.orm.hibernate4.SpringSessionContext class;

Note : The Springsessioncontext class not only gets the session object from the current thread (it is actually the database connection object connection), and then actively opens the transaction (The hibernate frame itself is a Run-time exception)

(ii) Steps for integration of the SSH framework:

(1) Copy jar package:

The jar package used by the STRUTS2 framework, the jar package used by the spring framework, the jar package used by the Hibernate framework, the database-driven jar package;

Org.springframework.orm.jar,struts2-spring-plugin-2.3.16.3.jar

(2) Configure STRUTS2 filters and spring listeners

  <!--Configure STRUTS2 filter-->
  <filter>
  	<filter-name>struts2</filter-name>
  	< Filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
  </filter>
  <!--all requests for filtering clients-->
  <filter-mapping>
  	<filter-name>struts2< /filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- Configure the Spring listener-->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>
  </listener>



(3) Configure the data sources used by the spring and hibernate frameworks (the C3P0 data source that I use, and the driver packages for the Oracle database)

	<!--Configure Data source-->
	<bean id= "ds" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" >
		<property name= "Jdbcurl" value= "Jdbc:oracle:thin: @localhost: 1521/xe" ></property>
		< Property Name= "Driverclass" value= "Oracle.jdbc.OracleDriver" ></property>
		<property "User" value= "ssh" ></property>
		<property name= "password" value= "ssh" ></property>
		< Property Name= "Maxpoolsize" value= "ten" ></property>
		<property name= "Minpoolsize" value= "5" ></ property>
		<property name= "MaxIdleTime" value= "></property> </bean>"
	

(4) Configure the Localsessionfactorybean class in Applicationcontext.xml in the core configuration file in spring;

You need to set the following properties:

DataSource DataSource; Data source

String[] Mappingresource; Resource path for the persisted class mapping file

Properties hibernateproperties; Hibernate Framework Operating parameters

<!--configuration Sessionfactory factory--> <bean id= "sessionfactory" class= " Org.springframework.orm.hibernate4.LocalSessionFactoryBean > <!--Configure the data source used by the Sessionfactory factory--> < Property Name= "DataSource" ref= "ds" ></property> <!--Configure the run parameters used by the Hibernate framework (dialect, automatic generation of SQL statements, formatting of SQL statements)-- > <property name= "hibernateproperties" > <props> <prop key= "Hibernate.dialect" >org.hibernate. Dialect. oracle10gdialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.forma T_sql ">true</prop> <!--Configure the Springsessioncontext class to implement the previous thread functionality, but also provides another feature that, when the current thread binds the session, actively opens a transaction --> <prop key= "Hibernate.current_session_context_class" > Org.springframework.orm.hibernate4.springsessioncontext</prop> <!--Configure the Hibernate framework to automatically create tables when it is started (the table is updated , no on creation)--> <prop key= "Hibernate.hbm2ddl.auto" >update</prop> </props> </property> &L t;! --Configure the corresponding mapping file for the persistence class--&Gt <!--the first configuration--> <!--<property name= "mappingresources" > <array> <value>com/xupt/ssh/ent ity/typeentity.hbm.xml</value> </array> </property>--> <!--The second configuration of--> <property NA Me= "Mappinglocations" > <array> <!--use wildcard characters to add mapping files for entity classes--> <value>classpath:com/xupt/ssh/ entity/*.hbm.xml</value> </array> </property> </bean>



(5) Inject sessionfactory into the DAO in the spring configuration file, use the getcurrentsession () method to get the session object, perform the database operation;

Note : The transaction manager Spring provides is responsible for binding the session object to the current thread and starting the transaction, so you must configure the Spring transaction manager;

<!--configuration transaction manager--> <!--Hibernatetransactionmanager is getting a session from the Sessionfactory factory, which gets the database connection object from the data source. Open transaction--> <bean id= "Mgr" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > <!-- Get Session object from Sessionfactory, open transaction management--> <property name= "sessionfactory" ref= "Sessionfactory" ></property > </bean> <!--Configure Transaction enhancements (after AOP configuration, transaction automatically commits and rolls back transactions)--> <tx:advice id= "Txadvice" transaction- manager= "Mgr" > <tx:attributes> <!--Configure transaction properties--> <tx:method name= "*get" read-only= "true"/> < Tx:method name= "*find" read-only= "true"/> <tx:method "name=" *search "true" "read-only=/> <tx:method" * "propagation=" REQUIRED "/> </tx:attributes> </tx:advice> <!--AOP configuration (in which the methods in the specific classes are controlled for transactions)--> &L T;aop:config> <!--define pointcuts (needs to be woven into an enhanced series of connection points)--> <aop:pointcut expression= "Execution (Public * com.xupt.ssh.serv Ice.. *.*(..))" Id= "PC1"/> <!--definition Enhancements--> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "PC1"/> </aop:config> 

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.