SSM框架-Spring4
2026/8/3 2:51:07 网站建设 项目流程

目录

一、使用Spring整合Mybatis

二、Spring的事务处理

1.什么是事务:

2.Spring的事务管理的API:

3.使用XML完成Spring的事务配置:

4.Spring中事务使用注意:

5.使用注解完成事务:


一、使用Spring整合Mybatis

1.需要写一个除了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 location="classpath:db.properties"></context:property-placeholder>标签去加载连接数据库的参数文件,db.properties是参数文件。

5.最后使用<property name="driverClassName" 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 name="dataSource" ref="上面定义数据源的bean标签的id"></property>这个标签去赋值。

8.扫描mapper包下面的所有的接口,使用bean对象接管mybatis-spring-x.x.x.jar包下面的包org.mybatis.spring.mapper.MapperScannerConfigurer中的MapperScannerConfigurer类。

9.接下来,我们需要对MapperScannerConfigurer类中的basePackage属性赋值,使用<property name="basePackage" value="mapper包所在的路径"></property>标签去赋值。

10.案例:

<?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" 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 location="classpath:db.properties"></context:property-placeholder> <!--数据源:DriverManagerDataSource dataSource = new DriverManagerDataSource()--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${mysql.driver}"></property> <property name="url" value="${mysql.url}"></property> <property name="username" value="${mysql.username}"></property> <property name="password" value="${mysql.password}"></property> </bean> <!--相当于:SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSessionFactoryBean是spring中提供用来整合mybatis的类 SqlSessionFactoryBean对象,由spring容器根据类型从bean的缓存池中找到使用 --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> <!--扫描com.spring.mapper包下所有接口--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.spring.mapper"></property> </bean> </beans>

二、Spring的事务处理

1.什么是事务:

(1)事务:逻辑上的一组操作,组成这组操作的各个单元,,要么全都成功,要么全都失败。

2.Spring的事务管理的API:

(1)Platform TransctionManager(平台事务管理器):接口,是Spring用于管理事务的真正的对象

  • DataSourceTransactionManager:底层使用JDBC管理事务。
  • HibernateTransactionManager:底层使用Hibernate管理事务

(2)TransactionDefinition(事务定义信息):用于定义事务的相关的信息,隔离级别、超时信息、传播行为(DML)、是否只读(查询)。

(3)TransactionStatus(事务的状态):用于记录在事务管理过程中,事务的状态的对象。

(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 name="transferAccount" propagation="REQUIRED"/>这个标签的name属性代表了你要使用事务的方法名,必须和你要使用事务的方法名一致,不然事务不生效,propagation属性代表的就是传播行为,而外层的tx:advice标签中的属性transaction-manager的值是上面配置事务管理器的切面时使用bean标签接管DataSourceTransactionManager类的bean标签id。

<tx:advice id="abc" transaction-manager="transactionMananger"> <tx:attributes> <tx:method name="transferAccount" propagation="REQUIRED"/> </tx:attributes> </tx:advice>

(4)最后,在把事务传播行为应用到指定的方法上,使用<aop:config></aop:config>,然后在这个aop:config标签之间使用<aop:advisor id="上面配置事务传播行为的最外层标签的id" pointcut="execution(* 包名.类名.方法名(..))"></aop:advisor>标签将事务传播行为应用到指定的方法上。

(5)案例:

<?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:tx="http://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 id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事物传播行为 :其实就是哪些方法应该受什么样的事物控制--> <tx:advice id="abc" transaction-manager="transactionMananger"> <tx:attributes> <tx:method name="transferAccount" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 把REQUIRED事务传播行为应用到指定的方法(transferAccount)上 使用spring的aop编程获取到需要使用事务的切入点(方法)transferAccount() --> <aop:config> <aop:advisor advice-ref="abc" pointcut="execution(* com.spring.service.impl.AccountServiceImpl.transferAccount(..))"/> </aop:config> </beans>

4.Spring中事务使用注意:

(1)spring对事务的处理是在底层捕捉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 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:tx="http://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 id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--开启注解方式事务的处理--> <tx:annotation-driven transaction-manager="transactionMananger" ></tx:annotation-driven> </beans>

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询