spring整合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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 创建spring容器时要扫描的包 要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架去管理 -->
<context:component-scan base-package="cn.itcast">
<!-- 配置要忽略的注解 @Controller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Spring整合Mybatis -->
<!-- 配置Spring内置数据源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 配置c3po数据源 -->
<!--<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>-->
<!-- 配置SqlSessionFactory的bean
SqlSessionFactoryBean来替代SqlSessionFactoryBuilder来创建SqlSession
采用数据映射器(MapperFactoryBean)的方式
不用写mybatis映射文件
采用注解方式提供相应的sql语句和输入参数。
-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"></property>
<!-- 给实体类配置别名 -->
<property name="typeAliasesPackage" value="cn.itcast.entity"/>
<!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
*是个通配符,代表所有的文件,**代表所有目录下 -->
<property name="mapperLocations" value="classpath:cn/itcast/dao/*.xml"/>
</bean>
<!-- spring与mybatis整合配置,扫描所有dao,在单数据源的情况下可以不写sqlSessionFactoryBeanName -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.dao"/>
<!--<property name="sqlSessionFactoryBeanName" ref="sqlSessionFactory"/>-->
</bean>
<!-- spring中基于xml的声明式事务控制步骤配置
1.配置事务管理器
2.配置事务的通知
此时我们需要导入事务的约束 tx名称空间和约束,同时也需要aop的
使用tx:advice标签配置事务通知
属性:
id:给事务通知起一个唯一标识
transaction-manage:给事务通知提供一个事务管理器引用
3.配置AOP中的通用切入点表达式
4.建立事务通知和切入点表达式的对应关系
5.配置事务的属性
是在事务的通知tx:advice标签的内部
-->
<!-- 配置声明式事务 -->
<!-- 配置事务 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置通知 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<!-- 配置事务的属性
isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用的是数据库的默认隔离级别。
propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定有事务,增删改的选择。查询方法可以选择SUPPORTS。
read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值为false,表示读写。
timeout:用于指定事务的超时时间。默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
rollback-for:用于指定一个异常,当产生该异常时,事务回滚。产生其他异常时,事务不回滚。没有默认值,表示任何事务都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚。产生其他异常时,事务回滚。没有默认值,表示任何事务都回滚。
-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.impl.*.*(..))"></aop:advisor>
</aop:config>
<!-- 组装切点加额外功能 在业务层的实现类上加@Tasaction 指定该类是一个切面类 方法都是切入点-->
<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
</beans>
来源:CSDN
作者:zp2605811855
链接:https://blog.csdn.net/zp2605811855/article/details/103773741