spring数据源配置及事务管理--entityManager

来源:互联网 发布:js判断大于当前时间 编辑:程序博客网 时间:2024/05/16 18:34

一、实体管理其必须运行在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="http://www.springframework.org/schema/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调用即可

原创粉丝点击