spring-mybatis整合的一些问题
-
忘了添加spring的jar报错信息
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1739) ... 40 more 这个报错信息是 项目中忘了添加 spring-jdbc.jar 包
Caused by: java.lang.ClassNotFoundException: org.springframework.dao.support.DaoSupport at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 91 more 这个报错信息是 项目中忘了添加 spring-tx.jar 包
-
mapper扫描器配置出错问题
报错信息如下:
Caused by: java.lang.ClassNotFoundException: ${db_driver} at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:618) ... 62 more
开始一直以为是applicationContext.xml 文件配置错误 找了好多遍
<!-- 配置 sqlSessionFactory 的bean 对象 产生 sqlSession--> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置mapper 扫描的bean 使用sqlsession 扫描mapper 包获取mapper接口实例化对象 --> <bean id="a" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="factory"></property> <property name="basePackage" value="com.xxx.mapper"/> </bean>
解决问题:
把sqlSessionFactory 的配置需要进行修改: <property name="sqlSessionFactory" ref="factory"></property> 把这个配置改成下面 <bean id="a" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="factory"></property> <property name="basePackage" value="com.sxt.mapper"/> </bean>
为什么什么报错 可以参考下面文章:
[https://www.cnblogs.com/wt20/p/10992297.html]: