一) in (List<Long>)
@Modifying
@Query("DELETE FROM Height h WHERE h.user.id = ?1 AND h.id in (?2)")
public void deleteHeightInBatchByUserIdAndIdIn(Long uid, List<Long> hids);
二) in(Long[])原生的 JPA可以如下:
public List<FamilyMember> findByFamilyIdAndUserIdIn(Long fid, Long[] uids);
代码试验一(成功):
Long[] adArray = new Long[ads.size()];
for (int i = 0; i < ads.size(); i++) {
adArray[i] = ads.get(i).getId();
}
timePushDao.findByIdIn(adArray);
public List<TimePush> findByIdIn(Long[] adIds);
代码试验二(没成功,估计是不能用@Query("")的形式。)
Long[] adArray = new Long[ads.size()];
for (int i = 0; i < ads.size(); i++) {
adArray[i] = ads.get(i).getId();
}
@Modifying (有这句或者没这句都不行)
@Query("select t from TimePush t where t.ad.id in ?1 and t.pushTime>?2 and t.pushTime <?3 ")
public List<TimePush> findPageByTimePush2(Long[] adIds, Calendar startTime, Calendar endTime);
三)in (select)
@Query("SELECT t FROM Tweet t where t.user.id IN " +
"(SELECT fm1.user.id FROM FamilyMember fm1 where fm1.family.id IN " +
"(SELECT fm2.family.id FROM FamilyMember fm2 where fm2.user.id = ?1)) " +
"AND t.id < ?2")
public Page<Tweet> findFamiliesTweetsByIdLessThan(Long uid, Long tid, Pageable page);
代码试验一(没有成功,很困惑;把Ad改成全路径也不行。)
@Query("select t from TimePush t where t.ad.id in (select a.id form Ad a where a.title like ?1 and a.keyWord like ?2) ")
public List<TimePush> findPageByTimePush3(String title, String keyWord);
上面代码没有成功,换了以下方案,这个方案也更好,其实不需要用in。
public List<TimePush> findByAdTitleLikeAndAdKeyWordLike(String title, String keyWord);
四)拼装String,即“2,3,4” 这种怀疑不行
@Query(nativeQuery = true,
value = "select * from t_serve_area where addr_id in (?1) and manager_type = ?2 group by manager_id")
public List<ServeArea> findDistinctManagerIdByIdsAndManagerType(String ids, Integer manager_type);
五.注意
1)JPA 原生的那种sql,不可以用String startTime去比较数据库中的Calendar startTime。
而@Query(nativeQuery = true,sql="你的sql语句")这种可以。
2)
Page<TimePush>的形式好像不能用@Query(
nativeQuery = true,sql="你的sql语句")的语句,能用@Query("select t from TimePush t where t.ad in (?1) and t.pushTime>?2 and t.pushTime <?3 ")
3)
|
in (List) |
in (Array) |
in (select) |
JPA(不需要写sql) |
ok |
ok |
-- |
原生sql |
ok |
不行 |
不行 |
nativeQuery sql |
ok |
未试验 |
未试验 |