spring data jpa 多表 分页 多条件 查询

结论如下:

用querydsl的方式。

既然提供了这种方式就用吧。单实体,单表多条记录,动态条件直接写,不用写dao层。

多表的,大部分用querydsl都能实现。不能实现的,用sql实现。

返回值方面 还是和以前一样,返回主实体类,附加的挂在对应的类属性上。需要很高效率的用dto返回。

代码如下:

 

package com.xxx.xxxxx.service.impl;

import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;



@Service
public class DemoStudentServiceImpl extends BaseServiceImpl<DemoStudent,String >
implements DemoStudentService {

@Autowired
private DemoStudentRepository demoStudentRepository;

@Autowired
public DemoStudentServiceImpl(DemoStudentRepository demoStudentRepository) {
super(demoStudentRepository);
}

@Override
public int updateNameByStudentGuid(String name, String studentGuid){
return demoStudentRepository.updateNameByStudentGuid(name,studentGuid);
}

@Override
public long countStudentBySex(String sex) {
return demoStudentRepository.countBySex(sex);
}

@Override
public int deleteStudentByName(String name) {
//return demoStudentRepository.deleteByNameQuick(name);
return demoStudentRepository.deleteXXXByName(name);//deleteXXXByXXX ,delte 后面跟着的,by前面跟着的没所谓起啥。都支持。
}

@Override
public List< DemoStudent > removeStudentByName(String name) {
return demoStudentRepository.removeStudentByName(name);
}

/**
* Query 注解加 Modifying 注解 方式更新多条记录
* @param name
* @param birthday
* @return
*/
public int updateNameByAge(String name, String birthday) {
return demoStudentRepository.updateNameByAge(name,birthday);
}

/**
* 查询单个
* @param name
* @return
*/
@Override
public DemoStudent findByNameAndAge(String name,String age) {
return demoStudentRepository.findByNameAndAge(name,age);
}

/**
* 查询单个(动态条件)
* @param name
* @param age
* @return
*/
@Override
public DemoStudent findStudentPredicate(String name, String age) {
QDemoStudent qDemoStudent=QDemoStudent.demoStudent;
BooleanBuilder builder = new BooleanBuilder();
if(StringUtils.isNotBlank(name)){
builder.and(qDemoStudent.name.like("%"+name+"%"));
}
if(StringUtils.isNotBlank(age)){
builder.and(qDemoStudent.age.eq(age));
}
//多条记录会报错
return demoStudentRepository.findOne(builder).get();
}

/**
* 单表集合 动态查询 predicate 方式
* @param page
* @param rows
* @param name
* @param age
* @return
*/
@Override
public Page<DemoStudent> pageStudentListPredicate(Integer page, Integer rows, String name, String age) {
//查找所有的的分页
//demoStudentRepository.findAll(PageRequest.of(page-1,rows));

//分页加排序加参数查询
QDemoStudent qDemoStudent=QDemoStudent.demoStudent;
BooleanBuilder builder = new BooleanBuilder();
if(StringUtils.isNotBlank(name)){
builder.and(qDemoStudent.name.like("%"+name+"%"));
}
if(StringUtils.isNotBlank(age)){
builder.or(qDemoStudent.age.eq(age));
}

return demoStudentRepository.findAll(builder,PageRequest.of(page-1,rows,Sort.by("age").descending()));
}

/**
* 多表集合 动态查询 predicate 方式,返回方式为主类,副类或者副类的其他字段以其他方式set进主类
* @param page
* @param rows
* @param name
* @param age
* @return
*/
@Override
public Page<DemoStudent> pageStudentClassListPredicate(Integer page, Integer rows, String name, String age) {
//分页加排序加参数查询
QDemoStudent qDemoStudent=QDemoStudent.demoStudent;
BooleanBuilder builder = new BooleanBuilder();
if(StringUtils.isNotBlank(name)){
builder.and(qDemoStudent.name.like("%"+name+"%"));
}
if(StringUtils.isNotBlank(age)){
builder.or(qDemoStudent.age.eq(age));
}
return demoStudentRepository.pageStudentClassListPredicate(builder,PageRequest.of(page-1,rows));
}

/**
* 多表查询,采用dsl的方式,返回自定义对象
* @return
*/
@Override
public List < DemoStudentVO > findStudentClassDslDTO(){
return demoStudentRepository.findStudentClassDslDTO();
}


/**
* 多表查询,原始查询的方式(native sql),并且按指定的resultSetMapping返回
* @return
*/
@Override
public Page<DemoStudent> pageStudentClassSqlMapResult(Integer page, Integer rows, String name, String age){
return demoStudentRepository.pageStudentClassSqlMapResult(PageRequest.of(page-1,rows),name,

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值