|
@@ -6,7 +6,10 @@ import com.mongodb.client.result.DeleteResult;
|
|
|
import com.mongodb.client.result.UpdateResult;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.java.Log;
|
|
|
+import org.bson.types.ObjectId;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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;
|
|
@@ -14,6 +17,9 @@ import org.springframework.data.mongodb.core.query.Update;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
@RestController
|
|
|
@RequestMapping("/user")
|
|
|
@Api("用户接口")
|
|
@@ -57,5 +63,38 @@ public class UserController {
|
|
|
return mongoTemplate.upsert(query,update,User.class);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 分页模糊搜索用户列表
|
|
|
+ */
|
|
|
+ @ApiOperation("模糊搜索用户列表")
|
|
|
+ @GetMapping("/queryUser")
|
|
|
+ public List<User> list(@RequestParam(value="userName",required = false)String userName,
|
|
|
+ @RequestParam(value = "pageNo",required = false)int pageNo,
|
|
|
+ @RequestParam(value = "pageSize",required = false)int pageSize)
|
|
|
+ {
|
|
|
+ //如果有其他条件,先构造Criteria
|
|
|
+ Criteria criteria = Criteria.where("userName").regex(".*?\\" + userName + ".*");
|
|
|
+ // 构造查询query,通过_id排序,此处id如果保存mongo时自动生成,下面查询时需要转化成ObjectId,如果自己生成需要可以排序
|
|
|
+ Query query = Query.query(criteria).with(Sort.by(Sort.Direction.DESC, "_id"));
|
|
|
+ if(pageNo != 1){
|
|
|
+ // number参数是为了查上一页的最后一条数据
|
|
|
+ int number = (pageNo - 1) * pageSize;
|
|
|
+ query.limit(number);
|
|
|
+ List<User> users = mongoTemplate.find(query, User.class);
|
|
|
+ //获取最后一条数据
|
|
|
+ User user = users.get(users.size() - 1);
|
|
|
+ // 取到上一页的最后一条数据id,当作条件查询接下来的数据
|
|
|
+ String id = user.get_id();
|
|
|
+ // 此处id如果保存mongo时自动生成,查询时需要转化成ObjectId
|
|
|
+ ObjectId objectId = new ObjectId(id);
|
|
|
+ query.addCriteria(Criteria.where("_id").lt(objectId));
|
|
|
+ }
|
|
|
+ // 页大小重新赋值,覆盖number参数
|
|
|
+ query.limit(pageSize);
|
|
|
+ List<User> userList = mongoTemplate.find(query, User.class);
|
|
|
+
|
|
|
+ return userList;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|