我在 google 或 stack 上看到这个问题的描述非常奇怪。让我解释。
我在接口(interface)方法的注释中有结果映射。只有在这种特殊情况下我才需要动态查询,这就是我决定在 xml 文件中为接口(interface)编写整个映射器的原因。下面我粘贴整个文件。选择查询应该没问题,但我遇到了一些困难 <resultMap>
.
在不同的网站上,我一直在寻找对一对一、一对多、多对一关联和此结果图的一般构造的很好解释。
我看到有某种可能性将它分成子查询、子结果映射。但是我已经用 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 >= #{dateFrom} AND
</if>
<if test="dateTo != null">
date <= #{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>
最佳答案
映射器 XML 的 XSD 期望在 <resultMap>
中:
<association>
毕竟必须来 <result>
标签,这意味着您需要对 <result>
进行分组标签,然后添加 <association>
之后的标签。
其次,你不需要两者resultType
和 resultMap
在getUsageCounterList
.使用您的情况下的任何一个 resultMap
第三,你的WHERE
getUsageCounterList
中的条款被打破。如果仅满足第一个条件怎么办,那么在这种情况下您的 SELECT 将类似于:
SELECT * FROM usage_counter WHERE api_consumer IN (1,2,3) AND
正如我在之前对您的查询的回答中提到的,您可以使用 <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
}
您可以使用 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>
所以Mybatis会确保创建一个City
的实例并在 capital.name
中赋值那个实例的 name
属性(property)。
对于一对多或多对多映射,您可以探索<collection>
MyBatis 结果映射的标签。有关更多信息,请查看 Advacned Result Maps提供的链接中的部分。
关于java - MyBatis 中带有关联的 XML ResultMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47435294/