关于动态sql的子查询

工作中遇到了一个查询需求,想查出来的数据结构是这样的

 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值