Mybatis报Type interface *.*Mapper is not known to the MapperRegis
作者:成为大佬先秃头
本文主要介绍了Mybatis报Type interface *.*Mapper is not known to the MapperRegis,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
问题发现
在学习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(); // 关闭 SqlSession sqlSession.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
),如果错误请修改为.
点。 - 如果鼠标悬浮到路径上,显示下划线说明配置正确。
如图所示
方法二:使用其他方法是否正确
换一种方法试试,SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。示例代码如下:
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"); // 关闭 SqlSession sqlSession.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(); // 关闭 SqlSession sqlSession.close(); } }
问题解决。
最开始看官网给的代码片段,以为可以去掉addMapper的步骤,后来才想起不注册的话怎么获取呢?啧~还是太粗心了呀!!!
到此这篇关于Mybatis报Type interface *.*Mapper is not known to the MapperRegis的文章就介绍到这了,更多相关Mybatis报Type interface内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!