【自己項目代碼】
@Query("select bean from User bean where bean.org.id=?1 and bean.group.id=?2")
public List<User> findByOrgIdAndGroupId(int orgId,int groupId);
問題:?1 和?2
回答:參數中的值在執行時可以賦值給?1或者?2的位置。
1 自己項目代碼 2 package com.jspxcms.core.repository; 3 4 import java.util.Collection; 5 import java.util.Date; 6 import java.util.List; 7 8 import org.springframework.data.domain.Page; 9 import org.springframework.data.domain.Pageable; 10 import org.springframework.data.jpa.domain.Specification; 11 import org.springframework.data.jpa.repository.Modifying; 12 import org.springframework.data.jpa.repository.Query; 13 import org.springframework.data.repository.Repository; 14 15 import com.jspxcms.common.orm.Limitable; 16 import com.jspxcms.core.domain.User; 17 18 /** 19 * UserDao 20 * 21 * @author liufang 22 * 23 */ 24 public interface UserDao extends Repository<User, Integer>, UserDaoPlus { 25 public Page<User> findAll(Specification<User> spec, Pageable pageable); 26 27 public List<User> findAll(Specification<User> spec, Limitable limitable); 28 29 public User findOne(Integer id); 30 31 public User save(User bean); 32 33 public void delete(User bean); 34 35 @Modifying 36 @Query("update User bean set bean.recommend=?2,bean.birthDate=?3 where bean.id=?1") 37 public void recommended(Integer id,Integer recommend,Date birth); 38 39 @Modifying 40 @Query("update User bean set bean.status=?2 where bean.id=?1") 41 public void checkUser(Integer id, int status); 42 43 // ------------------------------------ 44 45 public User findByUsername(String username); 46 47 public User findByQqOpenid(String qqOpenid); 48 49 public User findByWeiboUid(String weiboUid); 50 51 public User findByValidationTypeAndValidationKey(String type, String key); 52 53 @Query("select count(*) from User bean where bean.username=?1") 54 public long countByUsername(String username); 55 56 @Query("select count(*) from User bean where bean.org.id in ?1") 57 public long countByOrgId(Collection<Integer> orgIds); 58 59 @Query("select count(*) from User bean where bean.group.id in ?1") 60 public long countByGroupId(Collection<Integer> groupIds); 61 62 @Query("select bean from User bean where bean.org.id=?1 and bean.group.id=?2") 63 public List<User> findByOrgIdAndGroupId(int orgId,int groupId); 64 65 @Query("select bean from User bean where bean.org.parent.id=?1 and bean.group.id=?2") 66 public List<User> findByParentOrgIdAndGroupId(int orgId,int groupId); 67 68 @Query("select count(bean.id) from User bean where bean.group = 3") 69 public long findByCk(); 70 71 @Query("select count(bean.id) from User bean where bean.group = 4") 72 public long findByinvestor(); 73 74 }
【參考內容:http://www.itnose.net/detail/6095818.html】
1 在Spring框架中,關於從數據表獲取數據有不同的方法,當數據查詢比較簡單時,可以通過繼承JpaRepository<T, L> 使用findBy***方法,通過分析方法名來實現查詢,T表示要查詢的數據表所對應的實體,L表示該實體主鍵的類型,比如Long。關於findBy方法如何通過分析方法名來實現查詢,網上資料很多,不做贅述。 2 如果查詢的數據比較復雜,查詢條件比較復雜時,可以考慮使用JPA的@query方法來實現查詢。關於使用方法,下面做簡單介紹: 3 4 1.首先Dao層需繼承Repository<T, L>,T為實體名,L為該實體的主鍵類型。 5 2.寫查詢方法,並使用@query進行注解,例如: 6 @query(select u from User u where name=?1 and age=?2) 7 List findUser(String name,int age); 8 在這里,User為實體名,參數中的name值在執行時可以賦值給?1的位置。 9 但是,事實上,如果只是從一個數據表中獲取數據,使用上面所說的findBy**方法即可,並不需要使用@query來實現。 10 但是,當查詢條件比較復雜,無法使用findBy方法時,就可以考慮使用@query.先給出一個例子: 11 @Query("select max(jobinfo.processes) from Jobinfo jobinfo,Taskinfo taskinfo where taskinfo.jobid=jobinfo.id and (shenweistatus=4 or shenweistatus=5) and queuename=?1") 12 List findMaxNuclear(String queuename); 13 14 通過這條語句,可以看到其涉及到兩個實體,查詢的是max,用Findby是無法實現的,但是這個例子就可以很好的實現。而關於1中的T就沒有多大意義,應該是隨便寫語句中涉及到的一個實體即可。 15 關於其他的復雜查詢都可以通過這種方法實現。 16 再看下面這個例子: 17 @Query("select jobinfo.queuename from Jobinfo jobinfo,Taskinfo taskinfo where taskinfo.jobid=jobinfo.id and (shenweistatus=1 or shenweistatus=3) and jobinfo.queuename like ?1 group by jobinfo.queuename") 18 List findQueuenameByName(String name); 19 20 里邊涉及到了模糊查詢,在SQL中,模糊查詢應該是 like ‘%AA%’等形式的,但是這里的注解中如果加入了%,會報錯,怎么解決呢?可以在調用該方法是人為的將%傳進去,比如findQueuenameByName(“%”+name+”%”),這樣就可以實現模糊查詢。 21 需要注意的是,我測試發現,這種方法只能查詢一個字段,如果要查詢多個字段,只能寫多個方法,得到多個list,最后將查詢結果拼到一塊。(至於到底能不能一次查詢多個字段,正在探索中。。。。)