赞
踩
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
spring.elasticsearch.rest.uris=http://localhost:9200
@Document(indexName = "znsd",type = "student") public class Student { private Integer id; private String name; private String aihao; private String sex; public Student(Integer id, String name, String aihao, String sex) { this.id = id; this.name = name; this.aihao = aihao; this.sex = sex; } public Student() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAihao() { return aihao; } public void setAihao(String aihao) { this.aihao = aihao; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", aihao='" + aihao + '\'' + ", sex='" + sex + '\'' + '}'; } }
public interface StudentRepository extends ElasticsearchRepository<Student,Integer> {
//根据书名搜索书籍的方法(类似jpa中的方法命名规则)
//查询名字
List<Student> getListByName(String name);
//查询性别
List<Student> getListBySex(String sex);
//查询爱好
List<Student> getListByAihao(String aihao);
//查询所有
List<Student> getAllBy();
}
@Autowired ElasticsearchRestTemplate elasticsearchRestTemplate; @Test void contextLoads() { Student student1=new Student(1,"中","打篮球","女"); IndexQuery indexQuery =new IndexQueryBuilder() .withId(student1.getId().toString()) .withObject(student1) .build(); elasticsearchRestTemplate.index(indexQuery); Student student2=new Student(2,"中国人","打乒乓球","男"); IndexQuery indexQuery2 =new IndexQueryBuilder() .withId(student2.getId().toString()) .withObject(student2) .build(); elasticsearchRestTemplate.index(indexQuery2); elasticsearchRestTemplate.queryForObject(GetQuery.getById("2"),Student.class); Student student3=new Student(3,"中国话","打高尔夫","男"); IndexQuery indexQuery3 =new IndexQueryBuilder() .withId(student3.getId().toString()) .withObject(student3) .build(); elasticsearchRestTemplate.index(indexQuery3); }
@Test
void test3(){
List<Student> student=studentRepository.getListByName("中");
for (Student student1:student) {
System.out.println(student1);
}
}
Student{id=1, name='中', aihao='打篮球', sex='女'}
Student{id=3, name='中国话', aihao='打高尔夫', sex='男'}
Student{id=2, name='中国人', aihao='打乒乓球', sex='男'}
@Test void test5(){ //分页参数 int page = 0; int size = 1; // 构建查询条件 NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); // 查询id倒序查询 builder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC)); //模糊查询条件 builder.withQuery(QueryBuilders.matchPhraseQuery("name", "中")); //分页 builder.withPageable(PageRequest.of(page, size)); //查询 Page<Student> page123 =studentRepository.search(builder.build()); //总记录数 System.out.println(page123.getTotalElements()); //当前页数 System.out.println(page123.getTotalPages()); }
<!--quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Component public class QuartzDemo { @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次 public static void work() throws Exception { System.out.println("执行调度任务:"+new Date()); } @Scheduled(fixedRate = 5000)//每5秒执行一次 public static void play() throws Exception { System.out.println("执行Quartz定时器任务:"+new Date()); } @Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次 public static void doSomething() throws Exception { System.out.println("每2秒执行一个的定时任务:"+new Date()); } @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次 public static void goWork() throws Exception { System.out.println("每一小时执行一次的定时任务:"+new Date()); } @Scheduled(cron = "0 37 17 * * ?") // 到17点37执行 public static void date() throws Exception { System.out.println("到了17点37执行:"+new Date()); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。