整合SSM(基于MAVEN)Invalid bound statement (not found) 详解

今天小编在整合SSM框架的时候,选择的是MAVEN项目,基于wepapp骨架,在配置mybatis的过程中出现了mapper文件映射不到xml文件中的错误(Invalid bound statement (not found) ),下面小编详细解释这个过程

因为小编用的是最初的搭建方式,所以这里是以自己搭建工具类的形式来讲解:这是小编的目录结构:
在这里插入图片描述
资源文件类为resources文件夹目录下


1.创建SqlSessionUtils工具类

public class SqlSessionUtils {

    private static SqlSessionFactory factory = null;

    static {
        try {
            //  1.获得字节输入流
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            //  2. 获取工厂
            factory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static ThreadLocal<SqlSession> sessionThreadLocal = new ThreadLocal<>();

    public static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        sqlSession = sessionThreadLocal.get();
        if(sqlSession == null){
            sqlSession = factory.openSession();
            sessionThreadLocal.set(sqlSession);
        }
        return sqlSession;
    }

    /**
     * 一定要记得归还线程之前清空线程
     * @param sqlSession
     */
    public static void MyClose(SqlSession sqlSession){
        if(sqlSession != null){
            sqlSession.close();
            sessionThreadLocal.remove();
        }
    }

}

2.创建SsmDao类


public interface SsmDao {

    public List<Account> findAll();

    public void saveAccount(Account account);

}

3.创建mybatis-config.xml配置文件(在Resources资源文件下)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <properties resource="druid.properties"></properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>     
      <package name="cn.itcast.dao"/>
    </mappers>
</configuration>

4.创建SsmDao.xml配置文件(这里注意,必须要跟接口在同一目录下,并且名称要跟接口一致!!!!否则也会抛出这个异常!!!!这个坑要注意了!!)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- 使用接口实现-->

<mapper namespace="cn.itcast.dao.SsmDao">

    <select id="findAll" resultType="cn.itcast.domain.Account">

        select * from account

    </select>
    
    <insert id="saveAccount">

        insert into account (name,money) values (#{name},#{money})
    </insert>

</mapper>

5.创建测试类

 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountServiceImp asi = ctx.getBean(AccountServiceImp.class);
        List<Account> alist= asi.findAll();
        for (Account account : alist) {
            System.out.println(account);
        }

6.这里看起来一切都很正常并且很顺利的样子,但是,我们还是看到了如下异常:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.itcast.dao.SsmDao.findAll

	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225)
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
	at com.sun.proxy.$Proxy21.findAll(Unknown Source)
	at cn.itcast.service.Imp.AccountServiceImp.findAll(AccountServiceImp.java:15)
	at cn.itcast.test.TestSSM.test1(TestSSM.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		.....
Process finished with exit code -1

看到这里这个错误,小编当时想到很多可能性:

1可能是xml文件所在package名称和Mapper interface所在的包名 不一致,或者是mapper的namespace写的不对 (namespace一定要写src下的全路径!!!)
2可能是SsmDao的方法在SsmDao.xml中没有
2可能是SsmDao的方法在SsmDao.xml中没有
3SsmDao的方法返回值是List < Account> ,可能是select元素没有正确配置ResultType
4可能是方法名后面加了一个空格(有的喜欢敲空格的朋友们注意了)

经过验证…以上猜想全都不是 , 后来小编就查阅了很多资料,但是都跟小编提到的差不多,但是小编非常确认自己写的没有问题,后来换了个思路,查询的是MAVEN项目resources和java之间的关系,是不是存在包没扫描到,配置文件没有加载等等情况…,后来小编终于找到解决方式了,问题出在maven中的< build > 配置中,需要添加一些代码…

7. 解决方式:修改pom.xml文件

<build>
 <resources>
          <resource>
              <directory>src/main/java</directory>
              <includes>
                  <include>**/*.xml</include>
                  <include>**/*.properties</include>
              </includes>
              <filtering>true</filtering>
          </resource>
          <resource>
              <directory>src/main/resources</directory>
              <includes>
                  <include>**/*.xml</include>
                  <include>**/*.properties</include>
              </includes>
              <filtering>true</filtering>
          </resource>
      </resources>
        </build>

8.再次运行测试代码:

输出结果:
Account{id=1, name=‘ZhangSan’, money=2000.0}
Account{id=2, name=‘WangWu’, money=1750.0}
Account{id=3, name=‘LiuShun’, money=1850.0}
Account{id=4, name=‘MaKe’, money=1850.0}

总结:如果我们的mapper.xml文件没有放置到src-main-resources下面,是不会被maven build plugin给默认扫描到的。此时需要修改启动的模块的pom文件,在build标签里面加入。小编对maven不太熟练才踩了这个坑呐!!!到时候一定要回头重新学习!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值