启动项目,控制台打印错误信息,其中一条如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSession
Caused by: java.lang.ClassNotFoundException: org.apache.ibatis.session.SqlSession
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1328)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1156)
... 79 more
很明显,缺少SqlSession这个bean
问题一:缺少jar包
解决方法:在pom.xml文件导入jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
问题二:配置sqlSessionFactory不正确
解决方法:配置sqlSessionFactory
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<property name="mapperLocations">
<array>
<value>classpath:mybatis.base/*Mapper.xml</value>
<value>classpath:mybatis.expert/*Mapper.xml</value>
</array>
</property>
<!-- 指定别名包 -->
<property name="typeAliasesPackage" value="com.base.entity,com.base.entityVo,com.expert.entity,com.expert.entityVo" />
</bean>
问题三:jar已经导入,配置也正常,但把项目拉到其他地方运行报错
解决方法:问题出现在artifacts里出现多个部署文件,导致冲突。最简单的就是把之前的部署文件删除,重新build。