Type interface XXXMapper is not known to the MapperRegistry

编程入门 行业动态 更新时间:2024-10-08 04:29:46

Type <a href=https://www.elefans.com/category/jswz/34/1768718.html style=interface XXXMapper is not known to the MapperRegistry"/>

Type interface XXXMapper is not known to the MapperRegistry

最近做项目中报了个错,搞了几个小时才解决,在这里记录下来,以免下次报错的时候在那浪费时间
Type interface XXXMapper is not known to the MapperRegistry
字面意思就是说你用到了这个XXXMapper,然而它没有被注册
网上的说法都是说Mapper xml中的namespace对应的实体类的路径不对,显然在这里不是这个问题
通过debug发现了问题,你先debug到sqlSessionFactory:
查看sqlSessionFactory>>configuration>>mapperRegistry>>knownMappers这时下面会显示已经注册的mapper文件,查看是不是与你的mybatis.xml中设置的一致,这时发现是不一致的
原因如下:
我这里做的项目是maven项目,依赖了两个子工程,a_dao,b_dao,两个dao分别指向不同的数据库
连接数据库的DBUtils都是
public class DBUtils {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return
* @描述:获取SqlSession
*/
public static final SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
这里有两个问题,
1.由于两个子工程的DBUtils 的路径是一致的,都是在这package com.xxx.xx.utils包下,这样工程也不知道引入哪个DBUtils ,可能就引错了,所以这时需要修改其中一个名称为DBUtil

注意:实际上如果不改也是有问题的,因为打包后,两个同名的文件都会放在打包后的com/xxx/xx/utils这个目录下,这是有问题的...

2.第二修改一下dao工程中的mybatis.xml,可以一个为mybatis.xml,一个为mybatis-config.xml,这样名称不一样,在引入的时候也不会搞错

 

转载于:.html

更多推荐

Type interface XXXMapper is not known to the MapperRegistry

本文发布于:2024-02-06 13:43:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1749682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:interface   Type   MapperRegistry   XXXMapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!