JPA原生sql(动态条件,分页)

JPA原生sql查询(动态条件,分页)

JPA原生sql查询的几种方式

eg1:

dao层

@Query(value="    SELECT b.FirstName ,b.LandArea ,c.LandColor\n" +
        "    FROM\n" +
        "            (SELECT top 100 a.FirstName, Sum(a.LandArea) as LandArea  FROM dbo.XH_PlanCodeInfo a\n" +
        "   GROUP BY a.FirstName ORDER BY Sum(a.LandArea) DESC) b\n" +
        "    LEFT JOIN dbo.X_LandColor c on b.FirstName = c.LandTypeName",nativeQuery = true)
List<Object> getFirstPieDatas();

service层处理

List<Object> collection =  collection = planCodeInfoRepository.getFirstPieDatas();
List<GisPie> gisPieList = new ArrayList<GisPie>();
for (Object o : collection) {
            GisPie gisPie = new GisPie();
            Object[] ol = (Object[]) o; //转换成对象数组
            gisPie.setName((String) ol[0]);
            gisPie.setValue(((BigDecimal) ol[1]).divide( new BigDecimal("1000000") ,2));
            gisPie.setItemStyle(new Color((String) ol[2]));
            gisPieList.add(gisPie);
}
eg2:

直接在service层里写sql

@Autowired
private EntityManager em;

StringBuilder dataSql = new StringBuilder("SELECT HouseType as name,  sum(Area_SH) as value FROM [dbo].[HF_ResidentialLandInfo] GROUP BY HouseType ORDER BY value DESC");
Query dataQuery = em.createNativeQuery(dataSql.toString(), GisPieSimple.class);
List<GisPieSimple> gisPieSimpleList = dataQuery.getResultList();
eg3:

原生sql加动态条件加分页,写在service层

public Page<XJHBuilding> findByPageAndParams(XJHBuildingSearchModel param, Pageable pageable) {
    StringBuilder dataSql = new StringBuilder("SELECT * FROM [dbo].[XJH_Building]");
    StringBuilder countSql = new StringBuilder("SELECT COUNT(1) FROM [dbo].[XJH_Building]");

    //拼接where条件
    StringBuilder whereSql = new StringBuilder(" WHERE 1 = 1");
    if (BaseUtil.stringNotNull(param.getBuildingName())) {
        whereSql.append(" and BuildingName LIKE  CONCAT('%',:buildingName,'%')");
    }

    if (BaseUtil.stringNotNull(param.getCompanyName())) {
        whereSql.append("and No in \n" +
                "(SELECT DISTINCT BuildingNo  FROM [dbo].[XJH_Company] \n" +
                "where CompanyName LIKE CONCAT('%',:companyName,'%'))");
    }

    if (BaseUtil.stringNotNull(param.getBusinessAddress())) {
        whereSql.append("and No in \n" +
                "(SELECT DISTINCT BuildingNo  FROM [dbo].[XJH_Company] \n" +
                "where BusinessAddress LIKE CONCAT('%',:businessAddress,'%'))");
    }

    //组装sql语句
    dataSql.append(whereSql);
    countSql.append(whereSql);

    //创建本地sql查询实例
    Query dataQuery = em.createNativeQuery(dataSql.toString(), XJHBuilding.class);
    Query countQuery = em.createNativeQuery(countSql.toString());

    //设置参数
    if (BaseUtil.stringNotNull(param.getBuildingName())) {
        dataQuery.setParameter("buildingName", param.getBuildingName());
        countQuery.setParameter("buildingName", param.getBuildingName());
    }

    if (BaseUtil.stringNotNull(param.getCompanyName())) {
        dataQuery.setParameter("companyName", param.getCompanyName());
        countQuery.setParameter("companyName", param.getCompanyName());
    }

    if (BaseUtil.stringNotNull(param.getBusinessAddress())) {
        dataQuery.setParameter("businessAddress", param.getBusinessAddress());
        countQuery.setParameter("businessAddress", param.getBusinessAddress());
    }

    //设置分页
    dataQuery.setFirstResult((int)pageable.getOffset());
    dataQuery.setMaxResults(pageable.getPageSize());

    BigInteger count =  new BigInteger(String.valueOf(countQuery.getSingleResult()));
    Long total = count.longValue();
    List<XJHBuilding> content = total > pageable.getOffset() ? dataQuery.getResultList() : Collections.<XJHBuilding> 			emptyList();
    return new PageImpl<>(content, pageable, total);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值