Mybatis custom query failed if use docker to run my application


#1

Hello.
I am using mybatis to do custom query.
That is all right if i just compile my app and run it.

But if i compile and create my application as a docker image that is not ok.

Here is error

### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for getAllByProcessInstancesAndGroups
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for getAllByProcessInstancesAndGroups] with root cause
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for getAllByProcessInstancesAndGroups
       at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:875)
       at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:708)
       at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:701)
       at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
       at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
       at com.hikedu.backend.process.impl.camunda.customquery.dao.TaskDao.getAllByProcessInstancesAndGroups(TaskDao.java:56)
       at com.hikedu.backend.service.impl.signupproject.UserTaskServiceImpl.getRunningHumanTasks(UserTaskServiceImpl.java:109)
       at com.hikedu.backend.service.impl.signupproject.UserTaskServiceImpl$$FastClassBySpringCGLIB$$ebcbfe12.invoke(<generated>)
       at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
       at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
       at com.hikedu.backend.service.impl.signupproject.UserTaskServiceImpl$$EnhancerBySpringCGLIB$$e4ae9214.getRunningHumanTasks(<generated>)
       at com.hikedu.backend.controller.ProjectApplicationRecordController.getTheUserTasksOfTheGivenRecords(ProjectApplicationRecordController.java:304)
       at com.hikedu.backend.controller.ProjectApplicationRecordController.getResults(ProjectApplicationRecordController.java:264)
       at com.hikedu.backend.controller.ProjectApplicationRecordController.getAll(ProjectApplicationRecordController.java:194)
       at com.hikedu.backend.controller.ProjectApplicationRecordController$$FastClassBySpringCGLIB$$dc339407.invoke(<generated>)
       at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
       at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
       at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:69)
       at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
       at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
       at com.hikedu.backend.controller.ProjectApplicationRecordController$$EnhancerBySpringCGLIB$$9d81a730.getAll(<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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
       at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
       at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891)
       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
       at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
       at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
       at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
       at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
       at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
       at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.camunda.bpm.webapp.impl.security.filter.SecurityFilter.doFilterSecure(SecurityFilter.java:70)
       at org.camunda.bpm.webapp.impl.security.filter.SecurityFilter.doFilter(SecurityFilter.java:54)
       at org.camunda.bpm.spring.boot.starter.webapp.filter.LazyDelegateFilter.doFilter(LazyDelegateFilter.java:59)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.camunda.bpm.webapp.impl.security.auth.AuthenticationFilter$1.execute(AuthenticationFilter.java:61)
       at org.camunda.bpm.webapp.impl.security.auth.AuthenticationFilter$1.execute(AuthenticationFilter.java:59)
       at org.camunda.bpm.webapp.impl.security.SecurityActions.runWithAuthentications(SecurityActions.java:43)
       at org.camunda.bpm.webapp.impl.security.auth.AuthenticationFilter.doFilter(AuthenticationFilter.java:59)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
       at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at com.hikedu.backend.security.JwtAuthorizationTokenFilter.doFilterInternal(JwtAuthorizationTokenFilter.java:56)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
       at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
       at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
       at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
       at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
       at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
       at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
       at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
       at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
       at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
       at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
       at java.lang.Thread.run(Thread.java:748)

I checked the jar file and confirmed the mappr xml files are compiled and the spacename is not wrong
Here is the mappers from the jar file.
image

Here is the mapper files of the jar file

Here is the code of how I am build the SqlsessionFactory

@Bean(value = "customQuerySqlSessionFactory")
	public SqlSessionFactory createCustomQuerySqlSessionFactory() throws Exception{
		try{
			DataSource dataSource = ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration().getDataSource();


			TransactionFactory transactionFactory = new JdbcTransactionFactory();
			Environment environment = new Environment("customQuery", transactionFactory, dataSource);
			Configuration configuration = new Configuration(environment);
			TypeAliasRegistry aliasRegistry = configuration.getTypeAliasRegistry();
			aliasRegistry.registerAlias("Task", Task.class);
			aliasRegistry.registerAlias("HistoricActivity", HistoricActivity.class);
			aliasRegistry.registerAlias("variable", Variable.class);
			aliasRegistry.registerAlias("historicProcessInstance", HistoricProcessInstance.class);
			configuration.addMappers("com.hikedu.backend.process.impl.camunda.customquery.mapper");

			configuration.hasMapper(Task.class);
			configuration.hasMapper(HistoricActivity.class);
			configuration.hasMapper(Variable.class);
			configuration.hasMapper(HistoricProcessInstance.class);

//			Reader reader = Resources.getResourceAsReader("mybatisCustomQueryConfiguration.xml");

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

Here is the content of the taskMapper.xml file

<?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.hikedu.backend.process.impl.camunda.customquery.mapper.TaskMapper">

    <select id="getAllByProcessInstancesAndGroups" resultType="Task">
        select distinct
        task.ID_ as id,
        task.NAME_ as name,
        task.TASK_DEF_KEY_ as activityId,
        task.PROC_INST_ID_ as processInstanceId,
        task.CREATE_TIME_ as createTime
        from
        hikedu_backend.ACT_RU_TASK task
        join ACT_RU_IDENTITYLINK IDENTITYLINK on task.ID_ = IDENTITYLINK.TASK_ID_
        where 1 = 1
            and task.PROC_INST_ID_ in
            <foreach collection="processInstanceIds" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>

        <if test="groups != null and groups.isEmpty() == false">
            and IDENTITYLINK.GROUP_ID_ in
            <foreach collection="groups" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getByProcessInstanceIdAndGroups" resultType="Task">
        select distinct
        task.ID_ as id,
        task.NAME_ as name,
        task.TASK_DEF_KEY_ as activityId,
        task.PROC_INST_ID_ as processInstanceId,
        task.CREATE_TIME_ as createTime
        from hikedu_backend.ACT_RU_TASK task join ACT_RU_IDENTITYLINK IDENTITYLINK on task.ID_ = IDENTITYLINK.TASK_ID_ where task.PROC_INST_ID_ = #{processInstanceId}

        <if test="groups != null and groups.isEmpty() == false">
            and IDENTITYLINK.GROUP_ID_ in
            <foreach collection="groups" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>
</mapper>

Can someone help me to fix this ?
Thanks you all.


#2

I debuged the mybatis source code.
And finally find the reason and fixed it.

The mybatis will use the some tool to get the mapper files.
But the files in a jar so that is not able to get the files.

How do I resolved this ?

Just change the way of add mapper
Here is the code

@Bean(value = "customQuerySqlSessionFactory")
	public SqlSessionFactory createCustomQuerySqlSessionFactory() throws Exception{
		try{
			DataSource dataSource = ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration().getDataSource();


			TransactionFactory transactionFactory = new JdbcTransactionFactory();
			Environment environment = new Environment("customQuery", transactionFactory, dataSource);
			Configuration configuration = new Configuration(environment);
			TypeAliasRegistry aliasRegistry = configuration.getTypeAliasRegistry();
			aliasRegistry.registerAlias("Task", Task.class);
			aliasRegistry.registerAlias("HistoricActivity", HistoricActivity.class);
			aliasRegistry.registerAlias("variable", Variable.class);
			aliasRegistry.registerAlias("historicProcessInstance", HistoricProcessInstance.class);

                        //Use this way add mappers
			configuration.addMapper(TaskMapper.class);
			configuration.addMapper(HistoricActivityMapper.class);
			configuration.addMapper(VariableMapper.class);
			configuration.addMapper(HistoricProcessInstanceMapper.class);

			/*
			 Disable this.
			 */
//			configuration.addMappers("com.hikedu.backend.process.impl.camunda.customquery.mapper");

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