Spring MVC Integration Hibernate step

Source: Internet
Author: User

The process is documented today by integrating Hibernate into the SPRINGMVC framework from the beginning.

1. First, hibernate support is added to the listener configuration file as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:util= "Http://www.springframework.org/schema/util"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.1.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-4.1.xsd ">    <!--Configure the data source -    <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>        < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/tobepro"></ Property>        < Propertyname= "username"value= "root"></ Property>        < Propertyname= "Password"value= "root"></ Property>    </Bean>    <!--Configure Sessionfactory -    <BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <!--the data source points to the top datasource -        < Propertyname= "DataSource"ref= "DataSource" />        <!--Configure the package scan so that hibernate automatically searches for the @entity entity class file underneath the specified package at startup -        < Propertyname= "Packagestoscan"value= "Com.test.maven.model"/>        <!--Hibernate Property Configuration -        < Propertyname= "Hibernateproperties">            <Props>                <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</prop>                <propKey= "Hibernate.hbm2ddl.auto">Update</prop>                <propKey= "Hibernate.show_sql">True</prop>                <propKey= "Hibernate.format_sql">True</prop>            </Props>        </ Property>    </Bean>    <!--Configure the things manager -    <BeanID= "Transactionmmanager"class= "Org.springframework.orm.hibernate4.HibernateTransactionManager">        <!--Configure Sessionfactory for the top -        < Propertyname= "Sessionfactory"ref= "Sessionfactory" />    </Bean>    <!--configure transactions, provided by proxy (abstract may be configured as an interface and then add interfaces to other beans for specific pojo to implement) -    <BeanID= "Transactionproxy"class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean"Abstract= "true">        < Propertyname= "TransactionManager"ref= "Transactionmmanager" />        < Propertyname= "Transactionattributes">            <Props>                <propKey= "add*">Propagation_required,-exception</prop>                <propKey= "update*">Propagation_required,-exception</prop>                <propKey= "insert*">Propagation_required,-exception</prop>                <propKey= "modify*">Propagation_required,-exception</prop>                <propKey= "delete*">Propagation_required,-exception</prop>                <propKey= "get*">Propagation_never</prop>                <propKey="*">Propagation_required</prop>            </Props>        </ Property>    </Bean></Beans>

Configuration file, the spring data source is configured first, and then the session factory of Hibernate is established (I use the Pojo class file of hibernate annotations, so the Packagestoscan method is used, If using the HBM configuration file method, no need to add this configuration, but need to increase the Hibernate.cfg.xml configuration file configuration), set up a thing manager, manage the session factory, and then configure the agent. The specific configuration usage is the reason why do not explain, please own Baidu.

In the final proxy configuration, only the agent interface is configured, and the specific agent needs to be implemented by the actual proxy, as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.1.xsd ">    <!--configuring member assignments in a class in a DAO package -    <BeanID= "Wxuserdao"class= "Com.test.maven.dao.impl.WxUserDaoImpl">        < Propertyname= "Sessionfactory"ref= "Sessionfactory"/>    </Bean>        <!--Configuring member Assignments in class in a service pack -    <BeanID= "Wxuserservicereal"class= "Com.test.maven.service.impl.WxUserServiceImpl">        < Propertyname= "Wxuserdao"ref= "Wxuserdao"/>    </Bean>        <!--Configure the transaction agent (bring the service agent over) -    <BeanID= "Wxuserservice"Parent= "Transactionproxy">        < Propertyname= "Target"ref= "Wxuserservicereal"/>    </Bean></Beans>

This file first initializes the necessary members of the DAO Layer and service layer, and then maps the members to the proxy.

Hibernate annotation method for configuring entity class files, refer to Hibernate annotation parsing

Spring MVC Integration Hibernate step

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.