概述
文章目录
- 数据结构
- 实体类
- 第一层查询实现逻辑
- 第二层查询实现逻辑
- 第三层查询实现逻辑
- 总结
- 碰到问题
数据结构
实体类
@Data
public class AchievementRepetitionVo {
@ApiModelProperty(value = "成果编号")
private String code;
@ApiModelProperty(value = "成果名称")
private String name;
@ApiModelProperty(value = "成果介绍")
private String introduce;
@ApiModelProperty(value = "查重范围文档数")
private Integer checkNum;
@ApiModelProperty(value = "最高相似度")
private BigDecimal maxRate;
@ApiModelProperty(value = "成果对比明细")
private List<RepetitionDetailVo> detailList;
}
@Data
public class RepetitionDetailVo {
@ApiModelProperty(value = "对比成果名称")
private String contractName;
@ApiModelProperty(value = "对比成果介绍")
private String contractIntroduce;
@ApiModelProperty(value = "对比明细")
private List<RepetitionContractVo> contractList;
}
@Data
public class RepetitionContractVo {
@ApiModelProperty(value = "检测成果句子")
private String s1;
@ApiModelProperty(value = "对比成果库句子")
private String s2;
@ApiModelProperty(value = "相似度")
private BigDecimal rate;
}
第一层查询实现逻辑
1、外层service将code字段传入queryRepetitionReport方法,该方法查询的code作为参数(column=“{code=code}”)传给queryDetailList
2、mybatis循环调用queryDetailList
相关代码如下:
AchievementRepetitionVo queryRepetitionReport(String code);
<resultMap id="QueryRepetitionReportMap" type="AchievementRepetitionVo">
<result column="code" jdbcType="VARCHAR" property="code" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="introduce" jdbcType="VARCHAR" property="introduce" />
<result column="check_num" jdbcType="INTEGER" property="checkNum" />
<result column="rate" jdbcType="DECIMAL" property="maxRate" />
<collection property="detailList" ofType="RepetitionDetailVo"
select="queryDetailList"
column="{code=code}">
</collection>
</resultMap>
<select id="queryRepetitionReport" parameterType="string" resultMap="QueryRepetitionReportMap">
select rate.code,application.name,rate.check_num,rate.rate,rate.introduce
from t_achievement_repetition_rate rate
left join t_achievement_application application on rate.code = application.code
where rate.code = #{code}
</select>
第二层查询实现逻辑
queryDetailList接收上一层查询返回的code字段,每一次执行queryDetailList方法都会把查询出的code、contrast_code字段传给queryContractList方法
mybatis再循环调用queryContractList方法。
相关代码如下:
List<RepetitionDetailVo> queryDetailList(String code);
<resultMap id="QueryRepetitionDetailMap" type="com.aspirecn.rewardinnovation.entity.vo.achievementrepetition.RepetitionDetailVo">
<result column="contractName" jdbcType="VARCHAR" property="contractName" />
<result column="contractIntroduce" jdbcType="VARCHAR" property="contractIntroduce" />
<collection property="contractList" ofType="com.aspirecn.rewardinnovation.entity.vo.achievementrepetition.RepetitionContractVo"
select="queryContractList"
column="{code=code,contrastCode=contrast_code}">
</collection>
</resultMap>
<select id="queryDetailList" resultMap="QueryRepetitionDetailMap">
select result.code,application.name as contractName,result.contrast_code,result.contrast_introduce as contractIntroduce
from t_achievement_repetition_result result
left join t_achievement_application application on application.code = result.contrast_code
where result.code = #{code}
union
select result.contrast_code as code,application.name as contractName,result.code as contrast_code,result.introduce as contractIntroduce
from t_achievement_repetition_result result
left join t_achievement_application application on application.code = result.code
where result.contrast_code = #{code}
</select>
第三层查询实现逻辑
queryContractList接收上一层查询返回的code、contrastCode字段,
mybatis再循环调用queryContractList方法
相关代码如下:
List<RepetitionContractVo> queryContractList(String code, String contrastCode);
<select id="queryContractList" resultType="RepetitionContractVo">
select result.s1,result.s2,result.rate
from t_achievement_repetition_result result
where result.code = #{code} and result.contrast_code = #{contrastCode}
union all
select result.s1,result.s2,result.rate
from t_achievement_repetition_result result
where result.contrast_code = #{code} and result.code = #{contrastCode}
</select>
总结
其实就是Collection放到resultMap,下一层把上一层的查询结果作为条件传入。
参考链接
碰到问题
第二层或者第三层的映射文件加上parameterType="string"会报"nested exception is org.apache.ibatis.reflection.ReflectionException: There is no setter for property named ‘code’ in 'class java.lang.String"错误
<select id="queryDetailList" parameterType="string" resultMap="QueryRepetitionDetailMap">
<select id="queryContractList" parameterType="string" resultType="RepetitionContractVo">
原因不明,懂的大佬麻烦告知,不胜感激!
最后
以上就是狂野水壶为你收集整理的MyBatis实现多层级collection嵌套查询的全部内容,希望文章能够帮你解决MyBatis实现多层级collection嵌套查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复