ssh3,hibernate和maven
事物管理使用配置文件写
事物依赖
写了后就可以不用在方法或类上写事物了
org.springframework
spring-orm
5.1.7RELEASE
编写配置文件
applicationContext.xml
<cxt:property-placeholder location="classpath:jdbc.properties"/>
<cxt:annotation-config/>
<bean id="dataSource" class="com.mysql.jdbc.jdbc2.option.MysqlDataSource">
<property name="url" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="路径"/>
<property name="hibernateProperties" >
<value>
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hobernate.format_sql=true
</value>
</property>
</bean>
<bean class="org.springframework.orm.hibernate5.HibernateTemlate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
事物打开要加标注@Transactional事物应该在@service层中使用
事物不回滚加@Rollback(false)
hibernate的事物管理
<bean class="org.springframework.orm.hibernate5.HibernateTransactionManage">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
mybatis的事务管理{
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManage">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven/>
}
Hibernate-LocalSessionFactoryBean
dataSource、hibernateProperties、packagesToScan
Hibernate-HibernateTemplate
sessionFactory、get()、save()、
find(5.3:”?0,?1”)、findByNamedParam()
execute()
spring-tx.xsd
<annotation-driven />
<advice id >、
<attributes>
事物管理的几种情况可以在类上写了后再在方法上添加事物减少性能损失
<method name read-only no-rollback-for rollback-for />
execution(* basePackageName.*.*(..))
HibernateTransactionManager
sessionFactory
@Transactional
readOnly、noRollbackFor、rollBackFor
spring-aop.xsd
<advisor pointcut advice-ref pointcut-ref />
DataSourceTransactionManager
dataSource
测试断言