spring(3)
来源:互联网 发布:海岛奇单导弹升级数据 编辑:程序博客网 时间:2024/05/02 22:26
applicationContext.xml文件的配置:
首先配置数据源(和数据库的连接配置):
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- &在配置文件中代表& -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis117?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="lovo"/>
</bean>
然后配置SqlSession工厂(连接Mybatis配置文件):
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
再加将配置的SqlSeesion和持久层建立依赖:
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lovo.sm.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
</bean>
然后配置事务管理器:
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
最后就是配置事务管理:
一般有两种方式:
方式一:用注解来配置,优点是配置简单,但是只适用于单数据库,不能满足多种数据库的需求
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
在配置文件中配置后,还需要在响相应的业务层实现类写注释:
在业务层类前加注释:@Transactional
在方法前加注释:@Transactional(isolation=Isolation.SERIALIZABLE,readOnly=false,rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
方式二:采用springAOP来声明式的处理事务
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="true"></tx:method>
<tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
<tx:method name="search*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
如果是第二种方式来配置事务管理,还需要配置相应的事务关联(指定事务管理的位置);
<aop:config>
<!-- 声明切入点 -->
<aop:pointcut id="serviceMethod" expression="execution(* com.lovo.sm.service.impl.*ServiceImpl.*(..))"></aop:pointcut>
<!-- 将我们定义的通知,交给AOP来关联 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"></aop:advisor>
</aop:config>
方式二可以满足多个数据库的需求,达到分布式数据的效果,通知需要注意的是方式一和方式二只能存在一个,不能并存
- spring 3
- spring(3)
- Spring-3
- Spring 3
- Learn Spring in spring(3)
- Learn Spring in spring(3)
- Spring-(3)Spring DI
- Spring DAO(3):Spring 事务管理
- [Spring]Spring AOP学习笔记(3)---Spring JDBC总结
- spring学习笔记3---spring aop
- Spring 3 ehcache -spring-annotations 配置过程.
- 学习笔记3:Struts2+Spring JDBC+Spring
- Spring(3)--Spring示例再演示
- Struts+Hibernate+Spring学习心得(3)Spring
- Spring Cloud 3:Spring Boot快速入门
- Migrating spring 4 from spring 3
- 【Spring】Spring Framework Reference Documentation中文版3
- Spring Boot -- Spring Data Jpa(3)
- 82. Remove Duplicates from Sorted List II
- hdoj 利息计算 1994 (计算) 水
- SUSE11 NFS Server
- Ubuntu 14.04下OpenCV 3.0 安装 测试
- 函数节流闭包封装
- spring(3)
- 数据库SQL语句总结
- POJ 1308
- openshift 建站的总结
- 第五周项目3-时间类(增加n秒)
- 推荐系统学习01-引子
- Codeforces Round #346 (Div. 2) - C Tanya and Toys
- 【day0402】C++引用形参
- Python queue