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);
        }

以上为个人经验,希望能给大家一个参考

1. 本站所有资源来源于用户分享和网络转载,如有侵权请联系站长删除!
2. 分享目的仅供大家学习参考,源码类您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!

917资源网 » jpa实现只查询指定的字段

发表评论

提供最优质的资源集合

立即查看 了解详情