I have seen a lot of articles about the integration of Spring and Hibernate on the Internet, but because those articles were written earlier, many of them were older versions such as Spring 3 and Hibernate 4. So I'll use the updated version here to illustrate it.
Add project dependencies
First of all, we need a Java Web project, which is best to use Maven or Gradle build tools to facilitate our software dependencies. I'm using Gradle build tool here, the build script is as follows. As long as we introduce the two packages of spring-webmvc and spring-orm, other Spring dependencies will be automatically solved by the build tool. Then you also need to introduce dependencies such as data sources, Hibernate, JSTL, etc. The script defines a task to generate the corresponding pom file for easy use by Maven tools.
group 'yitian.learn'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'war'apply plugin: 'maven'apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'sourceCompatibility = 1.8repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } jcenter()}ext {springVersion = '4.3.6.RELEASE' aspectjVerison = '1.8.10'} dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion compile group: 'org.springframework', name: 'spring-orm', version: springVersion compile group: 'org.glassfish.web', name: 'jstl-impl', version: '1.2' compile group: 'org.projectlombok', name: 'lombok', version: '1.16.12' compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.6.Final' compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.40' compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1' compile group: 'org.aspectj', name: 'aspectjweaver', version: aspectjVerison}task writeNewPom { doLast { pom { }.writeTo("$projectDir/pom.xml")}}Configure web.xml
Then open the WEB-INF/web.xml file and add the following content.
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
Configure Spring
There should be two Spring configuration files /WEB-INF/applicationContext.xml and /WEB-INF/dispatcher-servlet.xml. The former is a root configuration file, which is used to configure back-end and global components such as databases, and the latter is an MVC configuration file, which is used to configure MVC and Web-related components.
Then in /WEB-INF/applicationContext.xml, we configure the components that integrate Hibernate and Spring. We need to configure the data source, HibernateSessionFactory, Hibernate transaction manager, transaction connection point, Hibernate template and other beans, and then use the Hibernate template when manipulating data to obtain the transaction management function controlled by Spring.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <!--Data Source--> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="12345678"/> </bean> <!--hibernate--> <bean id="sessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> <property name="packagesToScan" value="yitian.learn.entity"/> </bean> <!--Set hibernate template--> <bean id="hibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--Set hibernate transaction manager--> <bean id="transactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--Set hibernate transaction manager--> <bean id="transactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--Data access object--> <bean id="userDao"/> <!--Set transaction management--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!--Set transaction management with AOP--> <aop:config> <aop:pointcut id="userDaoPointcut" expression="execution(* yitian.learn.dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="userDaoPointcut"/> </aop:config></beans>
Then configure the components of Spring Web MVC. Add the following configuration in dispatcher-servlet.xml. Here we add JSP view parser and type converter. If you do not need to custom type conversion, you can delete the corresponding fragment.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:view-resolvers> <mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp" view-class="org.springframework.web.servlet.view.JstlView"/> </mvc:view-resolvers> <mvc:default-servlet-handler/> <mvc:annotation-driven conversion-service="conversionService"/> <context:component-scan base-package="yitian.learn"/> <bean id="conversionService"> <property name="converters"> <set> <bean/> </set> </property> </bean></beans>
At this point, the integration between Hibernate and Spring has been fully configured. Finally, I wrote a small example and put it on Github. Students who are interested can take a look.
Summarize
The above is all the detailed explanation of the integration configuration of Spring Web MVC and Hibernate in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!