spring第9天(事务)

依赖:spring-context,spring-jdbc(这个本身有依赖spring-tx,关于事务的),druid,mysql-connector-java,aspectjweaver五个

由于我是在dao层的实现类中直接使用JdbcTemplate属性进行一些简单的测试,所以

配置文件:仍然是先写一个DruidDataSource的bean,

JdbcTemplate的bean下,添加DruidDataSource的引用,然后配置dao和service。

事务配置:

1.DataSourceTrancetionManager(全称org.springframework.jdbc.datasource.DataSourceTransactionManager)的<bean>,配置dataSource属性。

 关于这个bean的id有一个注意的地方,下面2中的transaction-manager的默认值为"transactionManager",因此如果该bean的id为这个,下面2中的属性可以不

 写,默认值就是,会直接找的。

2.<tx:advice>,它导入的xmlns是以tx结尾的,别弄错了。例子:

<tx:advice id="txAdvisor" transaction-manager="txManager">
    <tx:attributes>
            <tx:method name="get*" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="com.dao.MyCheckEx"/>
     </tx:attributes>
 </tx:advice>
<!--
        tx:advice的事务管理器设置:
        如果你配置的事务管理器的名字就叫做transactionManager,
        那么transaction-manager就可以不用设置
        可以配置多个method,一般的配置,查询操作用只读事务,会优化性能
        它也支持通配符*

        默认情况下,spring会对运行时异常产生回滚,检查异常不回滚
        如果想针对检查异常也回滚,那么就需要配置rollback-for

        mybatis这种持久层框架,其所有数据库操作的异常都是运行时异常
        所以method的rollback-for保留默认即可,不需要额外配置

        事务传播:transaction propogation:主要指的是先前的事务信息 
-->   

  最后关于<aop>的配置,今天练习的时候出了一个坑,自己半天没发现,我在<aop:pointcut>中配置表达式,测试回滚的时候一直失败。怀疑是rollback-for的配置有问题,改了半天代码,最后突然想起来会不会是表达式写错了,一检查才发现指向的不是进行回滚测试方法的那个类——删除员工表数据和删除部门表数据,先删员工后删部门,这两个删除方法是另外写一个类的方法中调用的,本来表达式应该指向这个类的这个方法,但是我指向了单独的删除部门的方法。

另外今天有用到parent这个属性,忘记怎么写了——在要继承别的<bean>的bean上写上parent属性,关于这个父类bean,它如果是抽象类之类的,就不要写class了,并且加上abstract=“true”防止getBean。

今天还有一个错误,如果aop表达式写错,找不到目标的话,会报一个错,忘记记录了,总的来说就是总是说你的第一个<bean>找不到。

spring和mybatis的事务整合直接上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:context="http://www.springframework.org/schema/context"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:property-placeholder location="classpath:db.properties" local-override="true"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
        <property name="url" value="${mysql.url}"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath*:*Mapper.xml"></property>
        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
                <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"></property>
            </bean>
        </property>
        <property name="plugins">
            <list>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="supportMethodsArguments">true</prop>
                        </props>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <mybatis:scan base-package="com.zyl.paging.dao"></mybatis:scan>

    <bean id="deptServiceImpl" class="com.zyl.paging.service.impl.DeptServiceImpl" autowire="byType"></bean>
    <bean id="deptBackupServiceImpl" class="com.zyl.paging.service.impl.DeptBackupServiceImpl" autowire="byType"></bean>

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

    <tx:advice id="txAdvisor">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* com..service.impl.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvisor" pointcut-ref="myPointcut"></aop:advisor>
    </aop:config>
</beans>

复习的时候对web.xml中的3个配置产生了疑惑:

1.classpath和classpath*的区别:

classpath:只会到你指定的class路径中查找文件;

classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

2.配置的<context-param>中<param-name>contextConfigLocation</param-name>中的值是不是不能改:不能改

3.<listener>有什么用?注释后,在java代码getBean处会发生错误,ContextLoaderLitener实现了ServletContextListner,在servlet加载和销毁的时候会自动调用相应的方法。

4.<filter>中的<init-param>的<param-name>的值是不是不能改:不能。进入CharacterEncodingFilter类中,会找到相应的字段和set方法。过滤器中的两个<init-param>缺一不可,不然会中文字符变成?

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>

        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  

 

原文地址:https://www.cnblogs.com/woyujiezhen/p/11765210.html