同一个坑掉进去两次,醉了,Invalid bound statement (not found):com.xxx。springboot+mybatis-plus映射不到xml文件

因为同一个掉进去两次所以记录一下。虽然这个问题有点。。。

开发工具用的eclipse ,idea没用过。

从官网下的spring-boot 2.1.3 demo 集成mybatis-plus 把配置配好后用soapUI请求接口的时候一直报错Invalid bound statement (not found):
虽然知道是xml文件没映射到,但就是找不到是哪里出问题(还是太菜了),
在这里插入图片描述
花了一天时间才找到出问题的地方。居然和之前的原因一样,我真是个憨憨,太粗心了。之前遇到这个同样的问题,爬了两天的坑,扫描的路径配错了,但是用的是spring-boot 1.5.11,后来我居然自信到怀疑是版本问题。。。然后就把版本改回1.5.11,结果还是500
在这里插入图片描述

上网搜了一下报Invalid bound statement (not found):大概的原因就以下几点:

  1. xml中的namespace ctrl+左键也可以链接进去 命名空间也没错
<mapper namespace = 
"com.ztkj.yundan.monitor.user.mapper.UserMapper">
  1. 在mapper接口中用@select注解是可以连接到数据库
 //查询用户信息//查询用户信息
	//@Select("select user_id from zt_user where 1=1")
	public User queryPwd(String userName);
  1. 在dao层加上@mapper注解也没用
    在这里插入图片描述
  2. 在MybatisPlusConfig 中basePackages的扫描路径配置了@MapperScan也配置了 // 我的问题出在sqlSessionFactory.setMapperLocations 扫描的资源文件路径不对因为这里配置了mapper-locations 遗漏了
package com.ztkj.yundan.conf;

import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.MybatisConfiguration;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.mapper.LogicSqlInjector;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages="com.ztkj.**.**.mapper")
public class MybatisPlusConfig {

    @Bean
    public PerformanceInterceptor performanceInterceptor () {
        return new PerformanceInterceptor();
    }
    
    //分页
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持
        return paginationInterceptor;
    }    

    //dzyd数据源
    @Bean(name = "dzyd")
    @ConfigurationProperties(prefix = "spring.datasource.dzyd" )
    public DataSource dzyd () {
        return DruidDataSourceBuilder.create().build();
    }
    //网关数据库
    /*@Bean(name = "gateway")
    @ConfigurationProperties(prefix = "spring.datasource.gateway" )
    public DataSource gateway () {
        return DruidDataSourceBuilder.create().build();
    }*/
    /**
     * 动态数据源配置
     * @return
     */
    @Bean
    @Primary
    public DataSource multipleDataSource (@Qualifier("dzyd") DataSource dzyd/*,
                                          @Qualifier("gateway") DataSource gateway */) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map< Object, Object > targetDataSources = new HashMap<>();
        targetDataSources.put(DBTypeEnum.dzyd.getValue(), dzyd );
       // targetDataSources.put(DBTypeEnum.gateway.getValue(), gateway);
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(dzyd);
        return dynamicDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(/*multipleDataSource(*/dzyd()/*,gateway())*/);
        //问题就是出在这里哦,资源文件加载包名写错了T_T
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/com/ztkj/**/mapping/*.xml"));

        MybatisConfiguration configuration = new MybatisConfiguration();
        //configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
                paginationInterceptor(),
                performanceInterceptor()
        });
        sqlSessionFactory.setGlobalConfig(globalConfiguration());
        return sqlSessionFactory.getObject();
    }
    //全局配置
    @Bean
    public GlobalConfiguration globalConfiguration() {
        GlobalConfiguration conf = new GlobalConfiguration(new LogicSqlInjector());
        conf.setLogicDeleteValue("-1");
        conf.setLogicNotDeleteValue("1");
        conf.setIdType(0);
        //conf.setMetaObjectHandler(new MyMetaObjectHandler());
        conf.setDbColumnUnderline(true);
        conf.setRefresh(true);
        return conf;
    }
}

  1. application.properties 里面配置的mapper-locations 路径貌似没用
//可能是因为MybatisPlusConfig中配置了MapperLocations 所以application.properties中的没生效
 #spring.datasource.dzyd.mapper-locations=classpath*:com/ztkj/**/**/mapping/*.xml

总结:据说pom.xml文件里面这个加载资源的配错了也会有这种错误但是我去掉这个resource> 配置项目 直接启动不了了
在这里插入图片描述
总之报这个org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 报错 如果不是namespace写错,基本上就是资源加载路径的问题了。
下次在遇到这个问题就解决的快了,呸!没有下次了。

在spirngmvc 中又遇到了这个问题。

spring mvc 和boot 还是有些不同

<!-- 4. 扫描Mapper,并将其生命周期纳入Spring的管理 (配置Mapper接口)-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	    <property name="basePackage" value="com.zt"/>
	    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

原因是这个basePackage路径需要指向mapper接口的父目录 有多个则需要这样配置

 <property name="basePackage" value="com.zt.general.mapper,
        	com.zt.system.mapper,
        	com.zt.industry.mapper,
        	com.zt.worksite.mapper"/>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值