Spring+ MyBatis+ mvn Project , java.io.FileNotFoundException



我使用Spring 4.0.5和MyBatis 3.2.7与maven。我在WEB-INF/myBatis文件夹下有我的myBatis-config.xml文件。在尝试创建sqlSessionFactory bean时,我得到了FileNotFoundError。

这是我的豆子:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />         
            <property name="configLocation" value="myBatis/myBatis-config.xml" />
            <property name="mapperLocations" value="classpath*:com/attinad/mappers/*.xml" />
            </bean>

堆栈跟踪:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource [/WEB-INF/ds-config.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/myBatis/myBatis-config.xml]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/myBatis/myBatis-config.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:358)
    at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:340)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 21 more

我试过把/classpath*:WEB-INF/myBatis/myBatis-config.xml作为值。但它没有帮助。

请帮助我找到解决方案,因为我花了几个小时调试这个

请找到myBatis-config.xml文件。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>
 <settings>
 <setting name="cacheEnabled" value="true" />
 <setting name="lazyLoadingEnabled" value="true" />
 <setting name="multipleResultSetsEnabled" value="true" />
 <setting name="useColumnLabel" value="true" />
 <setting name="useGeneratedKeys" value="false" />
 <setting name="defaultExecutorType" value="SIMPLE" />
 <setting name="defaultStatementTimeout" value="100" />
 <setting name="safeRowBoundsEnabled" value="false" />
 <setting name="mapUnderscoreToCamelCase" value="false" />
 <setting name="localCacheScope" value="SESSION" />
 <setting name="jdbcTypeForNull" value="OTHER" />
 <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
 </settings> 
 <typeAliases><typeAlias type="classpath*:com.attinad.model.Employee" alias="Emp"/></typeAliases>
</configuration>

我的Mapper文件EmployeeMapper.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.attinad.dao.EmployeeMapperInterface">
<select id="getEmployeeWithId" parameterType="Long" resultType="Emp">
        select *  from
        employees where
        id=#{empId}
    </select>
</mapper>

你有两个选择。

  1. myBatis-config.xml放在WEB-INF/classes文件夹中,并输入classpath:myBatis-config.xml

  2. 由于您正在使用maven src/main/resources已经在classpath中,因此您可以将文件放在src/main/resources下并使用classpath:myBatis-config.xml

我的配置是:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="WEB-INFmybatisConf.xml" />
</bean>

<bean id="sqlMapClient" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="sqlSessionFactory" />
</bean>

相关内容

最新更新