【Spring】SpringData Elasticsearch 基本分页查询

  • Entity
package cn.edu.zucc.syx.rec.demo;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

//文档对象
@Document(indexName = "table01", type = "table033")
public class  Table01 {
    @Id
    @Field(type = FieldType.Integer)
    private Integer id;

    @Field(type = FieldType.Text)
    private String title;

    //type:数据类型
    @Field(type = FieldType.Text)
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Table01{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}
  • Dao
package cn.edu.zucc.syx.rec.demo;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface TableDao extends ElasticsearchRepository {
    //根据标题查询(含分页)
    Page findByTitle(String condition, Pageable pageable);
}
  • Service接口
package cn.edu.zucc.syx.rec.demo;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface TableServiceTest {
    //添加
    void save(Table01 table01);

    //分页查询
    Page findAll(Pageable pageable);
}
  • Service实现类
package cn.edu.zucc.syx.rec.demo;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TableServiceImplTest implements TableServiceTest {

    @Resource
    private TableDao dao;

    @Override
    public void save(Table01 table01) {
        dao.save(table01);
    }

    @Override
    public Page findAll(Pageable pageable) {
        return dao.findAll(pageable);
    }
}
  • Controller
package cn.edu.zucc.syx.rec.demo;

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.web.bind.annotation.*;


@RestController
@RequestMapping("/demo")
public class TableController {

    @Autowired
    private TableServiceTest service;

    @GetMapping("/demo")
    public void findAllPage(){
        Pageable pageable= PageRequest.of(0,5);
        Page page = service.findAll(pageable);
        for (Table01 table01:page.getContent()){
            System.out.println(table01);
        }
    }

    @PostMapping("/demo")
    public void save20(){
        for (int i=1;i<20;i++){
            Table01 table01=new Table01();
            table01.setId(i);
            table01.setTitle(i+"ES版本");
            table01.setContent(i+"基于Lucene搜索服务器");
            service.save(table01);
        }
    }
}

你可能感兴趣的:(Spring)