簡體   English   中英

如何在 MyBatis 動態 SQL 的帶注釋映射器中使用 XML ResultMap?

[英]How do I use an XML ResultMap in an annotated mapper for MyBatis Dynamic SQL?

我正在嘗試將 MyBatis 動態 SQL 用於我的應用程序,並且在遵循有關如何將 XML 映射器與帶注釋的映射器一起使用的指南時遇到了一些問題,如本頁上的“用於連接語句的 XML 映射器”中所述。

我正在嘗試使以下查詢起作用:

SessionMapper mapper = sqlSession.getMapper(SessionMapper.class);
SessionTableSupport sessionMaster = new SessionTableSupport();

SelectStatementProvider stmt = select(
        sessionMaster.sessionId,
        sessionMaster.module,
        sessionMaster.startTime,
        sessionMaster.endTime,
        sessionMaster.eventId,
        sessionMaster.userId)
        .from(sessionMaster.sessionTable)
        .where(sessionMaster.module, isEqualTo("sample_module"))
        .build()
        .render(RenderingStrategy.MYBATIS3);

List<SessionResult> sampleSessions = mapper.selectMany(stmt);

供您參考,配置文件如下所示:

<configuration>
    <settings> ... </settings>
    <environments> ... </environments>
    <mappers>
        <mapper resource="./mappers/SessionResultMapper.xml"/>
        <package name="mappers"/>
    </mappers>
</configuration>

然后,這是我的 xml 映射器SessionResultMapper.xml

<mapper namespace="SessionResultMapper">
    <resultMap id="SimpleResults" type="results.SessionResult">
        <id column="session_id" jdbcType="VARCHAR" property="sessionId" />
        <result column="module" jdbcType="VARCHAR" property="module" />
        <result column="start_time" jdbcType="BIGINT" property="startTime" />
        <result column="end_time" jdbcType="BIGINT" property="endTime" />
        <result column="event_id" jdbcType="INTEGER" property="eventId" />
        <result column="user_id" jdbcType="VARCHAR" property="userId" />
        <result column="institute" jdbcType="VARCHAR" property="institute" />
    </resultMap>
</mapper>

這是帶注釋的映射器接口SessionMapper.java

@Mapper
public interface SessionMapper {
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("SimpleResults")
    List<SessionResult> selectMany(SelectStatementProvider selectStatement);
}

最后,這是SessionResult.java

public class SessionResult {

    private String sessionId;
    private String module;
    private long startTime;
    private long endTime;
    private int eventId;
    private String userId;
    private String institute;

    public SessionResult(
            String sessionId,
            String module,
            long startTime, 
            long endTime,
            int eventId, 
            String userId, 
            String institute) {

        super();
        this.sessionId = sessionId;
        this.module = module;
        this.startTime = startTime;
        this.endTime = endTime;
        this.eventId = eventId;
        this.userId = userId;
        this.institute = institute;
    }

    public SessionResult() {}

    // Getters and Setters
    ...
}

當同時使用 xml 和注解運行上面的代碼時,會拋出以下異常:

Exception in thread "main" org.apache.ibatis.builder.IncompleteElementException: Could not find result map mappers.SessionMapper.SimpleResults
    at org.apache.ibatis.builder.MapperBuilderAssistant.getStatementResultMaps(MapperBuilderAssistant.java:346)
    at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:290)
    at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(MapperAnnotationBuilder.java:364)
    at org.apache.ibatis.builder.annotation.MethodResolver.resolve(MethodResolver.java:33)
    at org.apache.ibatis.session.Configuration.lambda$buildAllStatements$3(Configuration.java:795)
    at java.util.Collection.removeIf(Collection.java:414)
    at org.apache.ibatis.session.Configuration.buildAllStatements(Configuration.java:794)
    at org.apache.ibatis.session.Configuration.hasStatement(Configuration.java:763)
    at org.apache.ibatis.session.Configuration.hasStatement(Configuration.java:758)
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.resolveMappedStatement(MapperMethod.java:254)
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:224)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:50)
    at org.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:62)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:62)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
    at com.sun.proxy.$Proxy5.selectMany(Unknown Source)
    at test.TestMyBatisDynamic.main(TestMyBatisDynamic.java:70)
Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for mappers.SessionMapper.SimpleResults
    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:933)
    at org.apache.ibatis.session.Configuration.getResultMap(Configuration.java:645)
    at org.apache.ibatis.builder.MapperBuilderAssistant.getStatementResultMaps(MapperBuilderAssistant.java:344)
    ... 17 more

但是當我使用純注釋的結果映射時,一切正常。 不幸的是,我正在嘗試使用集合和關聯構建更復雜的結果映射,因此使用純注釋似乎不是一種選擇。

我已盡最大努力盡可能地遵循文檔中的示例。 我在這里完全不知所措......有沒有人知道如何解決這個問題? 任何幫助或提示將不勝感激! 提前感謝您的時間!

感謝評論中的帖子,我了解到我的代碼中存在命名空間沖突。 為了修復它,我更改了 xml 和帶注釋的映射器,如下所示:

SessionResultMapper.xml

<mapper namespace="SessionResultMapper">
    <resultMap id="SimpleResults" type="results.SessionResult">
        ...
    </resultMap>
</mapper>

因此,在 SessionMapper.java 中:

@ResultMap("SessionResultMapper.SimpleResults")

基本上,確保[namespace].[resultMap id]路徑在 xml 和帶注釋的映射器之間是一致的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM