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)類型接口沒(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)映射的類名是否正確。
- 在確認(rèn)包路徑使用的
.
點(diǎn)(com.*.*.Object
)而不是/
左斜杠(com/*/*/Object
)或\
右斜杠(com\*\*\Object
),如果錯(cuò)誤請(qǐng)修改為.
點(diǎn)。 - 如果鼠標(biāo)懸浮到路徑上,顯示下劃線說(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)題解決。
最開始看官網(wǎng)給的代碼片段,以為可以去掉addMapper的步驟,后來(lái)才想起不注冊(cè)的話怎么獲取呢?嘖~還是太粗心了呀?。?!
到此這篇關(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)文章
idea插件之mybatis log plugin控制臺(tái)sql的問(wèn)題
這篇文章主要介紹了idea插件之mybatis log plugin控制臺(tái)sql,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java使用DateFormatter格式化日期時(shí)間的方法示例
這篇文章主要介紹了Java使用DateFormatter格式化日期時(shí)間的方法,結(jié)合具體實(shí)例分析了java使用DateFormatter格式化日期時(shí)間的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04SpringCloud之Zuul網(wǎng)關(guān)原理及其配置講解
這篇文章主要介紹了SpringCloud之Zuul網(wǎng)關(guān)原理及其配置講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Servlet關(guān)于RequestDispatcher的原理詳解
這篇文章主要介紹了Servlet關(guān)于RequestDispatcher的原理詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼
這篇文章主要介紹了java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04詳解tryAcquire()、addWaiter()、acquireQueued()
這篇文章主要tryAcquire()、addWaiter()、acquireQueued()的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03