mybatis配置執行報出Caused by: org.apache.ibatis.binding.BindingException:相關錯誤解決或者空指標異常

終生學習code發表於2020-10-18

問題一報出Type interface com.example.springboot.mappers.AyUserMapper is already known to the MapperRegistry.

Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.binding.BindingException: Type interface com.example.springboot.mappers.AyUserMapper is already known to the MapperRegistry.

	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
	at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
	
	報出Type interface com.example.springboot.mappers.AyUserMapper is already known to the MapperRegistry.
	意思是說MapperRegistry. mapper登錄檔應經知道這個mapper已經註冊
	那已經註冊為啥會報錯,我搜尋很多相關就兩個解決方案
	
	方案一
	看mybatis-config配置中和對應mapper.xml檔案中名稱空間namespace是否一樣
	<mappers>
        <mapper resource="com/example/springboot/mappers/AyUserMapper.xml"></mapper>
    </mappers>
    UserInfoMapper.xml
    <mapper namespace="com.example.springboot.mappers.AyUserMapper"></mapper>
    我使用idea   namespace
    修改成<mapper namespace="com/example/springboot/mappers/AyUserMapper"></mapper>
    就成功執行。
	

問題二 執行直接報出空指標異常

問題可能出現在src/mian/java相關路徑下建立mappper.xml檔案
在這裡插入圖片描述
程式執行時候mybatis-scan 並不會掃描java包下的xml檔案打包亦不會打包
解決方案(加入下面程式碼讓程式進行載入掃描java包下xml檔案)

     <build>    
         <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

方案二就是將xml檔案建立在resource目錄下然後在mybatis-config配置下改成(可能這個方法不一定行)

<mappers>
        <mapper resource="mappers/AyUserMapper.xml"></mapper>
    </mappers>

問題三mybatis mapper介面報出org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.springboot.mappers.AyUserMapper.findNameById

解決方案
請參照解決方案內容很詳盡

相關文章