org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)总结

  • org.apache.ibatis.binding.BindingException: Invalid bound statement (not
    found)总结

今天做ssm项目整合是,遇见了 org.apache.ibatis.binding.BindingException: Invalid bound statement (not
found)的异常,但是奇怪的是在idea中添加单元测试能正常查出结果,代码如下:

 @Test
    public void Test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application-context.xml");
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        UserService userService = (UserService) applicationContext.getBean("userServiceImpl");
        UserVo user = new UserVo();
        user.setPwd("e10adc3949ba59abbe56e057f20f883e");
        user.setLoginname("admin");
        User login = userMapper.login(user);
        User user1 = userMapper.selectByPrimaryKey(1);
        System.out.println(login.toString());
        System.out.println(user1.toString());
        System.out.println(userMapper);
        System.out.println(userService);
    }

但是一到页面提交,就报上述错误。百思不得其解。最后发现,还是路径的问题,mapper.xml资源无法找到。

第一:我们在用单元测试是能找到,并且mapper.xml写在了src目录下,而不是classpath下,我们需要添加:
<build>
	<resources>
      <resource>
        <directory>src/main/java</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
 </build>

这样,idea中的maven项目就能读取src目录下的配置文件了。

第二:我们表单提交数据后,又出现了上述错误。

为什么测试时能访问到,但是表单提交数据就访问不到呢。

原因:

我们时web项目,在运行时已经被打成war包,所以我们更改配置文件后一定要重新生成一下war包,利用maven的攻击clean先清理一下。

我开始寻找mapper.xml的路径:

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations">
            <array>
                <value>com/ailine/sys/mapping/*Mapper.xml</value>
                <value>com/ailine/bus/mapping/*Mapper.xml</value>
            </array>
        </property>
<bean>

这种情况,在打成war包后,他已经在类路径下了,所以web此路径。写成如下:

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations">
            <array>
                <value>classpath:com/ailine/sys/mapping/*Mapper.xml</value>
                <value>classpath:com/ailine/bus/mapping/*Mapper.xml</value>
            </array>
        </property>
<bean>

完美搞定

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值