《 mongodb 学习 》java 基本操作 增删改,分页模糊精准查询
配置
spring: data: mongodb: uri: mongodb://localhost:27017 database: oreal·
实体类
@Document(collection = "tes_namespace") public class Namespace { @Id private String id; //@Field("name") private String name; //@Field("code") private String code; //@Field("description") private String description;
自定义主键ID
没有添加 @Field 也会生成
一、添加
@ApiOperation(value = "添加") @GetMapping(value = "/add/{id}") public LYResultVO add(@PathVariable("id") String id) throws URISyntaxException { return namespaceService.add(id); } @Autowired public MongoTemplate mongoTemplate; public LYResultVO add(String id) { Namespace ns = new Namespace(); ns.setCode("code" + id); ns.setDescription("desc" + id); ns.setName("name"+ id); ns.setId(id); ns.setCreatTime(new Date()); mongoTemplate.save(ns); return LYResultVO.successResultVO(ns.toString(),"success"); }
二、删除
@ApiOperation(value = "删除") @GetMapping(value = "/del/{id}") public LYResultVO del(@PathVariable("id") String id) throws URISyntaxException { return namespaceService.del(id); } public LYResultVO del(String id) { Query query=new Query(Criteria.where("id").is(id)); mongoTemplate.remove(query,Namespace.class); return LYResultVO.successResultVO("", "删除成功"); }
三、精准查询
@ApiOperation(value = "精准查询") @GetMapping(value = "/get/{id}") public LYResultVO get(@PathVariable("id") String id) throws URISyntaxException { return namespaceService.queryNamespaceById(id); } public LYResultVO queryNamespaceById(String id) { Query query = new Query(); query.addCriteria(Criteria.where("id").is(id)); Namespace namespace = mongoTemplate.findOne(query,Namespace.class); return LYResultVO.successResultVO(namespace, ""); }
四、模糊查询
@ApiOperation(value = "模糊查询") @GetMapping(value = "/query/{name}") public LYResultVO query(@PathVariable("name") String name) throws URISyntaxException { return namespaceService.queryNamespace(name); } public LYResultVO queryNamespace(String name) { Query query = new Query(); if(StringUtils.isNotEmpty(name)){ query.addCriteria(Criteria.where("name").regex(Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE))); } query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "creatTime"))); List<Namespace> list = mongoTemplate.find(query,Namespace.class); return LYResultVO.successResultVO(list,"success"); }
五、分页
@ApiOperation(value = "分页查询") @GetMapping(value = "/limitquery/{pageNo}") public LYResultVO query(@PathVariable("pageNo") int pageNo) throws URISyntaxException { return namespaceService.queryNamespaceBypage(pageNo); } public LYResultVO queryNamespaceBypage(int pageNo) { Query query = new Query(); int limit =10; int skip = (pageNo - 1) * limit; query.skip(skip);// 从那条记录开始 query.limit(limit);// 取多少条记录 List<Namespace> list = mongoTemplate.find(query,Namespace.class); return LYResultVO.successResultVO(list,"success"); }
六、日期判断
@ApiOperation(value = "日期查询") @GetMapping(value = "/querydate") public LYResultVO querydate() throws URISyntaxException { return namespaceService.queryNamespaceByDate(); } public LYResultVO queryNamespaceByDate() { Query query = new Query(); query.addCriteria(Criteria.where("creatTime").lte(new Date())); List<Namespace> list = mongoTemplate.find(query,Namespace.class); return LYResultVO.successResultVO(list,"success"); }