Why build the SqlSessionFactory of custom query failed?


#1

Hello everyone.

I am using gradle and spring boot and jpa.
For make query be easy and fast I need to config custom query
I was followed the example of custom query to configed my application but after that done I start my application and got an error from mybatis.

Here is my application struct

src    
   --main
          --java
                -- com.hikedu.backend.
                              --mapper 
                                        --TaskMapper.java
                              --dao
                                        --TaskDao.java
   --resources
                 --com.hikedu.backend.mapper
                                       --TaskMapper.xml
                 --mybatis
                                       --customMybatisConfiguration.xml

Here the content of the customMybatisConfiguration.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="lazyLoadingEnabled" value="false" />
	</settings>
	<mappers>
        <mapper resource="com/hikedu/backend/mapper/TaskMapper.xml" />
	</mappers>
</configuration>

Here is the code of build SqlSessionFactory

@Bean(value = "customSqlSessionFactory")
	public SqlSessionFactory createSqlSessionFactory() {


		try {
			ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
			ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
			DataSource dataSource = processEngineConfiguration.getDataSource();
			TransactionFactory transactionFactory = new ManagedTransactionFactory();

			Environment environment = new Environment("customTasks", transactionFactory, dataSource);

			XMLConfigBuilder parser = new XMLConfigBuilder( //
					getMybatisConfig(), //
					"", // set environment later via code
					getSqlSessionFactoryProperties((ProcessEngineConfigurationImpl) processEngineConfiguration));
			Configuration configuration = parser.getConfiguration();
			configuration.setEnvironment(environment);
			configuration = parser.parse();
			configuration.setDefaultStatementTimeout(processEngineConfiguration.getJdbcStatementTimeout());

			return new SqlSessionFactoryBuilder().build(configuration);
		}catch (Exception ex) {
			ex.printStackTrace();
			throw ex;
		}
	}

	private InputStream getMybatisConfig() {
		try {
			return resourceLoader.getResource("classpath:mybatis/customMybatisConfiguration.xml").getInputStream();
		}catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}


	private Properties getSqlSessionFactoryProperties(ProcessEngineConfigurationImpl conf) {
		Properties properties = new Properties();
		ProcessEngineConfigurationImpl.initSqlSessionFactoryProperties(properties, conf.getDatabaseTablePrefix(), conf.getDatabaseType());
		return properties;
	}

When I start my application I got the error

org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/hikedu/backend/mapper/TaskMapper.xml
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
	at com.hikedu.backend.BackendApplication.createSqlSessionFactory(BackendApplication.java:110)
	at com.hikedu.backend.BackendApplication$$EnhancerBySpringCGLIB$$2fe12c19.CGLIB$createSqlSessionFactory$4(<generated>)
	at com.hikedu.backend.BackendApplication$$EnhancerBySpringCGLIB$$2fe12c19$$FastClassBySpringCGLIB$$40c00de7.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:365)
	at com.hikedu.backend.BackendApplication$$EnhancerBySpringCGLIB$$2fe12c19.createSqlSessionFactory(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:197)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1267)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1124)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:333)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265)
	at com.hikedu.backend.BackendApplication.main(BackendApplication.java:50)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.io.IOException: Could not find resource com/hikedu/backend/mapper/TaskMapper.xml
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:367)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:118)
	... 50 more

Some one can help me figeout what is wrong in my configuration ? please.
Thank you all.


#2

@himly

this.getClass().getResource("com/izs/mybatis/FormMapper.xml");
you can try <mapper resource="com/izs/mybatis/FormMapper.xml" /> in the
Configuration.xml

(or)

It seems the resources cannot be seen on the classpath.
You can check by calling Class.getResource("/com/izs/mybatis/FormMapper.xml");
You can try <mapper resource="/com/izs/mybatis/FormMapper.xml"/>

If you are using maven, your mapper and config files should be put under the src/main/resources, not src/main/java.

(or)

Add classpath* before the mapper,similar to below:

<mapper resource="classpath*:mappers/User.xml>

#3

@aravindhrs Thank you. I am already resolved my problem.


#4

@himly thats great you’ve solved the problem. If you’ve solved the problem in different way which mentioned above, you can share your insights of your solution. :slight_smile:


#5

Sure. I will share the solution after finished my job. :grinning: