編寫自定義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;
}
}
_
