Wechat official account: CompassBlog

Welcome to follow, forward, learn from each other, common progress!

If you have any questions, please leave a message.

1. Integration principle of SSH three frameworks

  • Spring’s integration with Struts2 gives the Action object to the Spring container to create.

  • Spring’s integration with Hibernate gives SessionFactory maintenance to the Spring container, which takes care of Session maintenance and associated AOP transactions.

2. Spring integrates Hibernate framework

(1) Create a Web project and import the JAR packages required by Spring and Hibernate frameworks, as shown below:

(2) Separately configure the Spring container as follows:

applicationContext.xml

  • Create a configuration file and import constraints

<? The XML version = "1.0" encoding = "utf-8"? ><beans xmlns: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" 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 "> < / beans >Copy the code

web.xml

  • Configure Spring to start with the project

<! -- Let spring create a listener with web startup --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <! <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>Copy the code

(3) Configure Hibernate separately

  • Write entity classes and ORM metadata

  • Configure the master profile

hibernate.cfg.xml

<? The 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 Driver - > < property name = "hibernate. Connection. Driver_class" > com. Mysql.. JDBC Driver < / property > <! <property name="hibernate.connection.url"> JDBC :mysql:///hbDB</property> <! - the connection to the database user name - > < property name = "hibernate. Connection. The username" > root < / property > <! - password database connection - > < property name = "hibernate. Connection. Password" > root < / property > <! -- Database dialect note: MYSQL when choosing dialect, please select one of the shortest dialect. -- -- > < property name = "hibernate. The dialect" > org. Hibernate. The dialect. MySQLDialect < / property > <! <property name="hibernate.show_sql">true</property> <! <property name=" hibernate.format_SQL ">true</property> <! Export table structure automatically. <property name=" hibernate.hbm2dcl. auto">update</property> <! <mapping resource="com/spring/domain/*.hbm.xml" /> <mapping resource="com/spring/domain/*.hbm.xml" /> <mapping resource="com/spring/domain/*.hbm.xml" /> </session-factory></hibernate-configuration>Copy the code

(4) Spring integrates Hibernate

  • Configure the SessionFactory in the Spring container

<! <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <! <property name="dataSource" ></property> <! <property name="hibernateProperties"> <props> <! -- Mandatory 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> --> <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop> <! -- Optional configuration --> <prop key="hibernate.show_sql" >true</prop> <prop key="hibernate.format_sql" >true</prop> <prop key="hibernate.hbm2ddl.auto" >update</prop> </props> </property> <! - introduction of orm metadata, specify orm metadata package path, spring will automatically read all the configuration in the package - > < property name = "mappingDirectoryLocations" value="classpath:com/spring/domain" ></property> </bean>Copy the code

(5) Spring integrates Hibernate environment operation database

  • Create a Dao class that inherits HibernateDaoSupport

  • Configure the Action Service DAO in Spring

<! -- action --> <! <bean name="*Action" class="com.spring.web.action.*Action" scope="prototype" > <property name="*Service" ref="*Service" ></property> </bean> <! -- service --> <bean name="*Service" class="com.spring.service.impl.*ServiceImpl" > <property name="*demo" ref="*Dao" ></property> </bean> <! -- dao --> <bean name="*Dao" class="com.spring.dao.impl.*DaoImpl" > <! <property name="sessionFactory" ></property> </bean>Copy the code
  • Use hibernate templates for detailed operations

Recommended reading:

  • Transaction management for the Spring Framework family

  • Spring framework family JDBC integration

  • The Spring Framework family of AOP ideas

If you find this article useful, please share it with your friends!

This article can be reproduced arbitrarily, reproduced please indicate the source!

Click “Read the original article” to learn more about me!

Every step of growth, want to record and share

Click on follow to learn more