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

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

因为小编用的是最初的搭建方式,所以这里是以自己搭建工具类的形式来讲解:这是小编的目录结构:

整合SSM(基于MAVEN)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<>();/<sqlsession>

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();/<account>

public void saveAccount(Account account);

}

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

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<properties>

<environments>

<environment>

<transactionmanager>

<datasource>

<property>

<property>

<property>

<property>

<mappers> /<mappers>

<package>

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

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper>

<select>

select * from account

<insert>

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

5.创建测试类

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

AccountServiceImp asi = ctx.getBean(AccountServiceImp.class);

List<account> alist= asi.findAll();/<account>

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)/<init>

at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)/<init>

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

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

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

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

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

<build>

<resources>

<resource>

<directory>src/main/java/<directory>

<includes>

<include>**/*.xml/<include>

<include>**/*.properties/<include>

<filtering>true/<filtering>

<resource>

<directory>src/main/resources/<directory>

<includes>

<include>**/*.xml/<include>

<include>**/*.properties/<include>

<filtering>true/<filtering>

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不太熟练才踩了这个坑呐!!!到时候一定要回头重新学习!!!

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



分享到:


相關文章: