Dao层
public interface MongoDao extends Repository<Goods,Long> {
public Page<Goods> findByProductLike(String product, Pageable pageable);
}
测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestMongo {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 添加 一个
*/
@Test
public void test01() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", new Random().nextInt(1000000));
map.put("goodsName", "辣条");
map.put("brandName", "卫龙");
map.put("product", "食品");
mongoTemplate.save(map, "test4cl");
//mongoTemplate.createCollection()//创建集合
}
/**
* 添加 一个
*/
@Test
public void test02() {
Goods goods = new Goods();
goods.setId(new Random().nextInt(1000000));
goods.setBrandName("哈弗");
goods.setGoodsName("哈弗H9");
goods.setProduct("汽车");
goods.setCreateDate(new Date());
mongoTemplate.save(goods);
}
/**
* 存储多个 (list)
*/
@Test
public void test03() {
Goods goods = new Goods();
goods.setId(new Random().nextInt(1000000));
goods.setBrandName("宝马");
goods.setGoodsName("宝马X5");
goods.setProduct(&#