1, SSH three framework integration principle
The integration of spring and Struts2 is to give the Action object to the Spring container for creation.
The integration of spring and Hibernate is to give the sessionfactory to the spring container for maintenance, and the spring container is responsible for Session maintenance and related AOP transactions.
2. Spring Integrated Hibernate Framework
(1), create a new Web project, import the jar packages required by the Spring and Hibernate frameworks, as shown in:
(2), configure the Spring container separately, the specific configuration is as follows:
applicationContext.xml
- Create a configuration file and import constraints
<?xml version= "1.0" encoding= "UTF-8"?>
<BeansXmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
xmlns="Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context "
xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
xmlns:tx= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX"
Span class= "hljs-attr" >xsi:schemalocation= http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
Http://www.springframework.org/schema /context http://www.springframework.org/schema/context/spring-context-4.2.xsd
Http://www.springframework.org /SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/ Schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "
</< Span class= "Hljs-name" >BEANS>
web.xml
- Configure Spring to start with a project
<!--listeners that spring has created with Web launch--
<LISTENER>
<listener-class> Org.springframework.web.context.contextloaderlistener</ Listener-class>
</LISTENER>
<!--Configure Spring profile location parameters-->
< CONTEXT-PARAM>
<param-name> Contextconfiglocation</PARAM-NAME>
<param-value>classpath:applicationcontext.xml </PARAM-VALUE>
</CONTEXT-PARAM>
(3), configure Hibernate separately
hibernate.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<Hibernate-configuration>
<Session-factory>
<!--database-driven
<PropertyName="Hibernate.connection.driver_class" >com.mysql.jdbc.driver</Property>
<!--database URL--
<PropertyName="Hibernate.connection.url" >jdbc:mysql:///hbdb</Property>
<!--database connection user name--
<PropertyName="Hibernate.connection.username" >root</Property>
<!--database connection password--
<PropertyName="Hibernate.connection.password" >root</Property>
<!--database dialect
Note: When choosing a dialect, MySQL selects the shortest dialect.
-
<PropertyName="Hibernate.dialect" >org.hibernate.dialect.mysqldialect</Property>
<!--print hibernate-generated SQL statements to console--
<PropertyName="Hibernate.show_sql" >true</Property>
<!--formatting hibernate-generated SQL statements (syntax indentation)--
<PropertyName="Hibernate.format_sql" >true</Property>
<!--
Automatically export table structure. Auto-Build Table
-
<Propertyname= "Hibernate.hbm2ddl.auto" >update </PROPERTY>
<!-- Introduction of entity profile-->
<mapping resource=< Span class= "hljs-string" > "Com/spring/domain/*.hbm.xml"/>
< mapping resource= com/spring/domain/* . Hbm.xml "/>
<mapping resource=< Span class= "hljs-string" > "Com/spring/domain/*.hbm.xml"/>
</ SESSION-FACTORY>
</ Hibernate-configuration>
(4), Spring integration Hibernate
- Configuring Sessionfactory in the Spring container
<!--place Hibernate configuration information in the Spring configuration-
<BeanName="Sessionfactory"class="Org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<!--inject the connection pool into Sessionfactory, Hibernate gets connected through the connection pool-
<PropertyName="DataSource"ref="DataSource" ></Property>
<!--Configure Hibernate basic information-
<PropertyName="Hibernateproperties" >
<Props>
<!--required configuration--
<!--<prop key= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
<prop key= "Hibernate.connection.url" >jdbc:mysql:///crm_32</prop>
<prop key= "Hibernate.connection.username" >root</prop>
<prop key= "Hibernate.connection.password" >1234</prop>-
<Propkey="Hibernate.dialect" >org.hibernate.dialect.mysqldialect</Prop>
<!--optional configuration--
<Propkey="Hibernate.show_sql" >true</Prop>
<Propkey="Hibernate.format_sql" >true</PROP>
<prop key= "Hibernate.hbm2ddl.auto" >update </PROP>
</ PROPS>
</PROPERTY>
<!--introduces ORM metadata, specifies the package path where ORM metadata is located, and spring automatically reads all configuration--> in the package;
<< Span class= "Hljs-name" >property name= " Mappingdirectorylocations "value=" Classpath:com/spring/domain "></PROPERTY>
</BEAN>
(5), Spring integrated Hibernate environment Operation database
<!--action--
<!--Action object scope must be multiple---
<BeanName="*action"class="Com.spring.web.action.*action"Scope="Prototype" >
<PropertyName="*service"Te ="*service" ></Property>
</Bean>
<!--service--
<BeanName="*service"class="Com.spring.service.impl.*serviceimpl" >
<PropertyName="*demo"ref= "*dao" ></PROPERTY>
</BEAN>
<!--DAO-->
<bean name= "*Dao" class= "Com.spring.dao.impl.*daoimpl";
<!--injected sessionfactory-->
<property Span class= "hljs-attr" >name= "sessionfactory" ref=" sessionfactory "></PROPERTY>
</BEAN>
- Use hibernate templates for specific actions
Scan attention to the public number, learn more
The "SSH Framework" series of Spring integrated Hibernate framework