工作中遇到了一个查询需求,想查出来的数据结构是这样的
List<Student> studentList = studentMapper.selectStudentInfo();
Student的结构是这样的
public class Student{
private Long studentId;
private List<Report> reportList;
}
public class Report{
private String type;
private String reportNum;
}
可以用以下动态sql完成
<!--studentMapper中-->
<resultMap id="studentMap" type="Student">
<result column="id" property="id" jdbcType="BIGINT" />
<collection property="reportList" column="id" ofType="Report" select="reportMapper.selectById" />
</resultMap>
<select id="selectStudentInfo" resultMap="studentMap">
select id from student
</select>
<!--reportMapper中-->
<select id="selectById" resultMap="Report">
select type as type,report_num as reportNum from report where student_id = #{id,jdbcType=BIGINT}
</select>
标签collection中的属性
property:子查询的别名
column:子查询的入参
ofType:子查询的的返回类型
select:子查询的执行语句
//查询table表中flag字段0和1的数量
select
sum(case when flag = 0 then 1 else 0 end) as zeroCount,
sum(case when flag = 1 then 1 else 0 end) as oneCount
from table