mybatis使用注解的时候,找不到映射:Type interface com.dao.UserDao is not known to the MapperRegistry.

在使用 MyBatis 注解时遇到错误:找不到映射 Type interface com.dao.UserDao is not known to the MapperRegistry。尽管已在核心配置文件中指定接口路径,问题依旧存在。解决方案是在测试类启动代码中添加 factory.getConfiguration().addMapper(UserDao.class) 这一行,以注册注解文件,从而成功运行程序。
部署运行你感兴趣的模型镜像

今天按照视频来一步一步敲完之后,发现程序没有办法识别注解文件在哪个位置
但是我在mybatis核心配置文件中,已经说明了接口文件所在位置了,困扰了很久,最后终于找到答案;

<!--    指定接口所在位置-->
    <mappers>
        <package name="com.dao.UserDao"/>
    </mappers>

在核心配置文件中已经声明;

再看测试类中的启动代码

    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

		//非常需要注意下面这一行代码,用来注册注解文件
       // factory.getConfiguration().addMapper(UserDao.class);
        SqlSession sqlSession = factory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> users = userDao.findAll();
        for ( User user : users){
            System.out.println(user);
        }
        sqlSession.close();
        inputStream.close();

    }

这是原来的启动文件,使用它就会爆出错误:

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.dao.UserDao is not known to the MapperRegistry.
	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:745)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:292)
	at com.MybatisTest.main(MybatisTest.java:29)

Process finished with exit code 1

原来必须要添加上这一行代码:!!!!!!!!!
factory.getConfiguration().addMapper(UserDao.class);
用来注册注解文件

即:

    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);


        factory.getConfiguration().addMapper(UserDao.class);
        SqlSession sqlSession = factory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> users = userDao.findAll();
        for ( User user : users){
            System.out.println(user);
        }
        sqlSession.close();
        inputStream.close();

    }

最终成功运行:

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 CST 2018, sex='男', address='北京'}
User{id=42, username='小二王', birthday=Fri Mar 02 15:09:37 CST 2018, sex='女', address='北京金燕龙'}
User{id=43, username='小san王', birthday=Sun Mar 04 11:34:34 CST 2018, sex='女', address='北京金燕龙'}
User{id=45, username='传智播客', birthday=Sun Mar 04 12:04:06 CST 2018, sex='男', address='北京金燕龙'}
User{id=46, username='老王', birthday=Wed Mar 07 17:37:26 CST 2018, sex='男', address='北京'}
User{id=48, username='小马宝莉', birthday=Thu Mar 08 11:44:00 CST 2018, sex='女', address='北京修正'}
User{id=54, username='nananna', birthday=Sat Aug 21 18:29:06 CST 2021, sex='n', address='666'}
User{id=55, username='nananna', birthday=Sun Aug 29 12:40:45 CST 2021, sex='n', address='666'}

Process finished with exit code 0

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

`org.apache.ibatis.binding.BindingException: Type interface com.itheima.sh.pojo.dao.DishMapper is not known to the MapperRegistry` 错误表示 MyBatis映射器注册中心不知道 `com.itheima.sh.pojo.dao.DishMapper` 接口类型,通常是由于核心配置文件加载映射文件包名写错或配置文件路径问题导致的 [^1]。以下是可能的解决方法: #### 检查核心配置文件中映射文件的包名 核心配置文件(如 `mybatis-config.xml` 或 Spring Boot 项目中的 `application.yml` 或 `application.properties`)中,映射文件的包名要和 `DishMapper` 接口的包名一致 [^1]。 在 `mybatis-config.xml` 中,示例如下: ```xml <mappers> <package name="com.itheima.sh.pojo.dao"/> </mappers> ``` 在 Spring Boot 项目的 `application.yml` 中,示例如下: ```yaml mybatis: mapper-locations: classpath:com/itheima/sh/pojo/dao/*.xml ``` #### 检查接口名和映射文件名字是否一致 `DishMapper` 接口和对应的映射文件(如 `DishMapper.xml`)的名字要一致,并且映射文件要放在正确的路径下 [^4]。 #### 检查映射文件路径是否正确 确保映射文件的路径在配置文件中正确指定,如上述 `application.yml` 中 `mapper-locations` 的配置。 #### 检查类路径 确保 `DishMapper` 接口和对应的映射文件在类路径下,这样 MyBatis 才能到它们。 ### 示例代码 以下是一个简单的 Spring Boot 项目中 MyBatis 配置的示例: ```yaml mybatis: # 指定接口映射文件的位置 mapper-locations: classpath:com/itheima/sh/pojo/dao/*.xml # 为 pojo 类指定别名 type-aliases-package: com.itheima.sh.pojo.entity ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值