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&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