spring資料來源配置及事務管理--entityManager
一、實體管理其必須執行在web(ejb容器)容器,用main方法是不能執行的。
二、配置檔案:applicationContext-em.xml
<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:tx="http://www.springframework.org/schema/tx"
xmlns:context="
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.csair"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource_jdbc" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource_jdbc"/>
<property name="packagesToScan" value="com.csair.entity"/><!-- instead of the persistence.xml file -->
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/> <!-- No PersistenceProvider specified in EntityManagerFactory configuration -->
<property name="jpaProperties">
<props>
<!-- 命名規則 My_NAME->MyName -->
<!-- <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> -->
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref local="entityManagerFactory"/>
</property>
</bean>
<!-- <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> -->
<tx:advice id="advice" transaction-manager="jpaTransactionManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.csair.dao.**.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<bean id="entityManager" class="com.csair.dao.HibernateEntityManager">
</bean>
</beans>
三、dao類
@Service("hibernateEntityManager")
public class HibernateEntityManager {
@PersistenceContext
private EntityManager em;
public void saveEntity(com.csair.entity.UserAnotation user) {
try {
em.persist(user);
} catch(Exception e) {
e.printStackTrace();
}
}
}
四、通過web呼叫即可