mybatis异常:Invalid bound statement (not found)

首先分析出现这种原因的问题,是找不到接口实现对应的xml文件。

如果把xml文件放在src/main/java下就会找不到,

因为会自动在resource里面找,所以应该把mapper文件夹和该文件夹下的xml文件放入resource目录下,是剪切过去不要拷贝过去。

 

同时,注意在“”双引号里面写路径要用转义字符//而不是\,否则路径不识别还是会报错,下面是我的项目的目录结构:

mybatis异常:Invalid bound statement (not found)

 

这个是我的config类代码,注意里面的路径的写法:

package com.lqp.demo_mybatis_generator.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.lqp.demo_mybatis_generator.dao")
public class MyBatisConfig {
    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() throws Exception{
        Properties prop = new Properties();
        prop.put("driverClassName",env.getProperty("test.jdbc.driver-class-name"));
        prop.put("url",env.getProperty("test.jdbc.url"));
        prop.put("username",env.getProperty("test.jdbc.username"));
        prop.put("password",env.getProperty("test.jdbc.password"));
        return DruidDataSourceFactory.createDataSource(prop);
    }


    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception{
        SqlSessionFactoryBean fb=new SqlSessionFactoryBean();
        fb.setDataSource(ds);//指定数据源
        fb.setTypeAliasesPackage("com.lqp.demo_mybatis_generator.domain");//指定基包
       // System.out.println(""+new PathMatchingResourcePatternResolver().getResources("classpath:../mapper/*.xml"));
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper\\*.xml"));//指定xml文件路径
        return fb.getObject();
    }
}