公司动态
SSM框架-Spring4
目录一、使用Spring整合Mybatis二、Spring的事务处理1.什么是事务2.Spring的事务管理的API3.使用XML完成Spring的事务配置4.Spring中事务使用注意5.使用注解完成事务一、使用Spring整合Mybatis1.需要写一个除了spring.xml配置文件以外的配置文件这个配置文件中使用的约束是和spring.xml配置文件的的约束是一致的。2.在这个新写的配置文件中使用bean标签去接管spring-jdbc-x.x.x.RELEASE.jar包下面的包org.springframework.jdbc.datasource.DriverManagerDataSource中的DriverManagerDataSource类这个相当于定义了一个数据源。3.接管完DriverManagerDataSource类之后需要给这个类中的连接数据库的参数去赋值使用property标签。4.使用context:property-placeholder locationclasspath:db.properties/context:property-placeholder标签去加载连接数据库的参数文件db.properties是参数文件。5.最后使用property namedriverClassName value${mysql.driver}/property标签去动态的获取db.propertioes参数文件中的连接数据库的参数${这个里面的参数文件的的key}。6.完成上面一系列操作之后就可以去创建SqlSessionFactory对象了创建这个对象需要使用bean标签去接管mybatis-spring-x.x.x.jar包下面的包org.mybatis.spring.SqlSessionFactoryBean中的SqlSessionFactoryBean类。这个mybatis-spring-x.x.x.jar包是spring和mybatis的整合包。1注意这个bean标签的id可以写也可以不写因为这个类创建出对象后并不需要我们去调用它是由系统调用的。7.接下来我们需要对这个SqlSessionFactoryBean类中所持有的DataSource属性去赋值这个DataSource代表了数据源使用property namedataSource ref上面定义数据源的bean标签的id/property这个标签去赋值。8.扫描mapper包下面的所有的接口使用bean对象接管mybatis-spring-x.x.x.jar包下面的包org.mybatis.spring.mapper.MapperScannerConfigurer中的MapperScannerConfigurer类。9.接下来我们需要对MapperScannerConfigurer类中的basePackage属性赋值使用property namebasePackage valuemapper包所在的路径/property标签去赋值。10.案例?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocation http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd !--从src目录下加载db.properties文件,classpath表示从src目录下开始-- context:property-placeholder locationclasspath:db.properties/context:property-placeholder !--数据源DriverManagerDataSource dataSource new DriverManagerDataSource()-- bean iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSource property namedriverClassName value${mysql.driver}/property property nameurl value${mysql.url}/property property nameusername value${mysql.username}/property property namepassword value${mysql.password}/property /bean !--相当于SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream); SqlSessionFactoryBean是spring中提供用来整合mybatis的类 SqlSessionFactoryBean对象由spring容器根据类型从bean的缓存池中找到使用 -- bean classorg.mybatis.spring.SqlSessionFactoryBean property namedataSource refdataSource/property /bean !--扫描com.spring.mapper包下所有接口-- bean classorg.mybatis.spring.mapper.MapperScannerConfigurer property namebasePackage valuecom.spring.mapper/property /bean /beans二、Spring的事务处理1.什么是事务1事务逻辑上的一组操作组成这组操作的各个单元要么全都成功要么全都失败。2.Spring的事务管理的API1Platform TransctionManager平台事务管理器接口是Spring用于管理事务的真正的对象DataSourceTransactionManager底层使用JDBC管理事务。HibernateTransactionManager底层使用Hibernate管理事务2TransactionDefinition事务定义信息用于定义事务的相关的信息隔离级别、超时信息、传播行为(DML)、是否只读(查询)。3TransactionStatus事务的状态用于记录在事务管理过程中事务的状态的对象。4事务管理的API的关系Spring进行事务管理的时候首先平台事务管理器根据事务定义信息进行事务的管理在事务管理过程中产生各种状态将这些状态的信息记录到事务状态的对象中。Spring的事务的传播行为Spring中提供了七种事务的传播行为保证多个操作在同一个事务中PRORAGATION_REQUIRED默认值(一般都只要用这个)如果A中有事务使用A中的事务如果A中没有事务则创建一个新的事务将操作包含进来。PROPAGATION_SUPPORTS支持事务如果A中有事务使用A中的事务。如果A没有事务则不使用事务。PROPAGATION_MANDATORY如果A中有事务使用A中的事务。如果A没有事务抛出异常。保证多个操作不在同一个事务中PROPAGATION_REQUIRES_NEW如果A中有事务将A的事务挂起(暂停)创建新事务只包含自身操作。如果A中没有事务创建一个新事务包含自身操作。PROPAGATION_NOT_SUPPORTED如果A中有事务将A的事务挂起。不使用事务管理。PROPAGATION_NEVER如果A中有事务报异常。嵌套式事务PROPAGATION_NESTED嵌套事务如果A中有事务按照A的事务执行执行完成后设置一个保存点执行B中的操作如果没有异常执行通过如果有异常可以选择回滚到最初位置也可以回滚到保存点。3.使用XML完成Spring的事务配置1需要先配置事务管理器的切面也就是使用bean标签去接管包org.springframework.jdbc.datasource.DataSourceTransactionManager中的DataSourceTransactionManager类前面说过这个类是Spring用于管理事务的对象。2接管完成后使用property去为这个类中的dataSource属性去赋值赋值一个数据源就可以了可以使用前写过的。3接下来就是去配置事务的传播行为使用下面的一组标签其中tx:method nametransferAccount propagationREQUIRED/这个标签的name属性代表了你要使用事务的方法名必须和你要使用事务的方法名一致不然事务不生效propagation属性代表的就是传播行为而外层的tx:advice标签中的属性transaction-manager的值是上面配置事务管理器的切面时使用bean标签接管DataSourceTransactionManager类的bean标签id。tx:advice idabc transaction-managertransactionMananger tx:attributes tx:method nametransferAccount propagationREQUIRED/ /tx:attributes /tx:advice4最后在把事务传播行为应用到指定的方法上使用aop:config/aop:config然后在这个aop:config标签之间使用aop:advisor id上面配置事务传播行为的最外层标签的id pointcutexecution(* 包名.类名.方法名(..))/aop:advisor标签将事务传播行为应用到指定的方法上。5案例?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aop xmlns:txhttp://www.springframework.org/schema/tx xsi:schemaLocation http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd !-- bean definitions here -- !-- 配置事物管理器的切面 Spring中使用DataSourceTransactionManager对象来管理事务这里可以设置事务的传播行为 DataSourceTransactionManager transactionMananger new DataSourceTransactionManager() -- bean idtransactionMananger classorg.springframework.jdbc.datasource.DataSourceTransactionManager property namedataSource refdataSource/ /bean !-- 配置事物传播行为 其实就是哪些方法应该受什么样的事物控制-- tx:advice idabc transaction-managertransactionMananger tx:attributes tx:method nametransferAccount propagationREQUIRED/ /tx:attributes /tx:advice !-- 把REQUIRED事务传播行为应用到指定的方法(transferAccount)上 使用spring的aop编程获取到需要使用事务的切入点(方法)transferAccount() -- aop:config aop:advisor advice-refabc pointcutexecution(* com.spring.service.impl.AccountServiceImpl.transferAccount(..))/ /aop:config /beans4.Spring中事务使用注意1spring对事务的处理是在底层捕捉RuntimeException异常进行处理的所以在有事务的地方不要手动try catch异常信息否则事务会失效。5.使用注解完成事务1需要先配置事务管理器的切面也就是使用bean标签去接管包org.springframework.jdbc.datasource.DataSourceTransactionManager中的DataSourceTransactionManager类前面说过这个类是Spring用于管理事务的对象。2开启注解方式事务的处理使用tx:annotation-driven transaction-manager上面配置切面时使用bean标签接管类的bean标签id /tx:annotation-driven标签。3在需要使用注解的方法上面加一个Transactional(propagation Propagation.REQUIRED)注解就可以了括号中是用来配置事务的传播行为的。4案例?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aop xmlns:txhttp://www.springframework.org/schema/tx xsi:schemaLocation http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd !-- bean definitions here -- !-- 配置事物管理器的切面 Spring中使用DataSourceTransactionManager对象来管理事务这里可以设置事务的传播行为 DataSourceTransactionManager transactionMananger new DataSourceTransactionManager() -- bean idtransactionMananger classorg.springframework.jdbc.datasource.DataSourceTransactionManager property namedataSource refdataSource/ /bean !--开启注解方式事务的处理-- tx:annotation-driven transaction-managertransactionMananger /tx:annotation-driven /beans