解决Mybatis报Type interface *.*Mapper is not known to the MapperRegis
- 问题发现
- 问题解决
- 方法一:检查Mapper文件的namespace路径是否正确
- 方法二:使用其他方法是否正确
问题发现
在学习MyBatis框架的时候,不使用 XML 构建 SqlSessionFactory,调用Mapper的接口,报类型接口没有注册。
示例代码如下:
public class Test {public static void main(String[] args) throws IOException {DataSource dataSource = new PooledDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/spring_data?characterEncoding=utf-8&useSSL=false", "root", "123456");TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(environment);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);List<User> users = userMapper.selectByUser();// 关闭 SqlSessionsqlSession.close();}
}
public interface UserMapper {List<User> selectByUser();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisstudy.dao.UserMapper"><select id="selectByUser" resultType="com.example.mybatisstudy.User">select * from user;</select>
</mapper>
网上说都是命名空间的错误,发现命名空间也没问题。然后进行一系列的问题排查。
问题解决
方法一:检查Mapper文件的namespace路径是否正确
这也是网上解决方法最多的一种,貌似大部分人都是遇到这种错误。
- 先确认映射的类名是否正确。
- 在确认包路径使用的
.
点(com.*.*.Object
)而不是/
左斜杠(com/*/*/Object
)或\
右斜杠(com\*\*\Object
),如果错误请修改为.
点。 - 如果鼠标悬浮到路径上,显示下划线说明配置正确。
如图所示
方法二:使用其他方法是否正确
后面切换到,使用XML 中构建 SqlSessionFactory方法,示例代码如下:
public class Test {public static void main(String[] args) throws IOException {DataSource dataSource = new PooledDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/spring_data?characterEncoding=utf-8&useSSL=false", "root", "123456");TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(environment);configuration.addMapper(UserMapper.class);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);SqlSession sqlSession = sqlSessionFactory.openSession();List<User> selectByUser = sqlSession.selectList("selectByUser");// 关闭 SqlSessionsqlSession.close();}
}
运行成功,如图所示
然后逐个代码排除,根据错误提示(未注册)发现少了configuration.addMapper()
方法的代码,加上后,运行成功。
示例代码如下
public class Test {public static void main(String[] args) throws IOException {DataSource dataSource = new PooledDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/spring_data?characterEncoding=utf-8&useSSL=false", "root", "123456");TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(environment);configuration.addMapper(UserMapper.class);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);SqlSession sqlSession = sqlSessionFactory.openSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<User> users = mapper.selectByUser();// 关闭 SqlSessionsqlSession.close();}
}
问题解决。
最开始看官网给的代码片段,以为可以去掉addMapper的步骤,后来才想起不注册的话怎么获取呢?啧~还是太粗心了呀!!!