【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<Table01, Integer> {
    //根据标题查询(含分页)
    Page<Table01> 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<Table01> 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<Table01> 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<Table01> 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);
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值