spring 配置jdbc/hibernate/jpa
http://www.cnblogs.com/tazi/archive/2012/01/04/2311577.html
?
Spring配置事務(wù)的三種方式
步驟:數(shù)據(jù)源配置-事務(wù)配置(xml配置方式或注解方式)
如果要使用注解方式依賴注入sessionFactory到業(yè)務(wù)Bean中(使用@Resource)或者注入entityManager到業(yè)務(wù)Bean中(使用@PersistenceContext ),要加入<context:annotation-config/>
1.使用數(shù)據(jù)源:
<!-- 配置數(shù)據(jù)源 -->
??? <bean id="dataSource"
??? class="org.apache.commons.dbcp.BasicDataSource"
??? destroy-method="close"
??? >
??????? <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
??????? <property name="url"
??????? value="jdbc:mysql://localhost:3306/hib?useUnicode=true&characterEncoding=UTF-8"/>
??????? <property name="username" value="root"/>
??????? <property name="password" value="123456"/>
??????? <property name="initialSize" value="1"/>
??????? <property name="maxActive" value="100"/>
??????? <property name="maxIdle" value="2"/>
??????? <property name="minIdle" value="1"/>
??? </bean>
<!-- 數(shù)據(jù)源事務(wù)管理器-->
??? <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
??????? <property name="dataSource" ref="dataSource"/>
??? </bean>
? <!-- 注解方式配置事務(wù) -->
??? <tx:annotation-driven transaction-manager="txManager"/>
2.使用Hibernate。
(1).配置數(shù)據(jù)源同上。
(2).配置sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
??????? <property name="dataSource" ref="dataSource"/>
??????? <property name="mappingResources">
??????????? <list>
??????????????? <value>com/tazi/domin/Person.hbm.xml</value>
??????????? </list>
??????? </property>
??????? <property name="hibernateProperties">
??????????? <value>
??????????? hibernate.dialect=org.hibernate.dialect.MySQLDialect
??????????? hibernate.show_sql=true
??????????? hibernate.format_sql=true
??????????? hibernate.hbm2ddl.auto=update
??????????? </value>
??????? </property>
??? </bean>
(3).配置事務(wù)管理器
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
??????? <property name="sessionFactory" ref="sessionFactory"/>
??? </bean>
(4).注解方式配置事務(wù)同上。
3.使用JPA
(1).配置持久化數(shù)據(jù)單元
src/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
??? http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
???
??? <persistence-unit name="jpaPU" transaction-type="RESOURCE_LOCAL">
??????? <provider>org.hibernate.ejb.HibernatePersistence</provider>
????????? <properties>
????????????? <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
??????????? <property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
??????????? <property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/hib"/>
??????????? <property name = "hibernate.connection.username" value = "root"/>
??????????? <property name = "hibernate.connection.password" value = "123456"/>
??????????? <property name = "hibernate.hbm2ddl.auto" value = "update"/>
????????? </properties>
??? </persistence-unit>
?
</persistence>
(2).配置entityManagerFactory
<bean id="entityManagerFactory"
??????? class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
??????? <property name="persistenceUnitName" value="jpaPU" />
??? </bean>
(3).配置事務(wù)管理器
<bean id="transactionManager"
??????? class="org.springframework.orm.jpa.JpaTransactionManager">
??????? <property name="entityManagerFactory"?????? ref="entityManagerFactory" />
??? </bean>
(4).注解方式配置事務(wù)同上。
分析spring源代碼
LocalEntityManagerFactoryBean中的方法createNativeEntityManagerFactory()。由以下代碼可以看出Spring可以利用JPA的實現(xiàn)產(chǎn)品(這里是Hibernate)來創(chuàng)建實體管理器工廠(EntityManagerFactory)。
protected EntityManagerFactory createNativeEntityManagerFactory()
??? throws PersistenceException
? {
?? //日志記錄省略
??? PersistenceProvider provider = getPersistenceProvider();
??? if (provider != null)
??? {
????? EntityManagerFactory emf = provider.createEntityManagerFactory(getPersistenceUnitName(), getJpaPropertyMap());
????? if (emf == null) {
??????? throw new IllegalStateException("PersistenceProvider [" + provider + "] did not return an EntityManagerFactory for name '" + getPersistenceUnitName() + "'");
????? }
????? return emf;
??? }
??? return Persistence.createEntityManagerFactory(getPersistenceUnitName(), getJpaPropertyMap());
? }
?
?
LocalEntityManagerFactoryBean并沒有實現(xiàn)EntityManagerFactory接口,配置它得到EntityManagerFactory對象的原因是它實現(xiàn)了spring框架中的FactoryBean接口,從而Spring可以通過調(diào)用接口方法getObject等創(chuàng)建EntityManagerFactory實現(xiàn)者的實例。
總結(jié)
以上是生活随笔為你收集整理的spring 配置jdbc/hibernate/jpa的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java线程之守护线程(Daemon)
- 下一篇: spring 同时配置hibernate