Spring 引入属性文件的配置片段
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/database.properties</value>
</list>
</property>
</bean>
MyBatis 集成 Spring 的时候 ,让 MyBatis 去扫描接口(声明为 interface 的文件)和 xml 文件,我们应该这样写:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注意:这里配置扫描所有的 mapper 文件 -->
<property name="mapperLocations" value="classpath*:com/liwei/mybatis/model/*.xml"></property> -->
</bean>
配置扫描所有的接口(声明为 interface )文件:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.liwei.mybatis.inter"></property>
</bean>
MyBatis 引入属性文件的配置:
Spring 声明式事务的配置参考:
第 1 种方式:使用 xml 配置的方式实现声明式事务管理
<!-- 第 1 步:要想使用事务,首先要实例化一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 第 2 步:配置事务通知,这里配置的是最最简单的事务配置通知,不带事务传播特性,即所有的方法都开启了事务 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<!-- 配置事务属性 -->
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 第 3 步:配置事务切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.liwei.tx.service.*.*(..))" id="serviceMethod"/>
<aop:advisor advice-ref="advice" pointcut-ref="serviceMethod"/>
</aop:config>
参考资料:http://www.tuicool.com/articles/qEvy2m
第 2 种方式:使用 annotation 的方式实现声明式事务管理
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
然后在须要定义事务的方法上写上注解:@Transactional
。