idea的spring整合基于xml文件配置的mybatis报Invalid bound statement (not found): com.music.dao.MusicDao.findAll的问题

一、

  题主当时就是自己尝试整合spring和mybatis的时候遇到了这个问题,当时题主只看到了用注解的方式配置的dao层,题主用的是xml文件配置的形式,

  而且坑爹的是题主的两个文件的路径写的也不一致,就导致直接用<property name="basePackage" value="com.music.dao"></property> 导致绑定异常

后来在网上查到了解决的办法 ,就是如果路径一致,(如果一致你也就不会来看到本文了),

两个目录路径不一致的话,就代码👇

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置扫描器  但是只让他扫描service层和dao层 无需扫描注解-->
    <context:component-scan base-package="com.music">
        <!--exclude不扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--整合mybatia和spring-->
    <!--配置连接池-->
    <!--配置数据源 DriverManagerDataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///song?serverTimezone=GMT%2B8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--配置sqlsessionFactory的工厂-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factoryBean">
         <property name="dataSource" ref="dataSource"></property>
     <!-- ****************目录不一致的情况 要在这里添加这没一条配置******************************************** -->
     <!-- *通配符来匹配 **任意文件夹下的 *任意以xml结尾的文件 -->
     <property name="mapperLocations" value="classpath:com/music/**/*.xml"></property>
     <!-- ************************************************************ -->
</bean> <!--配置dao下的接口 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="MapperScannerConfig"> <property name="basePackage" value="com.music.dao"></property> </bean> </beans>

网上的官方解释文档:http://mybatis.github.io/spring/factorybean.html

posted @ 2019-12-14 10:40  少侠砍人不用刀  阅读(746)  评论(0编辑  收藏  举报