org.apache.ibatis.builder.IncompleteElementException: 找不到结果图。为什么?
org.apache.ibatis.builder.IncompleteElementException: Could not find result map . Why?
我无法使用 MyBatis 将查询结果映射到 bean,并且 Spring 使用 xml 文件引导。
配置:spring开机,mybatis.
1) 我有一个映射器:
package ru.kq.cn.mapper;
@Mapper
public interface GateMapper {
@Select("call [dbo].[cai_Select] 1, ")
@ResultMap("GateMapper.WResultMap")
WQueryResult call();
}
2) 在同一个包中,我有 xml 的结果集:
<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) 应用程序:
@MapperScan("ru.kq.cn.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
请帮忙!
正如你没有提到的,我假设数据库是 MS SQL 服务器。
一些基础知识。
- 调用存储过程需要设置
statementType="CALLABLE"
.
- 在JDBC中调用存储过程的语法是
{call proc(?,?,...)}
.
- 要处理多个结果集,您需要使用语句的
resultSets
命名每个结果集。
因此,映射器方法可能如下所示:
@Select("{call [dbo].[cai_Select](1)}")
@ResultMap("WResultMap")
@Options(
statementType = StatementType.CALLABLE,
resultSets = "firstRS,secondRS">
WQueryResult call();
你的程序returns两个结果集。我分别将它们命名为firstRS
和secondRS
。
并在结果映射中,指定 <collection />
的 resultSet
属性。
我假设第二个结果集是针对 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>
映射到 List<String>
有点棘手。我们有一个 FAQ entry.
这是一个可执行文件 demo project。
演示父子关系的映射,比你的复杂
我无法使用 MyBatis 将查询结果映射到 bean,并且 Spring 使用 xml 文件引导。
配置:spring开机,mybatis.
1) 我有一个映射器:
package ru.kq.cn.mapper;
@Mapper
public interface GateMapper {
@Select("call [dbo].[cai_Select] 1, ")
@ResultMap("GateMapper.WResultMap")
WQueryResult call();
}
2) 在同一个包中,我有 xml 的结果集:
<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) 应用程序:
@MapperScan("ru.kq.cn.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
请帮忙!
正如你没有提到的,我假设数据库是 MS SQL 服务器。
一些基础知识。
- 调用存储过程需要设置
statementType="CALLABLE"
. - 在JDBC中调用存储过程的语法是
{call proc(?,?,...)}
. - 要处理多个结果集,您需要使用语句的
resultSets
命名每个结果集。
因此,映射器方法可能如下所示:
@Select("{call [dbo].[cai_Select](1)}")
@ResultMap("WResultMap")
@Options(
statementType = StatementType.CALLABLE,
resultSets = "firstRS,secondRS">
WQueryResult call();
你的程序returns两个结果集。我分别将它们命名为firstRS
和secondRS
。
并在结果映射中,指定 <collection />
的 resultSet
属性。
我假设第二个结果集是针对 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>
映射到 List<String>
有点棘手。我们有一个 FAQ entry.
这是一个可执行文件 demo project。
演示父子关系的映射,比你的复杂