Spring learning-Hibernate Integration

Source: Internet
Author: User

Spring learning-Hibernate Integration
What is Spring integrated with Hibernate? 1 ). IOC containers are available to manage SessionFactory2 of Hibernate ). you need to configure the data source to the IOC container so that Hibernate uses the Spring declarative transaction Hibernate configuration file (less than a part of the Hibernate configuration), so you do not need to configure the data source here. associated. hbm. when configuring the SessionFactory instance in the IOC container, xml only needs to configure the dialect and Table Generation Policy. hibernate master Configuration

<Session-factory> <property name = "hibernate. dialect "> org. hibernate. dialect. mySQL5InnoDBDialect </property> <property name = "hibernate. show_ SQL "> true </property> <property name =" hibernate. format_ SQL "> true </property> <property name =" hibernate. hbm2ddl. auto "> update </property> <! -- Configure the attributes related to the hibernate second-level cache. --> </session-factory> Spring IOC container configuration

 

<! -- Configure the automatically scanned package --> <context: component-scan base-package = "com. atguigu. spring. hibernate"> </context: component-scan> <! -- 1. Configure the data source --> <! -- Import the resource file --> <context: property-placeholder location = "classpath: db. properties "/> <bean id =" dataSource "class =" com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" user "value =" $ {jdbc. user} "> </property> <property name =" password "value =" $ {jdbc. password} "> </property> <property name =" driverClass "value =" $ {jdbc. driverClass} "> </property> <property name =" jdbcUrl "value =" $ {jdbc. jdbcUrl} "> </property> <Property name = "initialPoolSize" value = "$ {jdbc. initPoolSize} "> </property> <property name =" maxPoolSize "value =" $ {jdbc. maxPoolSize} "> </property> </bean> <! -- 2. configure the SessionFactory instance of Hibernate: The LocalSessionFactoryBean provided by Spring has three attributes --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate4.LocalSessionFactoryBean "> <! -- Configure data source Attributes --> <property name = "dataSource" ref = "dataSource"> </property> <! -- Configure the location and name of the hibernate configuration file --> <! -- <Property name = "configLocation" value = "classpath: hibernate. cfg. xml"> </property> --> <! -- Use hibernateProperties to configure native attributes of Hibernate --> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> org. hibernate. dialect. mySQL5InnoDBDialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hibernate. format_ SQL "> true </prop> <prop key =" hibernate. hbm2ddl. auto "> update </prop> </props> </property> <! -- Configure the location and name of the hibernate ing file. You can use the wildcard --> <property name = "mappingLocations" value = "classpath: com/atguigu/spring/hibernate/entities /*. hbm. xml "> </property> </bean> <! -- 3. Configure Spring declarative transactions with XML --> <! -- 1. configure the Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate4.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <! -- 2. to configure transaction properties, you must use the transaction manager --> <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "get *" read-only = "true"/> <tx: method name = "purchase" propagation = "REQUIRES_NEW"/> <tx: method name = "*"/> </tx: attributes> </tx: advice> <! -- 3. configure transaction cut points and associate them with transaction properties --> <aop: config> <aop: pointcut expression = "execution (* com. atguigu. spring. hibernate. service. *. *(..)) "id =" txPointcut "/> <aop: advisor advice-ref =" txAdvice "pointcut-ref =" txPointcut "/> </aop: config> </beans>

 


Related Article

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.