BAT大牛亲授基于ElasticSearch的搜房网实战(第六章后台管理模块开发下)

上篇博客:BAT大牛亲授基于ElasticSearch的搜房网实战(第六章后台管理模块开发上)

从零起步基于ElasticSearch的搜房网(前后端集成)实战(介绍与整体目录)点击即可
静态资源集成太多页面,我已经上传到博客资源链接,供下载。后期代码全部完善后,会上传到github上。
静态资源链接下载(点击)

前端资源全部模板链接下载(点击)

本章实现模块如下:
第6章 房源信息管理模块实现
本章会对项目后台房源数据管理模块的开发进行详细的讲解,并手把手带领大家开发后台系统的相关功能,包括基于七牛云的图片上传、新增房源、数据浏览、编辑功能等基本增删查改的代码开发,以及基本的房源审核功能。


 6-9 房源浏览功能实现_基本开发
 6-10 房源浏览功能实现_分页实现
 6-11 房源浏览功能实现_多维度排序
 6-12 编辑功能实现_上
 6-13 编辑功能实现_下
 6-14 审核功能实现
 

房源信息浏览功能:

  1. 基础开发
  2. 分页实现
  3. 多维度排序

首先在base包中新建一个类:使用这类作为Datatables的响应结构:

1.基础开发:


ApiDataTableResponse:

package liangliang.bigdata.base;

/**
 * Datatables响应结构
 * Created by 彭亮.
 */
public class ApiDataTableResponse extends ApiResponse {
    private int draw; //验证结果
    //这两个字段 datatablede 要求必须这样写
    private long recordsTotal;
    private long recordsFiltered;

    public ApiDataTableResponse(ApiResponse.Status status) {
        this(status.getCode(), status.getStandardMessage(), null);
    }

    public ApiDataTableResponse(int code, String message, Object data) {
        super(code, message, data);
    }

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public long getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(long recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public long getRecordsFiltered() {
        return recordsFiltered;
    }

    public void setRecordsFiltered(long recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }
}

在form的文件夹下面再定义一个搜索的表单,因为datatable都有特定的要求:

DatatableSearch:
package liangliang.bigdata.web.form;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * Created by 彭亮.
 */
public class DatatableSearch {
    /**
     * Datatables要求回显字段
     */
    private int draw;

    /**
     * Datatables规定分页字段
     */
    private int start;
    private int length;
    /**
     *@描述 无痕奈何
     *@创建人 彭亮
     * 审核的状态码
     */

    private Integer status;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeMin;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTimeMax;

    private String city;
    private String title;
    private String direction;
    private String orderBy;

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Date getCreateTimeMin() {
        return createTimeMin;
    }

    public void setCreateTimeMin(Date createTimeMin) {
        this.createTimeMin = createTimeMin;
    }

    public Date getCreateTimeMax() {
        return createTimeMax;
    }

    public void setCreateTimeMax(Date createTimeMax) {
        this.createTimeMax = createTimeMax;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }
}

IHouseService中加入查询接口:

 ServiceMultiResult<HouseDTO> adminQuery(DatatableSearch searchBody);

HouseServiceImpl中实现查询方法:

 

 在AdminController中加入功能的接口开发:

 前端自己给我已经实现了一个分页结果:

 我们更多的是使用后端的分页操作,防止数据过多内存爆炸。

2、分页实现

把之前的Crud改成:PagingAndSortingRepository

 修改后:

public interface HouseRepository extends PagingAndSortingRepository<House, Long> {
}

修改后的HouseServiceImpl中实现查询方法

    @Override
    public ServiceMultiResult<HouseDTO> adminQuery(DatatableSearch searchBody) {
        List<HouseDTO> houseDTOS = new ArrayList<>();

        Sort sort = new Sort(Sort.Direction.fromString(searchBody.getDirection()), searchBody.getOrderBy());
        int page = searchBody.getStart() / searchBody.getLength();

        Pageable pageable = new PageRequest(page, searchBody.getLength(), sort);

        Page<House> houses = houseRepository.findAll(pageable);
        houses.forEach(house -> {
            HouseDTO houseDTO = modelMapper.map(house, HouseDTO.class);
            houseDTO.setCover(this.cdnPrefix + house.getCover());
            houseDTOS.add(houseDTO);
        });

        return new ServiceMultiResult<>(houses.getTotalElements(), houseDTOS);
    }

3、实现多维度排序:

开始在base文件夹中新建一个util

HouseStatus:
package liangliang.bigdata.base;

/**
 * 房源状态
 * Created by 彭亮.
 */
public enum HouseStatus {
    NOT_AUDITED(0), // 未审核
    PASSES(1), // 审核通过
    RENTED(2), // 已出租
    DELETED(3); // 逻辑删除
    private int value;

    HouseStatus(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}
package liangliang.bigdata.base;

/**
 * 房源状态
 * Created by 彭亮.
 */
public enum HouseStatus {
    NOT_AUDITED(0), // 未审核
    PASSES(1), // 审核通过
    RENTED(2), // 已出租
    DELETED(3); // 逻辑删除
    private int value;

    HouseStatus(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

想要实现多维度的排序查询,需要在HouseRepository加入JpaSpecificationExecutor

HouseRepository:

public interface HouseRepository extends PagingAndSortingRepository<House, Long>, JpaSpecificationExecutor<House> {
}

在HouseServiceImpl中加入实现多维度查询的方法:Specification

    @Override
    public ServiceMultiResult<HouseDTO> adminQuery(DatatableSearch searchBody) {
        List<HouseDTO> houseDTOS = new ArrayList<>();

        Sort sort = new Sort(Sort.Direction.fromString(searchBody.getDirection()), searchBody.getOrderBy());
        int page = searchBody.getStart() / searchBody.getLength();

        Pageable pageable = new PageRequest(page, searchBody.getLength(), sort);

        Specification<House> specification = (root, query, cb) -> {
            Predicate predicate = cb.equal(root.get("adminId"), LoginUserUtil.getLoginUserId());
            predicate = cb.and(predicate, cb.notEqual(root.get("status"), HouseStatus.DELETED.getValue()));

            if (searchBody.getCity() != null) {
                predicate = cb.and(predicate, cb.equal(root.get("cityEnName"), searchBody.getCity()));
            }

            if (searchBody.getStatus() != null) {
                predicate = cb.and(predicate, cb.equal(root.get("status"), searchBody.getStatus()));
            }

            if (searchBody.getCreateTimeMin() != null) {
                predicate = cb.and(predicate, cb.greaterThanOrEqualTo(root.get("createTime"), searchBody.getCreateTimeMin()));
            }

            if (searchBody.getCreateTimeMax() != null) {
                predicate = cb.and(predicate, cb.lessThanOrEqualTo(root.get("createTime"), searchBody.getCreateTimeMax()));
            }

            if (searchBody.getTitle() != null) {
                predicate = cb.and(predicate, cb.like(root.get("title"), "%" + searchBody.getTitle() + "%"));
            }

            return predicate;
        };

        Page<House> houses = houseRepository.findAll(specification, pageable);
        houses.forEach(house -> {
            HouseDTO houseDTO = modelMapper.map(house, HouseDTO.class);
            houseDTO.setCover(this.cdnPrefix + house.getCover());
            houseDTOS.add(houseDTO);
        });

        return new ServiceMultiResult<>(houses.getTotalElements(), houseDTOS);
    }

实现结果: 

二、房源信息编辑功能:

  1. 编辑功能实现_上
  2. 编辑功能实现_下
  3.  审核功能实现

https://blog.csdn.net/qq_41479464/category_8496960.html

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值