mybatis错误: Invalid bound statement (not found) 怎么解决

在网上找了很多方法都没得到解决,由于我是同时采用的注解和xml的方法来配置mybatis的mapper…测试xml对应的mapper接口的方法就失败,单独用注解的方式就能测试成功,后来发现坑爹的是由于mapper接口和mapper.xml文件不在一个包里!!!由于我用的generator自动生成的dao类,默认就给我分到两个包去了,希望以后遇到这个问题的童鞋注意哦哦哦.
2017.8.15更新:
本人是使用java配置来配置Spring的,也就是ContextLoaderListener也是基于java配置的.在SqlSessionFactoryBean中,就要用如下代码手动设置*mapper.xml文件位置,同时使用@mapperScan注解扫描dao类的位置,不然也会报错,其实就是xml配置的替代版 .如下所示:

 package cn.paul.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

/**
 * Created by paul on 2017-06-14.
 */
@ComponentScan(basePackages = "cn.paul.*", excludeFilters = {@ComponentScan.Filter
        (type = FilterType.ANNOTATION, classes = EnableWebMvc.class)})
@Configuration
@MapperScan({"cn.paul.dao","mapper"})
@ImportResource("classpath:spring-shiro-web.xml")
public class RootConfig {
    @Bean
    public DataSource dataSource() {
        return new ComboPooledDataSource();
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(new Resource[]{
                resolver.getResource("classpath:mapper/UserMapper.xml"),
                resolver.getResource("classpath:mapper/RoleMapper.xml"),
                resolver.getResource("classpath:mapper/PermissionMapper.xml")
        });
        return sqlSessionFactory;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

}

猜你喜欢

转载自blog.csdn.net/sinat_21372989/article/details/74741447