实例代码:
controller:返回的是Page<>对象
@Controller
@RequestMapping(value = "/egg")
public class EggController {
@ResponseBody
@RequestMapping(value = "/statisticsList")
public Page statisticsList(@RequestParam("actId") Long actId,HttpServletRequest request,
Pageable pageable){
Long entId = CasUtils.getEntId(request);
return eggService.findStatisticsWithPage(entId,actId,pageable) ;
}
}
serviceImpl中的实现方法:
这里需要查询出数据的数量total,以及查询到的数据list,最后new一个实现类 new PageImpl(list,pageable,total)
Page 的实现类是PageImpl Pageable 的实现类是PagerRequest
public Page findStatisticsWithPage(Long entId, Long actId, Pageable pageable) {
int total = wactPlayRecordDao.findDistinctPlayRecordTotal(entId, actId);
// List openIdList = wactPlayRecordDao.findDistinctPlayRecordList(entId, actId);
List wactPlayRecordList = wactPlayRecordDao.findDistinctPlayRecordList(entId,actId);
List statisticsDtoList = new ArrayList<>();
if (wactPlayRecordList.size()>0){
// statisticsDtoList = new ArrayList<>(wactPlayRecordList.size());
for (WactPlayRecord wactPlayRecord:wactPlayRecordList){
StatisticsDto statisticsDto = new StatisticsDto();
String openid = wactPlayRecord.getOpenid();
Long playRecordId = wactPlayRecord.getId();
Mpuser mpuser = mpuserDao.findMpUserByOpenId(openid);
List customerCollItemInfoList = customerCollitemInfoDao.findCustomerCollItemInfoByOpenId(openid,entId,actId,playRecordId);
List custCollItemsInfoDtoList = new ArrayList<>();
if (customerCollItemInfoList.size()>0){
statisticsDto.setDetailIsShow("able");
custCollItemsInfoDtoList = new ArrayList<>(customerCollItemInfoList.size());
for (CustomerCollItemInfo customerCollItemInfo:customerCollItemInfoList){
CustCollItemsInfoDto custCollItemsInfoDto = new CustCollItemsInfoDto(customerCollItemInfo);
custCollItemsInfoDtoList.add(custCollItemsInfoDto);
}
}else{
//已中奖但是未填写&&未中奖
statisticsDto.setDetailIsShow("unable");
}
if (wactPlayRecord.getIsWin()==0){
wactPlayRecord.setIsUse(2);
}
WactPlayRecordDto wactPlayRecordDto = new WactPlayRecordDto(wactPlayRecord);
MpuserDto mpuserDto = null;
if (mpuser!= null){
mpuserDto = new MpuserDto(mpuser);
}
statisticsDto.setWactPlayRecordDto(wactPlayRecordDto);
statisticsDto.setCustCollItemsInfoDtoList(custCollItemsInfoDtoList);
statisticsDto.setMpuserDto(mpuserDto);
statisticsDtoList.add(statisticsDto);
}
}
return new PageImpl(statisticsDtoList,pageable,total);
}
public interface WactPlayRecordDao extends JpaRepository{
@Query("FROM WactPlayRecord w WHERE w.entId = :endId AND w.actId = :actId AND w.status = 1")
List findDistinctPlayRecordList(@Param("endId") Long endId,@Param("actId") Long actId);
@Query("SELECT count(w.openid) FROM WactPlayRecord w WHERE w.entId = :endId AND w.actId = :actId AND w.status = 1")
int findDistinctPlayRecordTotal(@Param("endId") Long endId,@Param("actId") Long actId);
方式1(使用与查询条件在一个实体类中).:
controller:同样是返回的Page<>对象 ,增加了表单提交的数据对象
@Controller
@RequestMapping("/news")
public class NewsController {
private NewsService newsService;
private NewsCategoryService newsCategoryService;
@RequestMapping("/list")
@ResponseBody
public Page list(Pageable pageable, NewsCondition newsCondition) {
Long id = AppUtils.getBean("loginInfo", LoginInfo.class).getEntId();
newsCondition.setEntId(id);
return newsService.find(newsCondition, pageable);
}
}
@Service
public class NewsServiceImpl implements NewsService {
@Override
@Transactional(readOnly = true)
public Page find(final NewsCondition condition, Pageable pageable) {
Page page = newsDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
List list = new ArrayList<>();
list.add(cb.equal(root.get("entId").as(Long.class), condition.getEntId()));
list.add(cb.equal(root.get("deleted").as(Boolean.class), false));
Join newsCategoryJoin = root.join("newsCategory");
list.add(cb.equal(newsCategoryJoin.get("enable").as(Boolean.class), true));
list.add(cb.equal(newsCategoryJoin.get("deleted").as(Boolean.class), false));
if (condition.getCategoryId() != null) {
list.add(cb.equal(newsCategoryJoin.get("id").as(Long.class), condition.getCategoryId()));
}
if (StringUtils.isNotBlank(condition.getTitle())) {
list.add(cb.like(root.get("title").as(String.class), "%" + condition.getTitle() + "%"));
}
if (condition.getFromDate() != null) {
list.add(cb.greaterThanOrEqualTo(root.get("createdDate").as(Date.class), DateUtils.getDateWithStartSecond(condition.getFromDate())));
}
if (condition.getToDate() != null) {
list.add(cb.lessThanOrEqualTo(root.get("createdDate").as(Date.class), DateUtils.getDateWithLastSecond(condition.getToDate())));
}
query.orderBy(cb.desc(root.get("top")), cb.desc(root.get("id")));
Predicate[] predicates = new Predicate[list.size()];
predicates = list.toArray(predicates);
return cb.and(predicates);
}
}, pageable);
if (page.hasContent()) {
List newsEntities = page.getContent();
List newsDtos = new ArrayList<>(newsEntities.size());
List pids = new ArrayList<>();
for (NewsEntity newsEntity : newsEntities) {
if (newsEntity.getTitleImage() != null) {
pids.add(newsEntity.getTitleImage());
}
}
Map map = null;
if (CollectionUtils.isNotEmpty(pids)) {
map = mediaFileDao.findWithMap(pids);
}
for (NewsEntity newsEntity : newsEntities) {
newsDtos.add(new NewsDto(newsEntity, map != null ? map.get(newsEntity.getTitleImage()) : null));
}
return new PageImpl(newsDtos, pageable, page.getTotalElements());
}
return null;
}
}
dao:这里一定要继承JpasSpecificationExecutor,然后使用其中的findAll()方法,其中封装了分页方法,排序方法等
public interface NewsDao extends JpaRepository, JpaSpecificationExecutor {
}
controller
@Controller
@RequestMapping(value = "/egg")
public class EggController {
@ResponseBody
@RequestMapping(value = "/statisticsList")
public Page statisticsList(@RequestParam("actId") Long actId,HttpServletRequest request,
Pageable pageable,StatisticsCondition statisticsCondition){
Long entId = CasUtils.getEntId(request);
return eggService.findStatisticsWithPage(entId,actId,pageable,statisticsCondition) ;
}
@Override
@Transactional
public Page findStatisticsWithPage(Long entId, Long actId, Pageable pageable, StatisticsCondition statisticsCondition) {
Long total = wactPlayRecordDao.findTotalByCondition(entId,actId,
statisticsCondition.getParticipateBegin(),statisticsCondition.getParticipateEnd()
,statisticsCondition.getTelephone(),statisticsCondition.getIsWin(),statisticsCondition.getIsUse());
List statisticsDtoList = new ArrayList<>();
if (total >0){
List
dao daoplus daoImpl(dao继承daoPlus,daoImpl实现daoPlus)
daoplus
public interface WactPlayRecordPlusDao {
Long findTotalByCondition(Long entId,Long actId,Date participateBegin,Date participateEnd,String telephone,Integer isWin,Integer isUse);
List
daoImpl
public class WactPlayRecordDaoImpl implements WactPlayRecordPlusDao {
@PersistenceContext
private EntityManager entityManager;
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Long findTotalByCondition(Long entId, Long actId, Date participateBegin, Date participateEnd, String telephone, Integer isWin, Integer isUse) {
StringBuffer sql = null;
if (telephone != null && !"".equals(telephone)){
// hql = new StringBuffer("SELECT w.id,w.isWin,w.isUse,W.openid " +//表没有关联所以不能使用面向对象语句的left outer join inner join 等
// "FROM WactPlayRecord w inner join CustomerCollItemInfo c " +
// "ON w.id = c.playRecordId" +
// "WHERE w.entId = :entId AND w.actId = :actId AND w.status = 1 " +
// "AND c.entId = :entId AND c.actId = :actId AND c.status = 1 And c.val = :telephone ");
sql =new StringBuffer("SELECT count(p.id)" +
"FROM rp_act_play_record p inner join rp_act_customer_collitem_info c " +
"ON p.id = c.play_record_id " +
"WHERE p.ent_id = :entId AND p.act_id = :actId AND p.status = 1 " +
"AND c.ent_id = :entId AND c.act_id = :actId AND c.status = 1 And c.val = :telephone ");
}else {
// hql = new StringBuffer("SELECT w.id,w.isWin,w.isUse,w.openid " +
// "FROM WactPlayRecord w " +
// "WHERE w.entId = :entId AND w.actId = :actId AND w.status = 1 " );
sql = new StringBuffer("SELECT count(p.id) " +
"FROM rp_act_play_record p " +
"WHERE p.ent_id = :entId AND p.act_id = :actId AND p.status = 1 " );
}
if (participateBegin != null){
sql.append("AND p.created_date >= :participateBegin ");
}
if (participateEnd != null){
sql.append("AND p.created_date <= :participateEnd ");
}
if (isWin == 0){
sql.append("AND p.is_win = 0 ");
}else if (isWin ==1){
sql.append("AND p.is_win = 1 ");
}
if (isUse == 0){
sql.append("AND p.is_use = 0 ");
}else if (isUse == 1){
sql.append("AND p.is_use = 1 ");
}
sql.append("order by p.created_date ASC");
Query query = entityManager.createNativeQuery(sql.toString());
query.setParameter("entId", entId).setParameter("actId", actId);
if (participateBegin != null){
query.setParameter("participateBegin", participateBegin);
}
if (participateEnd != null){
query.setParameter("participateEnd", participateEnd);
}
if (telephone != null && !"".equals(telephone)){
query.setParameter("telephone",telephone);
}
Long total = new BigInteger(query.getSingleResult().toString()).longValue();
return total;
}
@Override
public List