MyBatis中的XML ResultMap与关联

xro*_*oss 3 java associations dynamicquery xmlmapper mybatis

我看到这个问题在Google或Stack上非常奇怪。让我解释。

我的界面方法的注释中有结果图。仅在这种特殊情况下,我才需要动态查询,这就是我决定在xml文件中为接口编写整个映射器的原因。下面,我粘贴整个文件。选择查询应该可以,但是我遇到了一些困难<resultMap>

在不同的Web站点上,我一直在寻找关于此结果图的一对一,一对多,多对一关联和构造的良好解释。

我看到有某种可能性将其分成子查询,子结果映射。但是我已经使用myBatis注释完成了此操作,并且我想使用它。您能指导我,应该如何构造resultMap?我看不到需要构造函数,区分<collection>符,但它仍在大喊……(这就是为什么我加了标记)-IntelliJ强调了整个<resultMap>说法:"The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)"

我知道这似乎很明显,但是我完全不知道如何正确地做。请帮我。

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="pl.net.manager.dao.UsageCounterDAO">
    <select id="getUsageCounterList" resultType="pl.net.manager.domain.UsageCounter"
            resultMap="getUsageCounterListMap">
        SELECT * FROM usage_counter WHERE
        <if test="apiConsumerIdsList != null">
            api_consumer IN
            <foreach item="item" index="index" collection="apiConsumerIdsList"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
            AND
        </if>
        <if test="serviceConsumerIdsList != null">
            service IN
            <foreach item="item" index="index" collection="serviceConsumerIdsList"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
            AND
        </if>
        <if test="dateFrom != null">
            date &gt;= #{dateFrom} AND
        </if>
        <if test="dateTo != null">
            date &lt;= #{dateTo} AND
        </if>
        <if test="status != null">
            status = #{status}
        </if>
    </select>
    <resultMap id="getUsageCounterListMap" type="">

                    ofType="pl.net.manager.domain.UsageCounter">
            <id property="id" column="id"/>
            <result property="date" column="date"/>
            <result property="apiConsumer" column="api_consumer"
                    javaType="pl.net.manager.domain.ApiConsumer"/>
            <association property="apiConsumer" column="api_consumer"
                         resultMap="pl.net.manager.dao.ApiConsumerDAO.getApiConsumer"/>
            <result property="service" column="service" javaType="pl.net.manager.domain.Service"/>
        <association property="service" column="service"
                     resultMap="pl.net.manager.dao.ServiceDAO.getService"/>

            <result property="counterStatus" column="counter_status"/>
            <result property="ratingProcessId" column="rating_process_id"/>
            <result property="value" column="value"/>
            <result property="createdDate" column="created_date"/>
            <result property="modifiedDate" column="modified_date"/>

    </resultMap>
</mapper>
Run Code Online (Sandbox Code Playgroud)

Moh*_*lla 5

Mapper XML的XSD期望在<resultMap>

<association>必须拿出所有的后<result>标签,你需要哪个组手段<result>标签,然后添加<association>后的标签。

其次,您不需要resultTyperesultMap中的getUsageCounterList。使用您所用的任何一种resultMap

第三,您的WHERE子句getUsageCounterList已损坏。如果仅满足第一个条件,那么在这种情况下,您的SELECT将类似于:

SELECT * FROM usage_counter WHERE api_consumer IN (1,2,3) AND 
Run Code Online (Sandbox Code Playgroud)

因此,正如我在上一个查询查询答案中提到的那样,您可以使用<where>标签并遵循该答案中提到的语法。

第四,resultMap getUsageCounterListMap您只需要type在您要填充的Java类中将其移动到ofType即可type

对于“ 一对一”映射,您可以只使用一个<association>标记,或者使用另一种方法,假设您具有如下所示的Java POJO:

public class Country{
    private String name;
    private City capital;
    //getters and setters
}
public class City{
    private String name;
    //getters and setters
}
Run Code Online (Sandbox Code Playgroud)

您可以编写XML映射,如下所示:

<select id="getCountries" resultType="Country">
    SELECT c.name, cy.name "capital.name" 
    FROM country c 
    JOIN city cy ON cy.name = c.capital
</select>
Run Code Online (Sandbox Code Playgroud)

因此,Mybatis将确保创建的实例City并将值分配capital.name给该实例的name属性。

对于一对多或多对多映射,您可以浏览<collection>MyBatis结果图的标签。有关更多信息,请在提供的链接中查看“ 高级结果图”部分。