• mybatis使用注解开发时报错:Type interface com.dao.UserMapper is not known to the MapperRegistry.


    第二次编辑:

    离大谱就,出现这个错的根本原因是我的加载映射文件写错了包的位置,本来加载映射已经在sqlMapConfig.xml文件中配置完了,但因为我配置的路径错误,导致我下一个查询时也出了很多错误,检查了一个早上,都没找到错误在哪,刚刚不知道咋又看了一遍,突然发现包路径错误

     之前写的包一直是com.mapper,但是基本的查询也能查询出来,是因为之前其他模块内都写了,直到使用复杂的就查不出来了,一直找不到原因

    劝诫:一定要看对自己的包位置!!!

    报错内容:

    org.apache.ibatis.binding.BindingException: Type interface com.dao.UserMapper 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:779)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:291)
        at com.test.MapperTest.before(MapperTest.java:32)

    项目目录:

     UserMapper接口:

    public interface UserMapper {
    
        @Insert("insert into user values (#{id},#{username},#{password},#{birthday})")
        public void save(User user);
    
        @Update("update user set username = #{username} ,password = #{password} where id = #{id}")
        public void update(User user);
    
        @Delete("delete from user where id = #{id}")
        public void delete(int id);
    
        @Select("select * from user where id = #{id}")
        public User findById(int id);
    
        @Select("select * from user")
        public List<User> findAll();
    }

    测试代码:

    public class MapperTest {
    
        private UserMapper mapper = null;
    
        private SqlSession sqlSession = null;
    
        @Before
        public void before() throws IOException {
            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //        sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);
            sqlSession = sqlSessionFactory.openSession(true);
            mapper = sqlSession.getMapper(UserMapper.class);
    
    
        }
    
        @Test
        public void save() throws IOException {
            User user = new User();
            user.setUsername("nihao");
            user.setPassword("123456");
            mapper.save(user);
        }
    
        @Test
        public void update(){
            User user = new User();
            user.setId(1);
            user.setUsername("zhanzhan");
            user.setPassword("fhg");
            mapper.update(user);
        }
    
        @Test
        public void delete(){
            mapper.delete(2);
        }
    
        @Test
        public void findById(){
            User user = mapper.findById(1);
            System.out.println(user);
        }
    
        @Test
        public void findAll(){
            List<User> userList = mapper.findAll();
            for (User user : userList) {
                System.out.println(user);
            }
        }
    
    
        @After
        public void after(){
            sqlSession.close();
        }
    
    }

    报错原因:

      写完接口类之后没有让mybatis注册这个类

    解决办法:

      在生成sqlSession工厂之后加入如下代码:

    sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);

  • 相关阅读:
    算术运算符
    短路运算
    基本运算符
    类型转换
    数据类型讲解
    关键字
    河北省重大技术需求征集八稿第六天
    河北省重大技术需求征集八稿第五天
    河北省重大技术需求征集八稿第四天
    河北省重大技术需求征集八稿第三天
  • 原文地址:https://www.cnblogs.com/jiliangqian/p/15722118.html
Copyright © 2020-2023  润新知