Reputation: 171
I cannot map the result of a query to a bean with MyBatis and Spring Boot using an xml file.
Configuration: spring boot, mybatis.
1) I have a Mapper:
package ru.kq.cn.mapper;
@Mapper
public interface GateMapper {
@Select("call [dbo].[cai_Select] 1, ")
@ResultMap("GateMapper.WResultMap")
WQueryResult call();
}
2) At the same package I have xml for ResultSet:
<mapper namespace="ru.kq.cn.mapper.GateMapper">
<resultMap id="WResultMap" type="ru.kq.cn.dto.WQueryResult">
<result property="proverTpId" column="proverTpId"/>
<collection property="itemIds" column="itemId">
</collection>
</resultMap>
</mapper>
3) DTO:
package ru.kq.cn.dto;
..
@Data
public class WQueryResult implements Serializable {
Long proverTpId;
List <String> itemIds;
}
3) application.properties:
mybatis.type-aliases-package=ru.kq.cn.dto
mybatis.mapper-locations='classpath:ru/kq/cn/mapper/*.xml'
4) App:
@MapperScan("ru.kq.cn.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Please help!
Upvotes: 0
Views: 4180
Reputation: 3594
As you didn't mention, I assumed that the DB is MS SQL Server.
A few basics.
statementType="CALLABLE"
.{call proc(?,?,...)}
.resultSets
of the statement.So, the mapper method may look something like this:
@Select("{call [dbo].[cai_Select](1)}")
@ResultMap("WResultMap")
@Options(
statementType = StatementType.CALLABLE,
resultSets = "firstRS,secondRS">
WQueryResult call();
Your procedure returns two result sets. I named them firstRS
and secondRS
respectively.
And in the result map, specify the resultSet
attribute of <collection />
.
I assumed that the second result set is for the itemIds
.
<resultMap id="WResultMap" type="ru.kq.cn.dto.WQueryResult">
<result property="proverTpId" column="proverTpId"/>
<collection property="itemIds" ofType="java.lang.String"
javaType="java.util.ArrayList" resultSet="secondRS">
<id column="itemId">
</collection>
</resultMap>
Mapping to List<String>
is a little bit tricky. We have an FAQ entry.
Here is an executable demo project.
To demonstrate mapping of parent-child relationships, it is more complicated than yours.
Upvotes: 1