application.yml 配置:org.springframework.boot spring-boot-starter引入版本号 org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-data-elasticsearch引入版本号(要和spring-boot版本匹配上)
spring: data: elasticsearch: # ip 地址:端口号 cluster-nodes: "localhost:9300" cluster-name: "dawnopus-cluster" repositories: enabled: true实体类:
@Data @document(indexName = "searchindex", type = "bond") public class Data { // @Id: key唯一标识符 @Id private String id; // @Field(type = FieldType.Text): string @Field(type = FieldType.Text) private String name; }持久层(DAO)
import domain.Data; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; import org.springframework.data.domain.Pageable; @Repository public interface DataRepository extends ElasticsearchRepository { //根据 名称查询 List findByName(String name); //根据 名称分页查询 PageRequest pageRequest = PageRequest.of(0, 5); List findByName(String name,Pageable pageable); //根据 id,name,分页查询 用And或者Or链接 List findByIdAndName(String id,String name,Pageable pageable); }service层 接口
public interface DataService{ //根据 名称查询 List findByName(String name); //根据 名称分页查询 PageRequest pageRequest = PageRequest.of(0, 5); List findByName(String name,Pageable pageable); //根据 id,name,分页查询 用And或者Or链接 List findByIdAndName(String id,String name,Pageable pageable); }service层 实现
@Component public class DataServiceImpl implements DataService{ //持久层(DAO) @Autowired private DataRepository dataRepository ; //根据 名称查询 @Override public List findByName(String name){ return dataRepository.findByName(name); } //根据 名称分页查询 @Override public List findByName(String name,Pageable pageable){ //分页 0-5 PageRequest pageRequest = PageRequest.of(0, 5); return dataRepository.findByName(name,pageRequest); } //根据 id,name,分页查询 用And或者Or链接 @Override public List findByIdAndName(String id,String name,Pageable pageable){ //分页 0-5 PageRequest pageRequest = PageRequest.of(0, 5); return dataRepository.findByName(id,name,pageRequest); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)