<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                **1. 封裝一個實體類** ```java @Data @Builder @NoArgsConstructor @AllArgsConstructor @Document(indexName = "product", shards = 3, replicas = 1) public class Product { /** 產品唯一標識 */ @Id private Long id; /** 產品名稱 */ @Field(type = FieldType.Text) private String title; /** 產品品牌 */ @Field(type = FieldType.Keyword) private String brand; /** 產品價格 */ @Field(type = FieldType.Double) private Double price; /** 產品圖片地址 */ @Field(type = FieldType.Text, index = false) private String images; } ``` **2. 繼承接口ElasticsearchRepository** ```java /** * 繼承接口ElasticsearchRepository<實體類, 實體類中的ID類型>,便可以調用接口的 * 方法與Elasticsearch交互了。 * * 在接口中聲明的方法的方法名是有一定規律的, * 查詢以 find開始、新增和更新以save開始、刪除以delete開始、統計以count開始、存在exists開始。 * 聲明一個方法按照從左到右編寫的話,IDEA是可以給出參考的方法名的。 */ @Repository public interface ProductDao extends ElasticsearchRepository<Product, Long> { /** 根據產品名稱查詢產品 */ List<Product> findByTitle(String title); /** 查詢價格在[price1, price2]之間的產品 */ List<Product> findByPriceBetween(double price1, double price2); /** 根據品牌查詢 */ List<Product> findByBrand(String brand); /** 根據品牌查詢,然后按照價格降序排序 */ List<Product> findByBrandOrderByPriceDesc(String brand); /** 根據品牌與價格區間查詢 */ List<Product> findByBrandAndPriceBetween(String brand, double price1, double price2); } ``` 更過命名規則參考官方文檔:https://docs.spring.io/spring-data/elasticsearch/docs/4.2.6/reference/html/#elasticsearch.query-methods.criterions **3. 調用接口中的方法** ```java @RunWith(SpringRunner.class) @SpringBootTest(classes = {SpringDataEsApplication.class}) public class ProductControllerTest { @Autowired private ProductDao productDao; /** * 新增或更新。 * 新增與更新都是調用save方法,沒有該條數據時是新增,已經有了就是更新。 */ @Test public void saveDocument() { Product product = Product.builder() .id(1001L) .title("華為 Mate 40 Pro 4G版") .brand("華為") .price(5899.0d) .images("https://res.xx455CB694209CB8A2mp.png") .build(); Product result = productDao.save(product); } /** * 刪除文檔。 */ @Test public void deleteDocument() { Product product = Product.builder().id(1001L).build(); productDao.delete(product); } /** * 批量新增文檔。//調用deleteAll便可實現批量刪除 */ @Test public void saveAll() { List<Product> products = new ArrayList<>(); products.add(new Product(1001L, "華為 Mate 30 Pro 4G版", "華為", 1000.0d, "http://1.jpg")); products.add(new Product(1002L, "華為 Mate 40 Pro 4G版", "華為", 2000.0d, "http://2.jpg")); products.add(new Product(1003L, "華為 Mate 50 Pro 4G版", "華為", 3000.0d, "http://3.jpg")); Iterable<Product> iterable = productDao.saveAll(products); } /** * 查詢所有。 * 根據id字段升序排序 */ @Test public void findAll() { Iterable<Product> iterable = productDao.findAll(Sort.by(Sort.Direction.ASC, "id")); iterable.forEach(product -> { System.out.println(product.toString()); }); //Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg) //Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg) //Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg) } /** * 根據id查詢產品 */ @Test public void findById() { Product product = Product.builder().id(1001L).build(); Product result = productDao.findById(product.getId()).get(); System.out.println(result); //Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg) } /** * 按照產品名稱查詢。 */ @Test public void findByTitle() { List<Product> products = productDao.findByTitle("華為 Mate 30 Pro 4G版"); products.forEach(System.out::println); //Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg) } /** * 按照價格區間查詢。 */ @Test public void findByPriceBetween() { List<Product> products = productDao.findByPriceBetween(1000, 3000); products.forEach(System.out::println); //Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg) //Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg) //Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg) } /** * 按照品牌查詢。 */ @Test public void findByBrand() { List<Product> products = productDao.findByBrand("華為"); products.forEach(System.out::println); //Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg) //Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg) //Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg) } /** *按照品牌查詢,然后按照價格降序排序 */ @Test public void findByBrandOrderByPriceDesc() { List<Product> products = productDao.findByBrandOrderByPriceDesc("華為"); products.forEach(System.out::println); //Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg) //Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg) //Product(id=1001, title=華為 Mate 30 Pro 4G版, brand=華為, price=1000.0, images=http://1.jpg) } /** * 根據品牌與價格區間查詢。 */ @Test public void findByBrandAndPriceBetween() { List<Product> products = productDao.findByBrandAndPriceBetween("華為", 2000, 3000); products.forEach(System.out::println); //Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg) //Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg) } /** * 分頁查詢 */ @Test public void findByPageable() { //根據價格降序排序 Sort sort = Sort.by(Sort.Direction.DESC, "price"); int currentPage = 0; //從第0頁開始 int pageSize = 2; //每一頁顯示2條數據 PageRequest request = PageRequest.of(currentPage, pageSize, sort); Page<Product> products = productDao.findAll(request); products.forEach(System.out::println); //Product(id=1003, title=華為 Mate 50 Pro 4G版, brand=華為, price=3000.0, images=http://3.jpg) //Product(id=1002, title=華為 Mate 40 Pro 4G版, brand=華為, price=2000.0, images=http://2.jpg) } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看