Mybatis報(bào)Type interface *.*Mapper is not known to the MapperRegis
問(wèn)題發(fā)現(xiàn)
在學(xué)習(xí)MyBatis框架的時(shí)候,不使用 XML 構(gòu)建 SqlSessionFactory,調(diào)用Mapper的接口,報(bào)類(lèi)型接口沒(méi)有注冊(cè)。
示例代碼如下:
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();
// 關(guān)閉 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>
網(wǎng)上說(shuō)都是命名空間的錯(cuò)誤,發(fā)現(xiàn)命名空間也沒(méi)問(wèn)題。然后進(jìn)行一系列的問(wèn)題排查。
問(wèn)題解決
方法一:檢查Mapper文件的namespace路徑是否正確
這也是網(wǎng)上解決方法最多的一種,貌似大部分人都是遇到這種錯(cuò)誤。
- 先確認(rèn)映射的類(lèi)名是否正確。
- 在確認(rèn)包路徑使用的
.點(diǎn)(com.*.*.Object)而不是/左斜杠(com/*/*/Object)或\右斜杠(com\*\*\Object),如果錯(cuò)誤請(qǐng)修改為.點(diǎn)。 - 如果鼠標(biāo)懸浮到路徑上,顯示下劃線(xiàn)說(shuō)明配置正確。
如圖所示

方法二:使用其他方法是否正確
換一種方法試試,SqlSession 提供了在數(shù)據(jù)庫(kù)執(zhí)行 SQL 命令所需的所有方法。你可以通過(guò) SqlSession 實(shí)例來(lái)直接執(zhí)行已映射的 SQL 語(yǔ)句。示例代碼如下:
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");
// 關(guān)閉 SqlSession
sqlSession.close();
}
}
運(yùn)行成功,如圖所示

然后逐個(gè)代碼排除,根據(jù)錯(cuò)誤提示(未注冊(cè))發(fā)現(xiàn)少了configuration.addMapper()方法的代碼,加上后,運(yùn)行成功。
示例代碼如下
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();
// 關(guān)閉 SqlSession
sqlSession.close();
}
}
問(wèn)題解決。
最開(kāi)始看官網(wǎng)給的代碼片段,以為可以去掉addMapper的步驟,后來(lái)才想起不注冊(cè)的話(huà)怎么獲取呢?嘖~還是太粗心了呀?。?!
到此這篇關(guān)于Mybatis報(bào)Type interface *.*Mapper is not known to the MapperRegis的文章就介紹到這了,更多相關(guān)Mybatis報(bào)Type interface內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot接口參數(shù)校驗(yàn)(Bean Validation)實(shí)戰(zhàn)指南
在開(kāi)發(fā)接口時(shí),參數(shù)校驗(yàn)是必不可少的環(huán)節(jié):前端傳參是否為空、格式是否正確、數(shù)值是否合法,都需要后端嚴(yán)格校驗(yàn),否則很容易出現(xiàn)臟數(shù)據(jù)、程序異常,SpringBoot 官方推薦使用?Bean Validation,今天就來(lái)介紹一下基礎(chǔ)注解、實(shí)戰(zhàn)使用,需要的朋友可以參考下2026-03-03
Mybatisplus集成springboot完成分頁(yè)查詢(xún)功能(示例代碼)
今天小編給大家分享Mybatisplus集成springboot完成分頁(yè)查詢(xún)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
Java數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)
線(xiàn)性表(linear?list)是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列。順序表是常見(jiàn)的線(xiàn)性表之一,本文將詳細(xì)講講順序表的原理與實(shí)現(xiàn),需要的可以參考一下2022-08-08
Mybatis-plus如何查詢(xún)返回對(duì)象內(nèi)有List<String>屬性
在使用Mybatis-Plus進(jìn)行開(kāi)發(fā)時(shí),我們經(jīng)常會(huì)遇到需要處理一對(duì)多關(guān)系映射的情況,例如,查詢(xún)用戶(hù)數(shù)據(jù)時(shí),可能需要同時(shí)獲取該用戶(hù)管理的所有小區(qū)名稱(chēng)列表,這要求我們?cè)诜祷氐膶?shí)體類(lèi)中包含一個(gè)List<String>屬性,用于存放小區(qū)名稱(chēng),實(shí)現(xiàn)這一功能2024-10-10
關(guān)于java關(guān)鍵字this和super的區(qū)別和理解
這篇文章主要給大家介紹了關(guān)于java關(guān)鍵字this和super的區(qū)別和理解的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringCloud分布式事務(wù)Seata部署和集成過(guò)程
這篇文章主要介紹了SpringCloud分布式事務(wù)Seata部署和集成過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10
Spring?Boot?3.4.3?基于?Spring?WebFlux?實(shí)現(xiàn)?SSE?功能(代碼示例)
Spring Boot 3.4.3 結(jié)合Spring WebFlux實(shí)現(xiàn)SSE 功能,為實(shí)時(shí)數(shù)據(jù)推送提供了優(yōu)雅的解決方案,通過(guò)本文的步驟,你可以快速搭建一個(gè)基于事件驅(qū)動(dòng)的后端服務(wù),滿(mǎn)足實(shí)時(shí)通知或監(jiān)控等需求,感興趣的朋友一起看看吧2025-04-04

