12

接続文字列などのグローバル パラメーターを指定するための XML 表記が気に入っています。Mapper アノテーションも気に入っています。2 つを組み合わせようとすると、この例外が発生します。

2つを組み合わせる方法はありますか?グローバル構成に XML ファイルを使用したいのですが、mybatis に Mapper インターフェースを考慮してもらいます。

問題は、SqlSessionFactoryBuilder().build() が Reader (XML 構成を渡すために使用したい) または Configuration オブジェクト (私をaddMappers()助けることができるメソッドを持っていることがわかります) のいずれかを取ることです-しかし、私はしません2つを組み合わせる方法を理解してください。

4

3 に答える 3

10

factory.getConfiguration().addMapper(...);

于 2011-01-06T14:38:10.433 に答える
6

xml の sql として正確なメソッド シグネチャを持つ抽象メソッドを使用してマッパー インターフェイスを作成する場合。

たとえば。これは、実際のクエリを含む dao.xml の名前空間でした。

<mapper namespace=" com.mybatis.dao.EntityMapperInterface">
    <select id="selectEmployeeWithId" parameterType="Long"
        resultType="com.mybatis.domain.Employee">
        select id,name from employee where 1=1
        <if test="_parameter != null"> 
            AND id=#{id} 
        </if>
        order by id
    </select>

インターフェイス com.mybatis.dao.EntityMapperInterfaceにマップされます

public interface EntityMapperInterface {
    public List<Employee> selectEmployeeWithId(Long id);

Mybatis-config ファイル

<mappers>
    <mapper resource="com/mybatis/mappers/EntityMapper.xml" />
</mappers>

アクションクラス/サーブレットからどのように呼び出すのですか? SqlSession を初期化したら、

EntityMapperInterface emi = session.getMapper(EntityMapperInterface.class);
List eList = emi.selectEmployeeWithId(1);
于 2012-03-22T04:39:47.303 に答える
0

私は同じ問題を抱えていましたが、これは mybatis マッパー ファイルの名前空間とマッパー インターフェイスのパッケージが一致していなかったためです。

于 2011-12-13T20:15:10.497 に答える