springDataJpa复杂多表sql的多条件结合分页实现
如题;此类查询,jpa⾃带的单表、本地sql查询已经⽆法满⾜需求,基于此要求,我们需要调⽤jpa的EntityManager内置查询引擎,⽅可实现。
在ReportRespository的实现类中引⼊实体管理引擎:
@PersistenceContext
private EntityManager entityManager;
sql组装:
public Long findOrderAmountByCondition(QueryParameter parameter) {
boolean isGroup =false;
String whereStr =" where 1=1 ";
StringBuffer sql = new StringBuffer();
sql.append("select  ");
if( commonUtil.FirstDeptHid())
&& "1".DeptSelect())){
sql.append( " c_first_dept_hid,"); //部门ID
sql.append( " max( c_first_dept_name),");//部门名称
}else{
sql.append(" max( c_first_dept_hid),max(c_first_dept_name),");
}
if(!commonUtil.MonthSelect()) && "1".MonthSelect())){
sql.append(" to_char(createTime,'YYYY-MM') ,"); //⽉份
}else{
sql.append(" '⽉份' ,");
}
if( (commonUtil.Supplier()) && "1".SupplierSelect()))
|| !commonUtil.Supplier())){
sql.append( " (case supplier when '1' then '苏宁' when '2' then '史泰博'  when '3' then '得⼒' else '' END )as supplier," );//平台
}else{
sql.append(" '全部平台'  ,") ;//所有
}
sql.append(" sum(personalPrice) ,"//个⼈版价格
+ " count(1) ," //订单数量
+ " sum(totalprice), " //下单⾦额
+" sum(quantity) "
+ " from ");
selectOrderAmountUnionInnerQuerySql(parameter, sql);
sql.append(" where 1=1 ");
if(commonUtil.FirstDeptHid())
&& "1".DeptSelect())){
//区分部门
sql.append(" c_first_dept_hid,");
isGroup =true;
}
if( (commonUtil.Supplier()) && "1".SupplierSelect()))
|| !commonUtil.Supplier())){
sql.append( " supplier," );//平台
isGroup =true;
}
}
if(!commonUtil.MonthSelect())
&& "1".MonthSelect()) ){
isGroup =true;
sql.append(" to_char(createTime,'YYYY-MM') ");
sql.append(" order by  to_char(createTime,'YYYY-MM') asc ");
}else{
sql = new StringBuffer(sql.substring(0, sql.length()-1));
}
if(isGroup){
sql.insert(sql.lastIndexOf(whereStr) + whereStr.length(), "  group by  ");
}
//通过组装好的sql创建查询引擎对象Query
Query query =  String());
if(!commonUtil.StartTime()) && !commonUtil.EndTime()) ) {
if(!commonUtil.MonthSelect()) && "1".MonthSelect())){
parameter.StartTime().substring(0, 7));
parameter.EndTime().substring(0, 7));
}
}
setParameterValues(query,parameter);
//需要分页时,可在此处添加
//query.setFirstResult((PageNumber()-1) * PageSize());
//query.PageSize());
@SuppressWarnings("unchecked")
List<Object[]> objectList = ResultList();  //获取查询结果
if(null==objectList || objectList.size()==0){
return 0L;
}else{
return new Long(objectList.size());
}
}
设置其他参数:
/**
* 设置参数值
* query
* supplier
* @param startTime
* @param endTime
* @param firstDeptId
*/sql中union多表合并
public void setParameterValues(Query query,QueryParameter parameter){
if(!commonUtil.isObjNullOrEmp(parameter)){
if(!commonUtil.StartTime())
&& !commonUtil.EndTime()) ){
query.setParameter("startTime", StartTime());
query.setParameter("endTime", EndTime());
}
if(!commonUtil.Supplier()) ){
query.setParameter("supplier", Supplier());
}
if(!commonUtil.FirstDeptHid()) ){
query.setParameter("firstDeptHid", FirstDeptHid());
}
if(null!=StatusList() && StatusList().size()>0){                  query.setParameter("status", StatusList());
}
if(!commonUtil.Name()) ){
query.setParameter("name", "%"+Name()+"%");
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。