秋老虎iu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<!-- 配置DataSource接口,这个我们可以直接用C3P0作为数据连接池
bean的命名规则:类名首字母小写,后面的不变
-->
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="user" value="hotelsup"/>
<property name="password" value="123"/>
<property name="maxPoolSize" value="40"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="60"/>
<property name="checkoutTimeout" value="2000"/>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 指的是当前的mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 首先告诉spring,所有的的对于数据库的操作都有事务开始管理 -->
<!-- 配置spring的声明式事务 -->
<!-- 事务管理的bean -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明式事务的配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 事务的定义 -->
<tx:attributes>
<!-- propagation称之为事务的传播性 -->
<tx:method name="*" rollback-for="EXCEPTION" propagation="REQUIRED"/><!-- 所有的方法全部加入到事务中 -->
<tx:method name="get*" propagation="NEVER"/><!-- 不执行事务操作 -->
<tx:method name="set*" propagation="NEVER"/><!-- 不执行事务操作 -->
</tx:attributes>
</tx:advice>
<!-- AOP编程实现 -->
<aop:config>
<!-- 切入点的配置,哪些类或者哪些方法进入到切面中 -->
<aop:pointcut expression="execution(* com.jinglin.hotelsup.service..*(..))" id="allMethods"/>
<!-- 把切入点同事务整合在一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethods"/>
</aop:config>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />


<!-- 通过spring的包的扫描的方式将这些包里的类扫描到spring的容器里 -->
<!-- 开启注解配置-->
<context:annotation-config/>
<!-- 对dao层的扫描,实际就是批量生成dao层的bean对象
生成的规则:bean的id名实际就是dao层类或接口的首字母小写
比如,UserInfoMapper->userInfoMapper
GoodsInfoMapper->goodsInfoMapper
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描的包名 -->
<property name="basePackage" value="com.jinglin.hotelsup.dao.imp"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 将service层全部扫描到spring容器里 -->
<context:component-scan base-package="com.jinglin.hotelsup.service"/>

</beans>

 

 

在Service层的体现

@Transactional // 注解,让事务失效,接触
@Service // 扫描了service 所以这里要注解
public class GoodsInfoService {
// 注入的dao层
@Autowired // 自动配置set get
private GoodsInfoMapper goodsInfoMapper;

// 查询
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public List<GoodsInfo> getlist() {
return goodsInfoMapper.getlist();
}

// 添加
public int adduser(GoodsInfo g) {
return goodsInfoMapper.additem(g);
}

}

posted on 2017-07-06 11:13  秋老虎iu  阅读(138)  评论(0编辑  收藏  举报