尚筹网项目学习第三天
Spring整合MyBatis注意事项
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)异常解决方式
*一、mapper.xml的namespace是否是对应接口类的全名(包括包名和类名)
二、mapper接口类的方法名是否与mapper.xml中sql标签的id相同(由于我使用maven工程逆向生成没有出现问题)
三、检查Spring配置文件 ,配置SqlSessionFactoryBean整合Mybatis时指定 Mapper 配置文件位置出现错误(使用Ctrl+鼠标左键 如果配置的路径正确,可以直接跳转到所选择的路径对应的包或类,不正确跳转不到。 注意路径是/ 不是* )主要检查下图三个位置
The matching wildcard is strict, but no declaration can be found for element 'aop:config’异常解决方式
在http://www.springframework.org/schema/aop下面加入http://www.springframework.org/schema/aop/spring-aop.xsd
一、必须命令空间xmlns:aop
二、http://www.springframework.org/schema/aop
和http://www.springframework.org/schema/aop/spring-aop.xsd
必须成对出现 俩者次序不能乱
声明式事务
目标:从事务角度一个事务方法中包含的多个数据库操作,要么一起提交、要么一起回 滚。也就是说事务方法中的多个数据库操作,有任何一个失败,整个事务全部回滚。 从声明式角度:由 Spring 来全面接管数据库事务。用声明式代替编程式
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.atguigu.crowd.service"/>
<!--配置事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--装配数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务切入点-->
<!-- 配置 AOP-->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* *..*ServiceImpl.*(..))" id="txPointCut"/>
<!-- <aop:pointcut expression="execution(**com.xx.xx.service.*(..))" id="txPointCut"/>-->
<!-- 将事务通知和切入点表达式关联到一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!--配置事务通知属性-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!--配置事务属性-->
<tx:attributes>
<!--查询方法 :配置只读属性 ,便于数据库根据只读属性进行相关性能优化 -->
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
<!-- 增删改方法另外配置 -->
<!--propagation 属性配置事务方法的传播行为 -->
<!-- 默认值:REQUIRED 表示:当前方法必须运行在事务中,如果没有事务,则开事务,
在自己的事务中运行。如果已经有了已开启的事务,则在当前事务中运行。有可能 和其他方法共用同一个事务。 -->
<!-- 建议值:REQUIRES_NEW 表示:当前方法必须运行在事务中,如果没有事务, 则开启事务,
在自己的事务中运行。和 REQUIRED 的区别是就算现在已经有了已开启的事务, 也一定要开启自己的事务,避免和其他方法共用同一个事务。 -->
<!--rollback-for 属性配置回滚的异常 --> <!-- 默认值:运行时异常 -->
<!-- 建议值:编译时异常+运行时异常 -->
<tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="remove*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
</beans>