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调用即可
- spring数据源配置及事务管理--entityManager
- spring数据源配置及事务管理--jdbc
- spring数据源配置及事务管理--hibernate4
- spring数据源配置及事务管理--mybatis3
- Spring 事务管理(1)数据源配置
- Spring 事务管理及配置
- JBoss 中配置数据源(datasource),及EntityManager的使用
- spring datasource 配置及事务管理
- Spring事务管理-配置及使用
- SSH中Spring数据源的配置以及事务管理
- spring声明式事务管理配置及说明
- spring声明式事务管理配置及说明
- spring声明式事务管理配置及说明
- 关于:No transactional EntityManager available--spring MVC 的事务管理
- Spring 配置数据源及properties配置文件加载
- spring-mvc数据源配置及modelAttribute注解
- Spring 事务管理的配置
- spring事务管理配置总结
- Reading notes on <The Art of Unix Programming>(1)
- java 二分查找算法
- SOM小结
- 一种巧妙方法为MFC添加背景图片
- (Java)Java双向链表实现(数据结构七)
- spring数据源配置及事务管理--entityManager
- linux命令目录
- android短信管理器-SmsManager
- Android五大基本组件
- c# 无法加载DLL“###.dll”,: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E),解决办法总结
- C# 产生验证码 利用背景图片绘制
- 我读过关于面试最好的一篇文章
- Search
- 电脑小技巧