将查询语句写在hbm.xml文件中
hbm.xml文件里面的格式:
<query name="Advertisement.GetUserOtherAdvertisementIdList">
??? ??? <![CDATA[
??? ??? ??? select a.advertisementId
??? ??? ??? from Advertisement a,
??? ??? ??? ??? Campaign c
??? ??? ??? where a.campaignId = c.campaignId??? ??? ??? ???
??? ??? ??? ??? and a.statusId = :statusId
??? ??? ??? ??? and c.userId = :userId
??? ??? ??? ??? and a.url <> :targetUrl
??? ??? ]]>
??? </query>
dao层的类:
Query query = getCurrentSession().getNamedQuery("Advertisement.GetUserOtherAdvertisementIdList");
??? ??? query.setInteger("userId", userId);
??? ??? query.setInteger("statusId", statusId);
??? ??? query.setString("targetUrl", targetUrl);
说明:颜色相同是对应的;query.setInteger("userId", userId);“”里面的是配置文件里面的颜色对应的。