第一步,看一下页面原型:
第二步 前端控制器
/** * * 条件分页查询 * @param deviceExtend 条件对象 * @param currentPage 当前页 * @param pageSize 每页显示数 * @param from 起始时间 * @param to 结束时间 * @return */ @GetMapping @ResponseBody public PagedeviceExtendPage(DeviceExtend deviceExtend,Integer currentPage,Integer pageSize,String from,String to ){ if(currentPage>0&&pageSize>0){ return deviceExtendService.getDeviceExtendPage(deviceExtend,currentPage,pageSize,from,to); } return null; }
第三步,需要的实体类:
@Document(collection = "DeviceExtend") public class DeviceExtend { /** * 设备ID */ @Id private String id; /** * 设备名称 */ @Field("name") @JsonProperty("name") private String name; /** * 设备SN码 */ @Field("SN") @JsonProperty("SN") private String SN; /** * 设备的MAC地址 */ @Field("MAC") @JsonProperty("MAC") private String MAC; /** * 设备类型 */ @Field("deviceType") @JsonProperty("deviceType") private String deviceType; /** * 制造商 */ @Field("manufacturer") @JsonProperty("manufacturer") private String manufacturer; /** * 入库时间 */ @Field("storageTime") @JsonProperty("storageTime") private Date storageTime; /** * 设备状态 1为入库, 2为出库, 3入网 */ @Field("deviceState") @JsonProperty("deviceState") private Integer deviceState; /** * 设备参数 */ @Field("deviceParameter") @JsonProperty("deviceParameter") private String deviceParameter; /** * 设备维保信息 */ @Field("maintain") @JsonProperty("maintain") private String maintain; public String getDeviceParameter() { return deviceParameter; } public void setDeviceParameter(String deviceParameter) { this.deviceParameter = deviceParameter; } public String getMaintain() { return maintain; } public void setMaintain(String maintain) { this.maintain = maintain; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSN() { return SN; } public void setSN(String SN) { this.SN = SN; } public String getMAC() { return MAC; } public void setMAC(String MAC) { this.MAC = MAC; } public String getDeviceType() { return deviceType; } public void setDeviceType(String deviceType) { this.deviceType = deviceType; } public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public Integer getDeviceState() { return deviceState; } public void setDeviceState(Integer deviceState) { this.deviceState = deviceState; } public Date getStorageTime() { return storageTime; } public void setStorageTime(Date storageTime) { this.storageTime = storageTime; } @Override public String toString() { return "DeviceExtend{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", SN='" + SN + '\'' + ", MAC='" + MAC + '\'' + ", deviceType='" + deviceType + '\'' + ", manufacturer='" + manufacturer + '\'' + ", storageTime=" + storageTime + ", deviceState=" + deviceState + ", deviceParameter='" + deviceParameter + '\'' + ", maintain='" + maintain + '\'' + '}'; } }
第四,分页实体类:
public class Page<T> { //总页数 private Long totalPage; //总记录数 private Long totalCount; //每页显示集合 private List<T> rows; public Long getTotalPage() { return totalPage; } public void setTotalPage(Long totalPage) { this.totalPage = totalPage; } public Long getTotalCount() { return totalCount; } public void setTotalCount(Long totalCount) { this.totalCount = totalCount; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } }
第五步,业务接口
/** * 自定义设备分页条件查询 * @param deviceExtend * @param currentPage * @param pageSize * @return */ public PagegetDeviceExtendPage(DeviceExtend deviceExtend,Integer currentPage,Integer pageSize,String from,String to);
第六步,业务实现类:
@Service public class DeviceExtendServiceImpl implements DeviceExtendService { //获得持久化操作对象 @Autowired private MongoTemplate mongoTemplate; /** * 自定义设备保存 * @param deviceExtend */ @Override public void save(DeviceExtend deviceExtend) { mongoTemplate.save(deviceExtend); } /** * 自定义设备分页条件查询 * * @param deviceExtend * @param currentPage * @param pageSize * @return */ @Override public PagegetDeviceExtendPage(DeviceExtend deviceExtend, Integer currentPage, Integer pageSize,String from,String to) { //创建查询对象 Query query=new Query(); //设置起始数 query.skip((currentPage-1)*pageSize); //设置查询条数 query.limit(pageSize); //创建条件对象 Criteria criteria=new Criteria(); if(StringUtils.isNotBlank(deviceExtend.getSN())){ //添加设备SN和MAC地址条件 criteria.orOperator(Criteria.where("SN").regex(deviceExtend.getSN()),Criteria.where("MAC").regex(deviceExtend.getSN())); } if(StringUtils.isNotBlank(deviceExtend.getName())){ //设备名的正则 Pattern pattern = Pattern.compile("^.*" + deviceExtend.getName() + ".*$", Pattern.CASE_INSENSITIVE); //添加设备名模糊查询条件 criteria.and("name").regex(pattern); } if(StringUtils.isNotBlank(from)&&StringUtils.isNotBlank(to)){ //添加时间段设备条件 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); try { criteria.and("storageTime").gte(format.parse(from)).lt(format.parse(to)); } catch (ParseException e) { e.printStackTrace(); } } if(StringUtils.isNotBlank(deviceExtend.getDeviceType())){ //设置设备类型条件 criteria.and("deviceType").is(deviceExtend.getDeviceType()); System.out.println("==="+deviceExtend.getDeviceType()); } if(StringUtils.isNotBlank(deviceExtend.getManufacturer())){ //设置厂商条件 criteria.and("manufacturer").is(deviceExtend.getManufacturer()); } if (deviceExtend.getDeviceState()!=null){ //设置设备状态 criteria.and("deviceState").is(deviceExtend.getDeviceState()); } query.addCriteria(criteria); //查询当前页数据集合 List deviceExtends = mongoTemplate.find(query, DeviceExtend.class); //查询总记录数 long count = mongoTemplate.count(query, DeviceExtend.class); //创建分页实体对象 Page page=new Page<>(); page.setRows(deviceExtends); page.setTotalCount(count); page.setTotalPage(count%pageSize==0?1:count/pageSize+1); return page; } /** * 自定义设备批量保存 * @param list */ @Override public void svaeBatch(List list) { if(list.size()>0){ for (DeviceExtend deviceExtend : list) { mongoTemplate.save(deviceExtend); } } } /** * 根据ID查询设备信息 * * @param id */ @Override public DeviceExtend findById(String id) { return mongoTemplate.findById(id,DeviceExtend.class); } /** * 根据设备ID修改设备状态 * * @param id * @param state */ @Override public void updateState(String id, Integer state) { Query query=new Query(); query.addCriteria(Criteria.where("id").is(id)); Update update= Update.update("deviceState",state); mongoTemplate.updateFirst(query,update,DeviceExtend.class); } }