JPA#復雜查詢#自定義查詢


編寫自定義SQL基於下面信息:
1. SpringData JPA 在為Repository接口生成實現的時候,會查找是否有 "接口名稱"+"Impl"的類,如果有的話,就把這個類的方法合並到要生成的實現當中。

-----

假設:要為接口StudentRepository編寫自定義sql查詢。
基於最前面的信息,要編寫自定義SQL,需要下面三步:
1. 自定義一個接口,在在接口中聲明方法StudentCoustomRepository,這個自定義接口名稱不重要;
2. 讓目標接口繼承自定義接口,這樣目標接口就有了相應的方法;
3. 編寫自定義方法的實現類,這個類需要使用"目標接口名稱"+"Impl"為類名,
即StudentRepositoryImpl,這樣SpringDataJpa 為StudentRepository生成實現的時候就會包含這里面的方法了。

 

----

 

public class StudentRepositoryImpl implements StudentCoustomRepository {
	
	// 這里可以寫很復雜的SQL,作為演示之用,就不弄那么復雜
	private static final String SQL = "select * from t_student where name like :name ";
	
	@PersistenceContext
	private EntityManager em;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<StudentVO> findByName(String name) {
		Query query = em.createNativeQuery(SQL).setParameter("name", "%"+name+"%");
		query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		List queryList = query.getResultList();
		
		if (queryList.size() == 0) {
			System.out.println("找不到Student name為" + name + "的記錄");
			return null;
		}
		
		List<StudentVO> retVal = new ArrayList<>();
		for(Object o : queryList) {
			Map student = (Map)o;
			StudentVO vo = new StudentVO();
			try {
				// org.apache.commons.beanutils.BeanUtils;
				// 使用apaches的beanutil,直接吧map轉為實例
				BeanUtils.populate(vo, student);
				retVal.add(vo);
			} catch (IllegalAccessException | InvocationTargetException e) {
				System.out.println("解析StudentVO bean時發生異常:" + e.getMessage());
			}
		}
		return retVal;
	}
}

  

 

 

_


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM