Mybatis事务管理

下面是applicationContext.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="com.wlyd.fmcgwms.service.*" />

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath*:jdbc.properties" />
    </bean>
    <!-- 数据源配置 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
    destroy-method="close" >
    <!-- 数据库驱动 -->
    <property name="driverClassName">
      <value>${jdbc.driverClass}</value>
    </property>
    <property name="url">
      <value>${jdbc.jdbcUrl}</value>
    </property>
    <property name="username">
      <value>${jdbc.user}</value>
    </property>
    <property name="password">
      <value>${jdbc.password}</value>
    </property>
    <!-- 连接池最大使用连接数 -->
    <property name="maxActive">
      <value>20</value>
    </property>
    <!-- 初始化连接大小 -->
    <property name="initialSize">
      <value>1</value>
    </property>
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait">
      <value>60000</value>
    </property>
    <!-- 连接池最大空闲 -->
    <property name="maxIdle">
      <value>20</value>
    </property>
    <!-- 连接池最小空闲 -->
    <property name="minIdle">
      <value>3</value>
    </property>
    <!-- 自动清除无用连接 -->
    <property name="removeAbandoned">
      <value>true</value>
    </property>
    <!-- 清除无用连接的等待时间 -->
    <property name="removeAbandonedTimeout">
      <value>180</value>
    </property>
    <!-- 连接属性 -->
    <property name="connectionProperties">
      <value>clientEncoding=UTF-8</value>
    </property>
  </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wlyd.fmcgwms.persistence.mapper" />
    </bean>

    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="del*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="insert*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="execute*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 事务拦截 -->
    <aop:config>
        <aop:pointcut id="pc"
            expression="execution(* com.wlyd.fmcgwms.service..*(..))" />
        <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
    </aop:config>

    <!-- 初始化基础数据  -->
    <bean id="InitSysProperties" class="com.wlyd.fmcgwms.util.sysinit.InitSysProperties" /> 

</beans> 

定时任务模板:

<!-- =====================定时任务job==========================saas -->
    <bean id="DailyTaskJobMethodSaas" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="DailyTaskJob"/>  
        </property>  
        <property name="targetMethod">  
            <!-- 要执行的方法名称 -->  
            <value>executeSaas</value>  
        </property>  
    </bean> 

    <!-- ======================== 调度触发器 ========================saas -->  
    <bean id="DailyTaskCronTriggerBeanSaas" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="jobDetail" ref="DailyTaskJobMethodSaas"></property>  
        <property name="cronExpression" value="0 25 15 * * ?"></property>
    </bean>

    <!-- ======================== 调度工厂 ======================== -->  
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>   
                <ref bean="DailyTaskCronTriggerBeanSaas"/> 
            </list>  
        </property>  
    </bean>   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值