mariadb 在Spring Data JPA中从多个连接表中提取特定列

vcirk6k6  于 2022-11-08  发布在  Spring
关注(0)|答案(2)|浏览(117)

我的公司最近决定在新项目中改用SpringDataJPA而不是Mybatis,所以我对SpringDataJPA的使用还是很陌生的。
我正在尝试执行以下sql查询

SELECT ab.status, buyer_rate, buyer_name, buyer_tel, bid_price, ADDTIME(complete_dt,"23:00:0.000000") AS send_time, brand, model 
FROM auction_bid ab 
INNER JOIN goods_auction ga ON ab.goods_auction_idx=ga.idx 
INNER JOIN auction_info ai ON ga.auction_info_idx=ai.idx 
WHERE is_success=1 and ab.status='008';

我已经创建了3个实体,每个实体都与我的mariadb数据库中的表相对应
第一个
我尝试将从查询中获得的信息Map到以下对象

public class MessageData {
    private String status;
    private Double buyerRate;
    private String buyerName;
    private String buyerTel;
    private Long bidPrice;
    private Date sendTime;
    private String brand;
    private String model;
}

我知道我必须创建一个扩展JpaRepository的接口,但是我看到的示例似乎只是获取整个表,而不是选择几个列,我对如何将结果Map到List有点困惑。
任何反馈都将不胜感激!
谢谢你!

n3h0vuf2

n3h0vuf21#

你可以通过实体模型用hql连接代替内部连接。
select a from auction_bid ab join ab.goodsAuction gA join auctionInfo aI
您可以使用“数据传输对象(DTO)”来提取特定列。

lg40wkob

lg40wkob2#

对于预测,您可以尝试“select new”。我假设MessageData在包www.example.com中com.foo:

select new com.foo.MessageData(ab.status, buyer_rate, buyer_name, buyer_tel, bid_price, ADDTIME(complete_dt,"23:00:0.000000"), brand, model) FROM ...

相关问题