Spring hive integration and springhive Integration

Source: Internet
Author: User

Spring hive integration and springhive Integration

Integrate Hibernate with Spring

I. integration goals

1. The IoC container manages the SessionFactory of Hibernate.
2. Let Hibernate use Spring declarative transactions

Ii. Integration steps

Add Hibernat, Spring, and integration.
Step 1:

Configure Hibernate
1. Add Hibernate-related packages
Required Hibernate package
C3p0 package and database driver package
AspectJWeaver. jar
Database driver package

 

 

2. Add the Hibernate configuration file hibernate. cfg. xml.

A. You can configure the Hibernate data source in Spring, so you do not need to configure it in hibernate. cfg. xml.

B. The associated. hbm. xml file can also be configured when SessionFactory is configured in the Spring configuration file.

C. You can configure the SQL dialect, SQL display, automatic table generation, and second-level cache in hibernate. cfg. xml.

 

3. Compile the object class and corresponding hbm. xml ing file.

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

 

 

 

Step 2: Add Spring

1. Add the Spring package.
Spring jar package
Aspectjweaver. jar
2. Add the Spring configuration file.
Configure data sources
1) create a resource file for db. properties and configure the connection information of the data source.

driverClass=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/mydbuser=rootpassword=minPoolSize=5maxPoolSize=20initialPoolSize=5

Import db. properties in the Spring configuration file <context: property-placehoder/>
Configure the ComboPooledDataSource of the c3p0 Data Source
(Data source configuration is successfully tested)

<! -- Load the resource object --> <context: property-placeholder location = "classpath: db. properties"/> <! -- Instantiate the c3p0 data source --> <bean class = "com. mchange. v2.c3p0. comboPooledDataSource "id =" dataSource "> <property name =" driverClass "value =" $ {driverClass} "> </property> <property name =" jdbcUrl "value =" $ {jdbcUrl} "> </property> <property name =" user "value =" $ {user} "> </property> <property name =" password "value =" $ {password} "> </property> <property name =" minPoolSize "value =" $ {minPoolSize} "> </property> <property name =" maxPoolSize "value =" $ {maxPoolSize} "> </property> <property name =" initialPoolSize "value =" $ {initialPoolSize} "> </property> </bean>

2) Configure SessionFactory of Hibernate -- configure it through LocalSessionFactoryBean provided by Spring

<! -- Configure SessionFactory of Hibernate --> <bean class = "org. springframework. orm. hibernate5.LocalSessionFactoryBean" id = "factory"> <! -- Configure data source Attributes --> <property name = "dataSource" ref = "dataSource"> </property> <! -- Configure the location of the Hibernate configuration file --> <property name = "configLocation" value = "classpath: hibernate. cfg. xml"> </property> <! -- Configure the location of the Hibernate ing file. You can use the wildcard --> <property name = "mappingLocations" value = "com/itnba/maya/entities /*. hbm. xml "> </property> </bean>

3) Configure Spring declarative transactions
Configure the Transaction Manager -- HibernateTransactionManager

<! -- Configure the spring transaction manager --> <bean class = "org. springframework. orm. hibernate5.HibernateTransactionManager" id = "transactionManager"> <! -- Configure it according to the hibernate version --> <property name = "sessionFactory" ref = "factory"> </property> </bean>

Configure transaction properties -- import the tx namespace

<! -- Configure transaction properties --> <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "*"/> </tx: attributes> </tx: advice>


Configure transaction cut points and associate the cut points with transaction attributes. -- Import the aop namespace

<! -- Configure the transaction start point --> <aop: config> <aop: pointcut expression = "execution (* com. itnba. maya. entities. *. *(..)) "id =" pointCut "/> <aop: advisor advice-ref =" txAdvice "pointcut-ref =" pointCut "/> </aop: config>

Step 3: write code
1. Configure the automatically scanned package in the Spring configuration file

<! -- Automatic scan --> <context: component-scan base-package = "com. itnba. maya. entities"> </context: component-scan>
Package com. itnba. maya. entities; import org. hibernate. session; import org. hibernate. sessionFactory; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. repository; @ Repository // automatically scans public class InfoDao {@ Autowired // automatically scans private SessionFactory factory; public Session getSession () {return factory. getCurrentSession ();} public void select () {Info data = getSession (). get (Info. class, "p005"); System. out. println (data. getName ());}}

Execute with main Function

package com.itnba.maya.entities;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {    public static void main(String[] args) throws SQLException {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        InfoDao data=(InfoDao) context.getBean(InfoDao.class);        data.select();                }}

Result:

Complete Spring configuration file

<? 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. Xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <! -- Automatic scan --> <context: component-scan base-package = "com. itnba. maya. entities"> </context: component-scan> <! -- Load the resource object --> <context: property-placeholder location = "classpath: db. properties"/> <! -- Instantiate a c3p0 object --> <bean class = "com. mchange. v2.c3p0. comboPooledDataSource "id =" dataSource "> <property name =" driverClass "value =" $ {driverClass} "> </property> <property name =" jdbcUrl "value =" $ {jdbcUrl} "> </property> <property name =" user "value =" $ {user} "> </property> <property name =" password "value =" $ {password} "> </property> <property name =" minPoolSize "value =" $ {minPoolSize} "> </property> <property name =" max PoolSize "value =" $ {maxPoolSize} "> </property> <property name =" initialPoolSize "value =" $ {initialPoolSize} "> </property> </bean> <! -- Configure SessionFactory of Hibernate --> <bean class = "org. springframework. orm. hibernate5.LocalSessionFactoryBean "id =" factory "> <property name =" dataSource "ref =" dataSource "> </property> <property name =" configLocation "value =" classpath: hibernate. cfg. xml "> </property> <property name =" mappingLocations "value =" com/itnba/maya/entities /*. hbm. xml "> </property> </bean> <! -- Configure spring declarative transactions --> <bean class = "org. springframework. orm. hibernate5.HibernateTransactionManager" id = "transactionManager"> <! -- Configure it according to the hibernate version --> <property name = "sessionFactory" ref = "factory"> </property> </bean> <! -- Configure transaction properties --> <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "*"/> </tx: attributes> </tx: advice> <! -- Configure the transaction start point --> <aop: config> <aop: pointcut expression = "execution (* com. itnba. maya. entities. *. *(..)) "id =" pointCut "/> <aop: advisor advice-ref =" txAdvice "pointcut-ref =" pointCut "/> </aop: config> </beans>

In addition:

Spring integrates Hibernate, or you can place the content in the Hibernate configuration file in the Spring configuration file without using the Hibernate configuration file. (Usually not used in this way)

<property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>....</props></property>

 

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.