「タイプインターフェイスはMapperRegistryに知られていません」MyBatisを使用した例外
質問
注釈を使用してMyBatisをセットアップし、この役立つ例外を取得しています
org.apache.ibatis.binding.bindingexception:タイプインターフェイスorg.foo.barはmapperregistryに知られていません
グーグルしています 何も見つかりませんし、ユーザーガイドも見つかりません。何が足りないの?
解決 3
わかりました - これは、私が構成にXMLファイルを使用していたために起こっています。マッパー自体に注釈が使用されていました - そして、MyBatisはXML構成を使用するときにマッパー注釈を見つけません。
これを参照してください フォローアップの質問.
他のヒント
Mybatisに慣れていないのでここにたどり着いた人のためだけにhttp://www.mybatis.org/core/configuration.html
http://www.mybatis.org/mybatis-3/configuration.html
構成ファイルマッパーセクション
<mappers>
<mapper class="my.package.com.MyClass"/>
</mappers>
これにより、config.xmlと注釈付きインターフェイスを使用して稼働します
追加 マッパー SQLSessionFactory構成へのクラスこれは次のとおりです。
SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(reader);
//very import
factory.getConfiguration().addMapper(BarMapper.class);
SqlSession sqlSession = factory.openSession();
mapper.xmlファイルでは、Mapperの名前空間はマッパーインターフェイスへのパスである必要があります。
例えば:
<mapper namespace="com.mapper.LineMapper">
<select id="selectLine" resultType="com.jiaotong114.jiaotong.beans.Line">
select * from bus_line where id = #{id}
</select>
</mapper>
マッパーインターフェイスはcom.mapperパッケージにある必要があり、その名前はLinemapperです。
mapper.xmlファイルが誤った名前空間を使用している可能性があります(おそらくコピーペーストエラーが原因です)。
たとえば、javaインターフェイスがあると言ってみましょう MyEntityMapper.java
それ したほうがいい MyBatis Mapper XML Config Callingにリンクします MyEntityMapper.xml
:
myentitymapper.java
package my.mappers;
public interface MyEntityMapper {
MyEntity getById(@Param("id") String id);
}
myentitymapper.xml
<?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="non.existent.package.NonExistentMapper">
<resultMap id="MyEntityResultmap" type="MyEntity">
<!-- some result map stuff here -->
</resultMap>
<select id="getByUuid" resultMap="MyEntityResultMap">
<!-- some sql code here -->
</select>
</mapper>
に注意してください namespace
の属性 <mapper>
の要素 MyEntityMapper.xml
存在しないマッパーを指しています non.existent.package.NonExistentMapper
, 、本当にそれが指摘されるべきであるとき my.mappers.MyEntityMapper
.
タイプインターフェイス org.domain.classmapper MapperRegistryには知られていません
MyBatisは、完全なパッケージ /クラスがマッパーXMLネームスペースに入力されない場合、この例外をスローします。
例えば<mapper namespace="classmapper">
例外を引き起こしますが
<mapper namespace="org.domain.classmapper">
作品
Spring Boot ProjectのShadowjar/Bootjarを作成し、org.springframework.boot gradleプラグインを使用するときに私に起こりました。
jarがbootjarの内部でzipしている場合、mybatisはxml構成ファイルを見つけることができず、説明された例外がスローされます
build.gradleファイルにこのブロックを追加します:
bootJar {
requiresUnpack '**/MyProblematic.jar'
}
私の問題を解決しました