package com.liupei.controller; import com.liupei.entity.Fruit; import com.liupei.entity.User; import com.mongodb.WriteResult; import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; import org.bson.Document; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; /** * TODO * * @author liu pei * @version 1.0 * @mail 3268727800@qq.com * @date 2020/11/17 22:07 */ @RestController @RequestMapping("/user") public class UserController { @Autowired private MongoTemplate mongoTemplate; /** * 单属性保存 * @param user * @return */ @RequestMapping("/save") public Object save(@RequestBody User user) { return mongoTemplate.save(user); } /** * 多属性保存 * @param fruit * @return */ @RequestMapping("/save/attr") public Object saveAttr(@RequestBody Fruit fruit) { return mongoTemplate.save(fruit); } /** * 根据姓名查询 * @param name * @return */ @GetMapping(value = "/test2") public User findTestByName(@RequestParam(value = "name") String name) { Query query = new Query(Criteria.where("name").is(name)); User user = mongoTemplate.findOne(query, User.class); return user; } /** * 使用in模糊查询类似于SQL中的IN * 多属性查询:根据名称查询 * @param colo * @return */ @GetMapping(value = "/colo") public Object findByColoer(@RequestParam(value = "colo") String colo) { Query query = new Query(Criteria.where("attr.name").in(colo)); List<Fruit> fruits = mongoTemplate.find(query, Fruit.class); return fruits; } /** * all * 多属性查询:根据名称查询 * @param colo * @return */ @GetMapping(value = "/colo/all") public Object findByColoerAll(@RequestParam(value = "colo") String colo) { Query query = new Query(Criteria.where("attr.name").all(colo)); List<Fruit> fruitList = mongoTemplate.find(query, Fruit.class); return fruitList; } /** * 根据id查询 * @param id * @return */ @GetMapping(value = "/find/id") public User findById(@RequestParam(value = "id") String id) { Query query = new Query(Criteria.where("_id").is(id)); User byId = mongoTemplate.findById(query, User.class); return byId; } /** * 根据id删除 * @param id * @return */ @GetMapping(value = "/del/id") public Object del(@RequestParam(value = "id") String id) { Query query = new Query(Criteria.where("_id").is(id)); DeleteResult remove = mongoTemplate.remove(query, User.class); return remove; } /** * 更新实体 * @param user * @return */ @RequestMapping("/update") public Object update(@RequestBody User user) { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(user.getId())); Document doc = new Document(); mongoTemplate.getConverter().write(user,doc); Update update = Update.fromDocument(doc, "_id"); UpdateResult updateResult = mongoTemplate.upsert(query, update, User.class); return mongoTemplate.save(updateResult); } /** * 更新某个属性 * @param user * @return */ @RequestMapping("/update/atrr") public Object updateAttr(@RequestBody User user) { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(user.getId())); //_id区分引号 "1"和1 Update update = Update.update("name", user.getName()); // WriteResult upsert = mongoTemplate.updateMulti(query, update, "userList"); //查询到的全部更新 // WriteResult upsert = mongoTemplate.updateFirst(query, update, "userList"); //查询更新第一条 UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);//有则更新,没有则新增 //返回执行的条数 return upsert; } /** * 根据名字模糊查询 * @param name * @return */ @GetMapping(value = "/find/name/page") public Object findByNamePage(@RequestParam(value = "name") String name) { Query query = new Query(); Criteria criteria = new Criteria(); Pattern pattern = Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE); //姓名模糊检索 query.addCriteria(criteria.where("name").regex(pattern)); //模糊查询名字 List<User> users = mongoTemplate.find(query, User.class); return users; } /** * 多条件:根据名字模糊查询,年龄6-18 * @param name * @return */ @GetMapping(value = "/find/like/page") public Object findByNameAgePage(@RequestParam(value = "name") String name) { Query query = new Query(); Criteria criteria = new Criteria(); //年龄 6-18 query.addCriteria(criteria.where("age").gte(6).lte(18)); //姓名模糊检索 query.addCriteria(criteria.where("name").regex(name)); //模糊查询名字 List<User> users = mongoTemplate.find(query, User.class); return users; } @GetMapping(value = "/page") public Object page(@RequestParam(value = "name") String name) { Map<String, Object> resultMap = new HashMap<>(); Query query = new Query(); Criteria criteria = new Criteria(); Pattern pattern = Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE); //姓名模糊检索 query.addCriteria(criteria.where("name").regex(pattern)); int totalCount = (int)mongoTemplate.count(query, User.class); int currentPage = 0; // 设置查询条数 int pageSize = 10; // 设置起始数 query.skip((currentPage - 1) * pageSize).limit(pageSize); // 数据总页数 int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1; // 设置记录总数和总页数 resultMap.put("totalCount", totalCount); resultMap.put("totalPage", totalPage); // 按创建时间倒序 query.with(new Sort(Sort.Direction.DESC, "age")); // 查询当前页数据集合 List<User> records = mongoTemplate.find(query, User.class); resultMap.put("data", records); return resultMap; } }