JPA联表查询或返回其他参数结果接收处理

JPA联表查询或返回其他参数结果接收处理

1、使用一个自定义VO类去接收:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class FormContainItemCountVO {

    private Long formId;

    private String formName;

    private Long itemCount;

}

JPA Repository方法:

@Query(value = "select new com.chakid.dto.statics.FormContainItemCountVO (f.id,f.fromName,count(i.id)) from Form f left join FormWithItem i on f.id = i.formId group by f.fromName")
List<FormContainItemCountVO > countItemCountGroupByFormName();

2、使用一个自定义接口接收:

前缀加get以驼峰方式命名,sql那需要用别名指定,才能正确映射到!!

public interface StaticsPublishFormV2VO {

    String getFormIds();

    /**
     * 对应时间单位
     */
    String getCheckTimeUnit();

    /**
     * 对应时间单位包含的数量
     */
    Long getCount();

}

JPA Repository方法:

@Query(value = "SELECT GROUP_CONCAT(form_id) as formIds,CONCAT( YEAR(create_time),'-',MONTH(create_time)) as checkTimeUnit,COUNT(*) as count FROM tbl_publish_form WHERE check_time = ?1 and create_time BETWEEN ?2 and ?3 GROUP BY CONCAT( YEAR(create_time),'-',MONTH(create_time))", nativeQuery = true)
List<StaticsPublishFormV2VO> staticsPublishFormMonth(String checkTime, Date startTime, Date endTime);

想要获取返回的结果,直接调用对应的get方法即可。

这两种方法的是区别是,第一个不是采用的原生SQL,有些SQL函数不支持,第二种使用的是原始SQL,支持的函数更多。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值