使用子查询获取一对多关系的数据
首先看封装的对象
@Data
@ApiModel(value = "专家意见Vo" ,description = "专家意见Vo")
public class SrExpertOpinionVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "专家意见id")
private String expertOpinionId;
@ApiModelProperty(value = "科研课题申报Id")
private String scientificSubjectId;
@ApiModelProperty(value = "年度")
private String subjecYear;
@ApiModelProperty(value = "标题")
private String scientificSubject;
@ApiModelProperty(value = "课题来源")
private String subjectOrigin;
@ApiModelProperty(value = "发布课题种类")
private String subjectType;
@ApiModelProperty(value = "评审时间")
private Date reviewTime;
@ApiModelProperty(value = "评审轮次")
private Integer reviewCount;
@ApiModelProperty(value = "评价总分")
private BigDecimal averageScore;
@JsonIgnore
private List<SrRateProject> srRateProjectList;
}
可以看出在SrExpertOpinionVo 实体里面封装了一个srRateProjectList,表明在SrExpertOpinionVo这实例上面就有可能出现多条srRateProjectList这样的数据(一般srRateProjectList都是一个表里面的字段不用关联其他表获取)。
mybatis中xml的写法(获取一对多的数据)
<select id="selectExpertOpinion" parameterType="string" resultMap="ExpertOpinionResult">
SELECT
ed.expert_opinion_id,
ss.scientific_subject_id,
ss.subject_year,
ss.scientific_subject,
ss.subject_origin,
ss.subject_type,
ss.create_time,
ed.rounds_of_expert_valuation
FROM
trs_cloud.sr_expert_draw ed
LEFT JOIN trs_cloud.sr_scientific_subject ss ON ss.scientific_subject_id = ed.data_id
WHERE
ed.type = 1
AND ss.scientific_subject_id = #{subjectId}
首先看一对多中 ”一“ 的SQL的写法,主要讲的就事resultMap=“ExpertOpinionResult”>这一标签,在对复杂语句进行联合映射的时候,它很可能可以代替数百行的同等功能代码。在这里主要是做了数据库和实体类做了映射。
其次看一对多中 ”多“ 的SQL的写法:
<resultMap type="com.trs.scientific.research.management.domain.vo.SrExpertOpinionVo"
id="ExpertOpinionResult">
<result property="expertOpinionId" column="expert_opinion_id"/>
<result property="scientificSubjectId" column="scientific_subject_id"/>
<result property="subjecYear" column="subject_year"/>
<result property="scientificSubject" column="scientific_subject"/>
<result property="subjectOrigin" column="subject_origin"/>
<result property="subjectType" column="subject_type"/>
<result property="reviewTime" column="create_time"/>
<result property="reviewCount" column="rounds_of_expert_valuation"/>
<collection property="srRateProjectList"
ofType="com.trs.scientific.research.management.domain.entity.SrRateProject"
column="scientific_subject_id"
select="getSrRateProjectList"
javaType="java.util.ArrayList"></collection>
</resultMap>
<select id="getSrRateProjectList" parameterType="com.trs.scientific.research.management.domain.entity.SrRateProject"
resultType="com.trs.scientific.research.management.domain.entity.SrRateProject">
SELECT "rate_id", "data_id", "scoring_id", "score", "create_by", "create_time", "update_by", "update_time"
FROM "trs_cloud"."sr_rate_project"
WHERE data_id = #{scientificSubjectApplyId};
</select>
这里就涉及到了多个xml中常用的标签以及标签中的属性,首先是中的type是指向一对多中 "一"包的类路径,id就表示"一"的写法中标签的resultMap的名称 ,id和resultMap的名称要对应不然匹配不上;接下来是标签中的property和column属性前者是实体类中的字段后者是数据库中的字段;接下来是标签中的property属性表示:刚才说的实体类封装的srRateProjectList,ofType属性表示:srRateProjectList实体所在的包路径,column属性表示:通过那个字段去查这个一对多的多数据srRateProjectList(一般通过表中的唯一字段进行关联),select属性表示:用于查询多条属性的记录(srRateProjectList)的sql名称,javaType属性就表示返回的类型,一般就是List。