springBoot整合mybatis使用xml文件的方式,出现的错误

来源:13-4 mybatis xml方式使用

Draven_is_good

2017-12-25

http://img.mukewang.com/szimg/5a4078f30001035013980859.jpg

http://img.mukewang.com/szimg/5a4078f30001f36113980859.jpg

我是一步一步按照您的视频上的步奏来写的,最开始使用注解的方式是没有报错的,后来使用xml文件的方式,包括pom.xml(添加mybatis依赖),mapper.java(接口的方法),mapper.xml(写的一些sql),配置扫描mapper的路径(@MapperScan),application.yml(配置mybatis.config-location找到mabatis的xml的配置文件,就是这儿出的错),后来运行测试用例的时候,就出问题了;后来不管是注解的方式还是xml的方式都报错了,之前测试注解是能够跑通的;当我删掉application.yml中的config-location后,注解的方式还是行的通;这就是我操作的大致步奏,如果mybatis是报的sql语句不对什么的,那个我就不会麻烦您了,自己百度得到。希望您有空能解答我的问题,给您带来的不便,还请包涵。
Caused by: java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productCatetoryMapper' defined in file [E:\idea_projects_2\sell\target\classes\com\imooc\demo\dataobject\mapper\ProductCatetoryMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist


写回答

3回答

廖师兄

2017-12-25

图片太模糊,我这边看不清楚了。我给2个建议

  1. 注解方式和xml方式,先不要混合起来用。

  2. 对比我的源码,甚至直接运行我的源码试试

1
1
Draven_is_good
非常感谢!
2017-12-26
共1条回复

Draven_is_good

提问者

2017-12-26

首先,我不是一有问题就来问廖师兄的(因为我感觉这个好家伙应该很忙),都是先百度去的,这个错误我弄了一个下午和一个小晚上的,弄的头昏眼花才来问的。后来自己解决了,是application.yml配置文件的错误,mybatis.config-location替换成mybatis.mapper-location就行了,的确是个小问题,不过自己的感触是真的蛮深的。。。

1
1
廖师兄
我刚刚看了下,我给的源码里,包括视频里,用的就是mybatis.mapper-location https://gitee.com/liaoshixiong_spring/sell/blob/13-4/src/main/resources/application.yml 你是用哪看到的用mybatis.config-location
2017-12-26
共1条回复

No_7479

2019-09-24

是IDEA吗?看下你的项目打包出来代码是否有mapper/*.xml配置文件,没有要在pom.xml配置

可以参照下面

https://blog.csdn.net/Angel_Holy/article/details/98081308

0
0

Spring Boot双版本(1.5/2.1) 打造企业级微信点餐系统

从0到1开发中小型企业级Java应用,并学会迭代重构技巧

6410 学习 · 5247 问题

查看课程