Recently engaged in a Web project, used to do projects in the company is JPA to do the ORM persistence layer, this project is my own answer, I decided to change, with Hibernate3 to do ORM persistence layer, so I searched the internet Hibernate3 How to configure integration to Spring3 MVC, found strange, and many are not based on annotation mode configuration, obviously those words above the configuration has been with today's Hibernate3 annotation support way out of line, so I decided to make a, first of all, the network of those configuration ways of bad places, Many articles refer to the Jdbc.properties file with the Hibernate config file, which is now based on annotations and is completely unnecessary. I'll summarize myself by integrating annotation-supported Hibernate3 in an already existing SPRING3 MVC project, and before doing it, let's start by saying my entire configuration environment:
Web server: TOMCAT7
Development environment: ECLIPSEEE
Spring3 version: 3.2.5 release
JDK version: JDK8
Database: MySQL 5.x
Then perform the following five steps:
First step: Add package dependency support in Maven
Add Hibernate package dependencies to the project's Pom.xml file, and add the MySQL JDBC package dependencies to the Apache-common DBCP package dependency, and add spring-orm and Spring-context package dependencies, The added pox.xml dependencies are as follows:
<dependency><groupid>org.hibernate</groupid><artifactid>hibernate-core</artifactid ><version>3.6.10.final</version></dependency><dependency><groupid>mysql</ groupid><artifactid>mysql-connector-java</artifactid><version>5.1.37</version></ dependency><dependency><groupid>commons-dbcp</groupid><artifactid>commons-dbcp</ Artifactid><version>1.4</version></dependency>
spring-orm Dependency Additions:
<dependency><groupid>org.springframework</groupid><artifactid>spring-orm</ Artifactid><version>${spring.version}</version></dependency><dependency><groupid >org.springframework</groupid><artifactid>spring-context</artifactid><version>${ Spring.version}</version></dependency>
Step Two: Create an XML file
Name yourself, but the file must be located under the Web-inf directory. My practice is to create a subdirectory under Web-inf. Spring then creates an XML file Data-context.xml
Step three: Edit the Web. xml file
Add the following configuration to the Web. xml file:
<context-param><param-name>contextconfiglocation</param-name><param-value>/web-inf/ Spring/data-context.xml</param-value></context-param><listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener>
Fourth Step: Create the application database in MySQL,
In the case of MySQL booting, create a new database Yourdb after connecting to MySQL via Heidisql, and create a package as your entity class in the application, create a test entity class you can choose the class name yourself, This step can be skipped if you have already created the appropriate database.
Fifth step: Add Hibernate related Configuration
Here are mainly divided into several parts, one is to support the annotation configuration, the second is to support the service and DAO layer package scan, three is to configure the data source DataSource, four is to configure the connection management Sessionfactory, five is configured to support transaction management, the complete configuration file is as follows:
<?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:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd "><context:annotation-config/><!--component Scan--><context:component-scan Base-package= "Com.clotherdye.dao.impl"/><context:component-scan base-package= "Com.clotherdye.service.impl "/><!--Add a data source--><bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "jdbc:mysql:// Localhost:3306/clother "/><property name=" username "value=" root "/><property name=" password "value=" Password123 "/><property name=" InitialSize "value=" 5 "/> <property name=" maxactive "value=" ten "/> < Property Name= "Testonborrow" value= "true"/><property name= "validationquery" value= "SELECT 1"/></bean ><!--Configuring session factory, connecting--><bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "><property name=" DataSource " ref= "DataSource" ></property><property name= "hibernateproperties" > <props> <p ROP key= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</prop> <prop key= "Hibernate.show_ SQL "≫true</prop> <prop key= "Hibernate.format_sql" >true</prop> <prop key= "Hiber Nate.hbm2ddl.auto ">update</prop> </props> </property> <property name=" Packagestosca N "value=" Com.clother.data.model "/></bean><!--support transaction management--><tx:annotation-driven/> <bean id=" TransactionManager "class=" Org.springframework.orm.hibernate3.HibernateTransactionManager "> <property Name= "Sessionfactory" ref= "Sessionfactory" ></property> </bean></beans>
Then save all the changes, MAVEN compiles the package, and it's ready to be used directly.
It is particularly important to note that the session factory is configured with Annotationsessionfactorybean as the session factory. It also supports the automatic creation of tables, which is particularly convenient during project development, and can be changed to validate after the project is finished.
Please note that it is reproduced in the "gloomyfish-jia20003"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
In Spring3 MVC, the five-step configuration integration Annotation Method Hibernate3