当前位置:龙哥网 > 网络技术 > jpa实现只查询指定的字段_java(jpa自定义查询)

jpa实现只查询指定的字段_java(jpa自定义查询)

longge3个月前 (03-01)网络技术42
目录
  • 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);
            }
    
    免责声明
    本站部分资源来源于互联网 如有侵权 请联系站长删除
    龙哥网是优质的互联网科技创业资源_行业项目分享_网络知识引流变现方法的平台为广大网友提供学习互联网相关知识_内容变现的方法。#转载请注明出处!