本篇将重点分析加载mapper.xml的路径问题。
以上是MyBatis最简单的例子,从数据库查找id=10的记录。 如果不懂MyBatis执行流程,请参照前面的博文“MyBatis系列学习—《MyBatis执行流程》”。
加载核心配置文件
sqlMapConfig.xml 内容如下所示:
<configuration>
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/smartshop?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- Mapper的位置 Mapper.xml 写Sql语句的文件的位置 -->
<mappers>
<mapper resource="cn/mapper/BrandMapper.xml"/>
</mappers>
</configuration>
BrandMapper.xml 内容如下:
<!-- 写Sql语句 -->
<mapper namespace="test">
<!-- 通过ID查询一个用户 -->
<select id="findBrandById" parameterType="Integer" resultType="cn.bean.product.Brand">
select * from bbs_brand where id = #{v}
</select>
</mapper>
接下来将分析如何找到cn/mapper/BrandMapper.xml
1. 查看JAVA项目的编译目录
- 右键项目 -> 属性
- Java Build Path -> Source -> Default output folder
在项目smartshopLocal中,build目录就是以上所配置的output目录,build目录与src目录一一对应
2. getResourceAsStream() 分析
getResourceAsStream()加载的resource 以build为根目录。sqlMapConifg.xml位于src/config,对应于build中目录为config。
因此,resouce=“config/sqlMapConfig.xml”。
3. BrandMapper.xml
test.findBrandById 来自BrandMapper.xml。BrandMapper.xml位于src/cn/mapper,在build中对应为cn/mapper。
因此,sqlMapConfig.xml中BrandMapper.xml配置为:
本篇小结
以上代码运行逻辑如下图所示:
在运行过程中因路径配置问题报错如下,贴出来和大家一起学习分享。
org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for test.findBrandById
Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for test.findBrandById
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
at cn.bean.junit.smartshopJunit.testMybatis(smartshopJunit.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for test.findBrandById
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:933)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:726)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:719)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)