弹簧启动用mybatis,运行mvn包war将发生无效的绑定语句(未找到)

时间:2016-11-28 13:12:24

标签: spring-boot mybatis spring-mybatis

我已经使用spring boot和mybatis构建了一个项目,它在eclipse中成功运行,但是当我mvn clean package并执行java -jar yishi-service.war时,它发生了异常:

[http-nio-8080-exec-1] ERROR c.j.yishi.service.PageViewService - Invalid bound statement (not found): com.jiajian.yishi.mybatis.mapper.PageViewMapper.insertSelective 
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jiajian.yishi.mybatis.mapper.PageViewMapper.insertSelective
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:214)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    at com.sun.proxy.$Proxy61.insertSelective(Unknown Source)
    at com.jiajian.yishi.service.PageViewService.add(PageViewService.java:35)
    at com.jiajian.yishi.service.PageViewService$$FastClassBySpringCGLIB$$f6c85bb1.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at com.jiajian.yishi.service.PageViewService$$EnhancerBySpringCGLIB$$a64c8127.add(<generated>)
    at com.jiajian.yishi.common.pv.PageViewContainerRequestFilter.filter(PageViewContainerRequestFilter.java:60)
    at org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:132)
    at org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:68)
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:318)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)

我的SessionFactory是:

@Configuration
@EnableTransactionManagement
@MapperScan(value = "com.mybatis.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class SessionFactoryConfig implements TransactionManagementConfigurer{  
    private static String MYBATIS_CONFIG = "mybatis-config.xml";    
    @Autowired    
    private DataSource dataSource;
    @Autowired
    private ResourceLoader resourceLoader;
    private String typeAliasPackage = "com.mybati.model";   
    @Bean(name = "sqlSessionFactory")   
    public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();    
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG));
         sqlSessionFactoryBean.setDataSource(dataSource);    sqlSessionFactoryBean.setMapperLocations(ResourcePatternUtils.getResourcePatternResolver(resourceLoader).
                 getResources("classpath:com/mybatis/mapper/*.xml"));            sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasPackage);        
         return sqlSessionFactoryBean;    
     }
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}

我确信我的mapper和xml是正确的,因为它在我的eclipse中成功运行,我得到了数据。

1 个答案:

答案 0 :(得分:0)

当我睡觉时,我觉得这个异常很奇怪,我怀疑maven插件包可能会省略我在src / main / java文件夹下的xml文件。现在,我确保异常是由于这个原因引起的。 spring boot maven资源默认不包含xml文件,在src / main / java下,我更改了我的pom.xml并且启动成功。 pom.xml添加了这个:

<resources>  
        <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/application*.yml</include>
                    <include>**/application*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>**/application*.yml</exclude>
                    <exclude>**/application*.properties</exclude>
                </excludes>
            </resource>
            <resource>  
                <directory>src/main/java</directory>  
                <includes>  
                    <include>**/*.properties</include>  
                    <include>**/*.xml</include>  
                </includes>   
                <filtering>false</filtering>  
            </resource>  
        </resources>