Mybatis学习记录,Target下的classes目录会生成两个同样的包

Maven的resources目录创建文件夹

目录结构如下图:

Mybatis学习记录,Target下的classes目录会生成两个同样的包_第1张图片

mapper映射配置:

<mappers>
        <mapper resource="com/test/dao/UserMapper.xml">mapper>
mappers>

进入正题:

学习Mybatis时,在resources目录下创建了com.test.dao这个包目录(自己以为成功创建了包目录)

然后在启动测试类的时候报了异常:

java.lang.ExceptionInInitializerError
	at TestDemo.getUserMapper(TestDemo.java:23)
...
Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/test/dao/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/test/dao/UserMapper.xml
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
	at com.test.utils.MybatisUtil.<clinit>(MybatisUtil.java:24)
	... 24 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/test/dao/UserMapper.xml
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
	... 26 more
Caused by: java.io.IOException: Could not find resource com/test/dao/UserMapper.xml
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
	at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:371)
	at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
	... 28 more


java.lang.NullPointerException ...

Process finished with exit code -1

当然,异常无非就是说我的包映射路径不对,系统找不到,我反复点开包查看,感觉没错啊??

在打开target的classes目录时,让我产生了困惑,我的java和resources目录下都是同样的包,为什么产生了两份??

结果发现,在resources下创建包时,不能直接将包名命名为“com.test.dao”,否则会直接将“com.test.dao”当成当前一级包名,而不是一个层级目录的包名!

后来重新在resources下创建包(需要一级一级的创建),先创建com包,然后基于com包创建test包,再基于test包创建dao包,将XxxMapper.xml文件放于dao包内,再次测试,发现运行成功,问题解决。

重新创建后的包路径:

Mybatis学习记录,Target下的classes目录会生成两个同样的包_第2张图片

执行成功!

D:\java\Java-8\bin\java.exe ...
update success...

Process finished with exit code 0

你可能感兴趣的:(Mybatis学习记录,java,maven,开发语言,mybatis)