Spring Integrated Hibernate

Source: Internet
Author: User

Integrating Hibernate into Spring is actually the use of hibernate in
Data source DataSource, sessionfactory instances (typically when you use Hibernate to access a database, the application first creates an Sessionfactory instance), and the transaction manager are managed by the spring container.
When consolidating, you can use only the spring configuration file (usually the Applicationcontext.xml file or other naming) to complete the two framework initialization tasks and no longer use the Hibernate.cfg.xml configuration file.

Defining a Data source DataSource

<BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>        < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/yhssh?useunicode=true&amp;characterencoding=utf-8">        </ Property>        < Propertyname= "username"value= "root"></ Property>        < Propertyname= "Password"value=""></ Property></Bean>

Managing Sessionfactory Instances declaratively

Use spring's Localsessionfactorybean to configure Hibernate's Sessionfactory,
A reference to the data source that is injected by the spring IOC container when the Sessionfactory is configured.

<BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean">        < Propertyname= "DataSource"ref= "DataSource"/>                       < Propertyname= "Mappingdirectorylocations">            <List>                <value>Classpath:com/yh/xml</value>            </List>        </ Property>                    <!--Set Some attributes about Hibernate -        < Propertyname= "Hibernateproperties">            <Props>                               <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</prop>                              <!--sets whether hibernate outputs SQL statements in the console, usually set to true during the development debugging phase -                <propKey= "Hibernate.show_sql">False</prop>                <!--set hibernate maximum number of SQL statements in a commit batch -                <propKey= "Hibernate.jdbc.batch_size">50</prop>            </Props>        </ Property></Bean>

After the configuration of the sessionfactory is completed in the spring configuration file,
You can inject sessionfactorybean into other beans, such as injected into DAO components.
When a DAO component obtains a Sessionfactorybean reference, it can implement access to the database.

Define Hibernate's transaction manager Hibernatetransactionmanager

<id= "TransactionManager"  class= " Org.springframework.orm.hibernate3.HibernateTransactionManager ">        <!--  -         < name = "Sessionfactory" ref  = "sessionfactory"/></bean>

Spring Integrated Hibernate

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.