如何在 MyBatis 中使用两个不同的 resultMaps 重复使用相同的 SELECT 查询?
How to re-use the same SELECT query with two different resultMaps in MyBatis?
我有一个映射器 resultMap
,它有两个我需要 select 内联的集合
<collection property="companies" ofType="Company" column="user_id" select="selectCompanies"/>
<collection property="companyGroups" ofType="CompanyGroup" column="user_id" select="selectCompanyGroups"/>
这两个集合的查询完全相同,但生成的地图彼此非常不同。有没有一种方法可以将相同的 select 与两个不同的 resultMaps
一起使用?
我必须 运行 这些查询内联,因为如果它是主查询的一部分,由于左连接,它会导致额外的几千条记录。
我不能使用 SQL 因为它只允许静态参数解析,而不是动态的。
Is there a way that I can use the same select with two different resultMap
s?
是的,您可以像这样将相同的 select 与两个不同的 resultMap
一起使用。
<mapper namespace="com.example.PersonRepository">
<!-- The common re-usable SELECT query -->
<sql id="select-part">
SELECT first_name, last_name, ...other attributes...
FROM table
<!-- You may use dynamic parameters here the same way as in a regular query -->
<where>
<if test="searchParams.firstName != null">
first_name = #{searchParams.firstName}
</if>
<if test="searchParams.lastName != null">
and last_name = #{searchParams.lastName}
</if>
</where>
</sql>
<!-- Method List<Type1> getAsType1(SearchParams searchParams) -->
<select id="getAsType1" resultMap="map1">
<include refid="select-part"/>
</select>
<!-- Method List<Type2> getAsType2(SearchParams searchParams) -->
<select id="getAsType2" resultMap="map2">
<include refid="select-part"/>
</select>
<resultMap id="map1" type="Type1">
...
</resultMap>
<resultMap id="map2" type="Type2">
...
</resultMap>
</mapper>
不用担心 where
和 and
子句,MyBatis 会 handle them correctly.
您的 Java 代码可能如下所示:
package com.example;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
// Other imports
@Repository
public interface PersonRepository {
public List<Type1> getAsType1(@Param("searchParams") SearchParams searchParams);
public List<Type2> getAsType2(@Param("searchParams") SearchParams searchParams);
}
和SearchParams
只是一个带有firstName
、lastName
等的POJO
我有一个映射器 resultMap
,它有两个我需要 select 内联的集合
<collection property="companies" ofType="Company" column="user_id" select="selectCompanies"/>
<collection property="companyGroups" ofType="CompanyGroup" column="user_id" select="selectCompanyGroups"/>
这两个集合的查询完全相同,但生成的地图彼此非常不同。有没有一种方法可以将相同的 select 与两个不同的 resultMaps
一起使用?
我必须 运行 这些查询内联,因为如果它是主查询的一部分,由于左连接,它会导致额外的几千条记录。
我不能使用 SQL 因为它只允许静态参数解析,而不是动态的。
Is there a way that I can use the same select with two different
resultMap
s?
是的,您可以像这样将相同的 select 与两个不同的 resultMap
一起使用。
<mapper namespace="com.example.PersonRepository">
<!-- The common re-usable SELECT query -->
<sql id="select-part">
SELECT first_name, last_name, ...other attributes...
FROM table
<!-- You may use dynamic parameters here the same way as in a regular query -->
<where>
<if test="searchParams.firstName != null">
first_name = #{searchParams.firstName}
</if>
<if test="searchParams.lastName != null">
and last_name = #{searchParams.lastName}
</if>
</where>
</sql>
<!-- Method List<Type1> getAsType1(SearchParams searchParams) -->
<select id="getAsType1" resultMap="map1">
<include refid="select-part"/>
</select>
<!-- Method List<Type2> getAsType2(SearchParams searchParams) -->
<select id="getAsType2" resultMap="map2">
<include refid="select-part"/>
</select>
<resultMap id="map1" type="Type1">
...
</resultMap>
<resultMap id="map2" type="Type2">
...
</resultMap>
</mapper>
不用担心 where
和 and
子句,MyBatis 会 handle them correctly.
您的 Java 代码可能如下所示:
package com.example;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
// Other imports
@Repository
public interface PersonRepository {
public List<Type1> getAsType1(@Param("searchParams") SearchParams searchParams);
public List<Type2> getAsType2(@Param("searchParams") SearchParams searchParams);
}
和SearchParams
只是一个带有firstName
、lastName
等的POJO