jpa可以自定义接口查询,也就意味着自己在mappe按照要求写一个方法,service只需要调用传值就可以了
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
spring:
elasticsearch:
rest:
uris: http://localhost:9200
@Data
@Document(indexName = "goods")
public class Goods implements Serializable {
@Field(type = FieldType.Keyword)
private String id;
@Field(type = FieldType.Text)
private String goodsName;
@Field(type = FieldType.Integer)
private Integer store;
@Field(type =FieldType.Double)
private double price;
}
@Repository
public interface GoodsDao extends ElasticsearchRepository<Goods,String> {
}
@SpringBootApplication
public class ElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class, args);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ElasticsearchApplication.class)
public class ElastricsearchTest {
//注入dao
@Autowired
private GoodsDao goodsDao;
/**
* @Description: 插入一个
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testCreate(){
Goods goods = new Goods("1","springoot整合",12,300.0);
goodsDao.save(goods);
}
/**
* @Description: 插入多个
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testCerateMany(){
ArrayList<Goods> list = new ArrayList<>();
Goods goods = new Goods("2","springoot整合2",12,300.0);
Goods goods1 = new Goods("3","springoot整合3",12,300.0);
list.add(goods);
list.add(goods1);
goodsDao.saveAll(list);
}
/**
* @Description: 根据id查询
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testFindById(){
Optional<Goods> goods = goodsDao.findById("2");
System.out.println("goods.get() = " + goods.get());
}
/**
* @Description: 根据所有查询
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testFindAll(){
goodsDao.findAll().forEach((z)-> System.out.println("z = " + z));
}
/**
* @Description: 修改
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testUpdate(){
Goods goods = new Goods("3","springoot整合2",12,200.0);
goodsDao.save(goods);
}
/**
* @Description: 排序查询
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testSort(){
ArrayList<String> list= new ArrayList<>();
list.add("price");
Sort orders = new Sort(Sort.Direction.ASC,list);
goodsDao.findAll(orders);
}
/**
* @Description: 分页查询
* @Author: Mr.Zhan
* @Date: 2022/2/14
*/
@Test
public void testPageResult(){
Pageable pageable = Pageable.unpaged();
goodsDao.findAll(Pageable.unpaged()).get().forEach((s)-> System.out.println("s = " + s));
}
}