1
votes

I am using Spring 4.0.5 and MyBatis 3.2.7 with maven. I have my myBatis-config.xml file under WEB-INF/myBatis folder. While trying to create the sqlSessionFactory bean, I get the FileNotFoundError.

This is my bean:

<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>

Stack Trace:

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

I have tried putting /classpath*:WEB-INF/myBatis/myBatis-config.xml as the value. But it didn't help.

Please help me find the solution as I have spent hours debugging this.

Please find myBatis-config.xml file.

<?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>

My Mapper file EmployeeMapper.xml file is as follows:

<?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>
2

2 Answers

2
votes

There is two option for you.

  1. Place your myBatis-config.xml inside WEB-INF/classes folder, and say classpath:myBatis-config.xml

  2. Since you are using maven src/main/resources is already in the classpath so you can place your file under src/main/resources and reference it using classpath:myBatis-config.xml

0
votes

My configuration is:

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

and

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

Hope it will help you.