Java Mybatis:从选择返回嵌套映射,不带N+;1选择(即不使用@Many或结果映射子查询)
对于一个简单的查询:Java Mybatis:从选择返回嵌套映射,不带N+;1选择(即不使用@Many或结果映射子查询),java,mybatis,ibatis,Java,Mybatis,Ibatis,对于一个简单的查询: select primaryKey, secondaryKey, attributeOne, attributeTwo 和一个普通的域对象: public class ExampleDto { Integer primaryKey; Integer secondaryKey; String attributeOne; String attributeTwo; } 我希望: Map<PrimaryKey, Map<Seconda
select primaryKey, secondaryKey, attributeOne, attributeTwo
和一个普通的域对象:
public class ExampleDto {
Integer primaryKey;
Integer secondaryKey;
String attributeOne;
String attributeTwo;
}
我希望:
Map<PrimaryKey, Map<SecondaryKey, ExamplDto>>
Map
我当前的方法是使用一个额外的Dto对象:
<resultMap id="ExampleCollection" type="ExampleCollectionDto" autoMapping="true">
<id property="primaryKey" column="primaryKey" javaType="integer"/>
<collection property="values" ofType="ExampleDto" autoMapping="true">
<id property="primaryKey" column="primaryKey" javaType="integer"/>
<id property="secondaryKey" column="secondaryKey" javaType="integer"/>
</collection>
</resultMap>
public class ExampleCollectionDto {
Integer primaryKey;
List<ExampleDto> values;
public Map<Integer, KeyedValue> getMap() {
Map<Integer, ExampleDto> results;
for(ExampleDto value : values) {
results.put(value.secondaryKey, value);
}
return results;
}
}
公共类示例CollectionDTO{
整数主密钥;
列表值;
公共地图getMap(){
地图结果;
例如(例如到值:值){
结果.put(value.secondaryKey,value);
}
返回结果;
}
}
并通过
public interface ExampleMapper {
@MapKey("primaryKey")
Map<Integer, ExampleCollectionDto> getExampleCollectionDtoMap();
}
公共接口示例映射器{
@MapKey(“primaryKey”)
映射getExampleCollectionDtoMap();
}
是否有任何方法(通过注释或xml映射)可以避免收集DTO,或者使用基本的MyBatis功能,或者通过在结果处理流中注入我自己?即
public interface ExampleMapper {
// possibly some annotation here
Map<Integer, Map<Integer, ExampleDto>> getExampleDtoMap();
}
公共接口示例映射器{
//这里可能有注释
映射getExampleDtoMap();
}
到目前为止,我找到的唯一方法是N+1查询问题(我已经有了所需的所有数据)。这并不是您想要的,因为mapper接口中的方法不会直接返回值,但仍然可能是一个选项 您可以尝试使用自定义
ResultHandler
来发布处理结果:
ExampleMapper.xml
<resultMap id="ExampleDtoMap" type="ExampleDto" autoMapping="true">
<id property="primaryKey" column="primaryKey" javaType="integer"/>
</resultMap>
<select id="getExampleDtoMap" resultMap="ExampleDtoMap">
select * from example_table
</select>
MyService.java
class MyResultHandler implements ResultHandler {
Map<Integer, Map<Integer, ExampleDto>> result = new HashMap<>();
@Override
public void handleResult(ResultContext context) {
ExampleDto dto = (ExampleDto)context.getResultObject();
Map<Integer, ExampleDto> map = result.get(dto.primaryKey);
if (map == null) {
map = new HashMap<>();
}
map.put(dto.secondaryKey, dto);
}
};
MyResultHandler handler = new MyResultHandler();
ExampleMapper exampleMapper = ...;
exampleMapper.getExampleDtoMap(handler);
return handler.result;
类MyResultHandler实现ResultHandler{
映射结果=新的HashMap();
@凌驾
public void handleResult(ResultContext上下文){
ExampleDto dto=(ExampleDto)context.getResultObject();
Map Map=result.get(dto.primaryKey);
if(map==null){
map=新的HashMap();
}
map.put(dto.secondaryKey,dto);
}
};
MyResultHandler=新的MyResultHandler();
示例映射器示例映射器=。。。;
getExampleDtoMap(处理程序);
返回handler.result;
感谢您的回复。正如你所说,这并不完全是我想要的,但我有一种感觉,这是我将得到的最好的。如果在4天内没有更好的响应,我会将其标记为正确,然后继续。
class MyResultHandler implements ResultHandler {
Map<Integer, Map<Integer, ExampleDto>> result = new HashMap<>();
@Override
public void handleResult(ResultContext context) {
ExampleDto dto = (ExampleDto)context.getResultObject();
Map<Integer, ExampleDto> map = result.get(dto.primaryKey);
if (map == null) {
map = new HashMap<>();
}
map.put(dto.secondaryKey, dto);
}
};
MyResultHandler handler = new MyResultHandler();
ExampleMapper exampleMapper = ...;
exampleMapper.getExampleDtoMap(handler);
return handler.result;