在Hibernate中query.setParameter的两种传参方式

时间:2021-04-01 22:11:42

方式一:

select
        h.regionid regionid,
        r.region_name regionidName,
        h.pubdate pubdate,
        xlcl.f_gtDayAmount(h.regionid,
        h.pubdate,
        0) localAmount,
        XLCL.f_gtDayAmount(h.regionid,
        h.pubdate,
        1) otherAmount,
        XLCL.f_gtDayAmount(h.regionid,
        h.pubdate,
        2) interAmount,
        sum(staynum) totalAmount
    from
        xlcldata.mfs_history_day h
    inner join
        xlclgis.mfb_region r  
            on h.regionid=r.id  
    where
        h.areacode is not null
        and r.pid=0  
        and h.pubdate=?
    group by
        h.regionid,
        h.pubdate,
        r.region_name
    order by
        totalAmount asc

忽略sql中的逻辑关系,有没有发现sql中的问号?那么在sql中传参查询时利用setParameter方法进行传参,具体如下:

query.setParameter("0",queryDate);//queryDate为日期对象

方式二:

把sql语句中问号那一行改为

and h.pubdate=:dateTime

那么传参方式为:

query.setParameter("dateTime",queryDate);

在实际工程项目中,第一种传参方法要多一些