笔记(杂项,自己看)

2 篇文章 0 订阅

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值