|
| 1 | +package cn.iocoder.springboot.lab15.springdatasolr.repository; |
| 2 | + |
| 3 | +import cn.iocoder.springboot.lab15.springdatasolr.Application; |
| 4 | +import cn.iocoder.springboot.lab15.springdatasolr.dataobject.SolrProductDO; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.test.context.junit4.SpringRunner; |
| 10 | + |
| 11 | +@RunWith(SpringRunner.class) |
| 12 | +@SpringBootTest(classes = Application.class) |
| 13 | +public class ProductRepository02Test { |
| 14 | + |
| 15 | + @Autowired |
| 16 | + private ProductRepository02 productRepository; |
| 17 | + |
| 18 | + @Test // 根据名字获得一条记录 |
| 19 | + public void testFindByName() { |
| 20 | + SolrProductDO product = productRepository.findByName("芋道源码"); |
| 21 | + System.out.println(product); |
| 22 | + } |
| 23 | + |
| 24 | +// @Test // 使用 name 模糊查询,分页返回结果 |
| 25 | +// public void testFindByNameLike() { |
| 26 | +// // 根据情况,是否要制造测试数据 |
| 27 | +// if (false) { |
| 28 | +// testInsert(); |
| 29 | +// } |
| 30 | +// |
| 31 | +// // 创建排序条件 |
| 32 | +// Sort sort = Sort.by(Sort.Direction.DESC, "id"); // ID 倒序 |
| 33 | +// // 创建分页条件。 |
| 34 | +// Pageable pageable = PageRequest.of(0, 10, sort); |
| 35 | +// // 执行分页操作 |
| 36 | +// Page<SolrProductDO> page = productRepository.findByNameLike("芋道", pageable); |
| 37 | +// // 打印 |
| 38 | +// System.out.println(page.getTotalElements()); |
| 39 | +// System.out.println(page.getTotalPages()); |
| 40 | +// } |
| 41 | +// |
| 42 | +// /** |
| 43 | +// * 为了给分页制造一点数据 |
| 44 | +// */ |
| 45 | +// private void testInsert() { |
| 46 | +// for (int i = 1; i <= 100; i++) { |
| 47 | +// SolrProductDO product = new SolrProductDO(); |
| 48 | +// product.setId(i); // 一般 ES 的 ID 编号,使用 DB 数据对应的编号。这里,先写死 |
| 49 | +// product.setName("芋道源码:" + i); |
| 50 | +// product.setDescription("我只是一个描述"); |
| 51 | +// product.setCid(1); |
| 52 | +// product.setCategoryName("技术"); |
| 53 | +// productRepository.save(product); |
| 54 | +// } |
| 55 | +// } |
| 56 | + |
| 57 | +} |
0 commit comments