JPA SQL HQL

//原生sql+接口映射——可行

@Query(value = "select SUM(IF(type='recharge_online' and module='balance',money_points,0))+SUM(IF(type='consume' and consume_type='recharge_online',money_points,0)) as recharge_online, SUM(IF(type='present_online' and module='balance',money_points,0))+SUM(IF(type='consume' and consume_type='present_online',money_points,0)) as present_online, SUM(IF(type<>'present_online' and type<>'recharge_online' and module='balance',money_points,0))+SUM(IF(type='consume' and consume_type='other',money_points,0)) as other from socialx_order o where o.status = 'succeed'", nativeQuery = true)

List<MultiBalance> getMultiMoneyPointsForBalance();

//非原生sql+使用表名——不可行----非原生sql需要使用映射类

    @Query(value = "select SUM(case when type='recharge_online' and module='balance' then money_points else 0 end)+SUM(case when type='consume' and consume_type='recharge_online' then money_points else 0 end) as recharge_online, SUM(case when type='present_online' and module='balance' then money_points else 0 end)+SUM(case when type='consume' and consume_type='present_online' then money_points else 0 end) as present_online, SUM(case when type<>'present_online' and type<>'recharge_online' and module='balance' then money_points else 0 end)+SUM(case when type='consume' and consume_type='other' then money_points else 0 end) as other from socialx_order o where o.status = 'succeed'")

    List<Map> getMultiMoneyPointsForBalance();

//非原生sql+使用类名+表字段或者类字段——都可行

    @Query(value = "select SUM(case when type='recharge_online' and module='balance' then money_points else 0 end)+SUM(case when type='consume' and consume_type='recharge_online' then money_points else 0 end) as recharge_online, SUM(case when type='present_online' and module='balance' then money_points else 0 end)+SUM(case when type='consume' and consume_type='present_online' then money_points else 0 end) as present_online, SUM(case when type<>'present_online' and type<>'recharge_online' and module='balance' then money_points else 0 end)+SUM(case when type='consume' and consume_type='other' then money_points else 0 end) as other from SocialxOrder o where o.status = 'succeed'")

    List<MultiBalance> getMultiMoneyPointsForBalance();

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。