配置文件出现问题,报错的原因在于创建sqlsessionfactroy失败(这个坑确实比较隐晦)
Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [org.springframework.core.io.Resource[]] for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [ssm/mybatis/mapper*.xml]: ServletContext resource [/ssm/mybatis/] cannot be resolved to URL because it does not exist
看详细的设置之后,不论是eclipse还是idea开发,关键是配置sqlSessionFactory个路径是用“.”还是用“/”表示,还有是ref还是value等。网上的教程有的时间比较老,所以多尝试几次
我用的是eclipse开发,是在applicationContext.xml文件中,创建SqlSessionFactory出现了问题,报错的主要是在处理sql文件映射时
这里处理问题的关键是,在路径下加了类路径classpath,在该目录下去寻找mapper的映射文件,就解决该问题了
<property name="mapperLocations" value="classpath:ssm/mybatis/mapper/*.xml"/>
另外:还有一个经常出错的原因是多级目录建立的原因 :例如(之前看个博客上有人回复)
<mappers> <mapper resource="com.abc.mybatis.dao/EmployeesMapper.xml"/> com.abc.mybatis.dao/EmployeesMapper.xml <!--<mapper resource="EmployeesMapper.xml"/>--> </mappers> 建立的多级目录有问题,仔细看看是com.abc.mybatis.dao还是com/abc/mybatis/dao