jpa实现只查询指定的字段
目录
- jpa查询指定的字段
- 下面是实例代码
- spring data jpa查询自定义查询字段
jpa查询指定的字段
JpaRepository中new的方式产生新的对象,并配置好其构造函数
JPA对字段命名有一点规则,推荐使用驼峰式命名,如果下面的方法会报错,并且没有明确的报错信息,请先看字段是否合法!!!!!
下面是实例代码
JpaRepository:
@Transactional public interface PayeeDataRepo extends JpaRepository<PayeeData, String> { @Query("select new com.stylefeng.guns.workday.bean.PayeeData(p.id,p.name,p.bankAccountNumberEncode,p.adhocPayeeID,p.isDomestic,p.bankAccountName ) from PayeeData p") List<PayeeData> finddAllNoBankNumber(); }
PayeeData Bean:
public class PayeeData implements Serializable { public PayeeData() { } public PayeeData(String id, String name, String bankAccountNumberEncode, String adhocPayeeID, Integer isDomestic, String bankAccountName) { this.id = id; this.name = name; this.bankAccountNumberEncode = bankAccountNumberEncode; this.adhocPayeeID = adhocPayeeID; this.isDomestic = isDomestic; this.bankAccountName = bankAccountName; } }
spring data jpa查询自定义查询字段
@Query(value = "SELECT a.city city,sum(m.page_view) view from t_ques m " + "LEFT JOIN attractions a on m.spot_name=a.name where m.ques_time<= ?1 and a.province_code=?2 GROUP BY a.city ORDER BY view desc", nativeQuery = true) List<Map<String, Object>> getHotCity(Date date, String pcode);
List<Map<String, Object>> cityHotBOList = quesRepository.getHotCity(dataDate, pcode); if(null == cityHotBOList || cityHotBOList.size() <= 0){ return null; } for(int i = 0; i< top && i<cityHotBOList.size();i++){ Map<String, Object> cityHotBO = cityHotBOList.get(i); HotCityResVO hotCityResVO = new HotCityResVO(); hotCityResVO.setName((String)cityHotBO.get("city")); List<Attractions> attractionsList = attractionsRepository.findByCity((String)cityHotBO.get("city")); hotCityResVO.setCode(attractionsList.get(0).getCityCode()); hotCityResVO.setNum((BigDecimal)cityHotBO.get("view")); hotCityResVOList.add(hotCityResVO); }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)