今天做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资源无法找到。
这样,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>完美搞定