1、通过Jpa查询(带参数)分页
1.1)Java
/** * *方式一:基于MongoRepository *优势:方便简单,方法实现由工具类完成。 *缺点:不适合多个可变查询条件。 */
Controller
@RequestMapping("selectStudentPagerByJpa")
@ResponseBody
public Object selectStudentPagerByJpa(@RequestParam(value = "page") String page,@RequestParam(value = "rows") String rows) {
Map<String,Object> result = new HashMap<String,Object>();
try {
Page<StudentBo> studentBos = studentService.queryAllByPage(Integer.parseInt(page),Integer.parseInt(rows));
List<StudentBo> studentBoList = studentBos.getContent();
for (StudentBo studentBo : studentBoList) {
String courseName = null;
List<CourseBo> courses = studentBo.getCourses();
for (CourseBo course : courses) {
if (StringUtils.isBlank(courseName)) {
courseName = course.getCname();
} else {
courseName += "," + course.getCname();
}
}
studentBo.setCoursesName(courseName);
}
result.put("total",studentBos.getTotalElements());
result.put("rows",studentBos.getContent());
} catch (Exception e) {
log.error(e);
}
return result;
}
Service
public Page<StudentBo> queryAllByPage(int page,int rows) throws Exception {
PageRequest pageRequest = new PageRequest(page - 1,rows,new Sort(new Sort.Order(Sort.Direction.DESC,"id")));
return iStudentDao.findAll(pageRequest);
}
dao
@Repository
public interface IStudentDao extends MongoRepository<StudentBo,ObjectId> {
public Page<StudentBo> findBySnameLike(String Sname,Pageable pageable);
}
带分页的模糊查询
Controller
@RequestMapping("findBySnameLike")
@ResponseBody
public Object findBySnameLike(@RequestParam(value = "page",defaultValue = "1") String page,@RequestParam(value = "rows",defaultValue = "10") String rows,@RequestParam(value = "sname") String sname) {
Map<String,Object>();
try {
Page<StudentBo> studentBos = studentService.findBySnameLike(Integer.parseInt(page),Integer.parseInt(rows),sname);
result.put("total",studentBos.getContent());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
service
public Page<StudentBo> findBySnameLike(int page,int rows,String sname) {
Pageable pageRequest = new PageRequest(page-1,"id")));
return iStudentDao.findBySnameLike(sname,pageRequest);
}
dao
@Repository
public interface IStudentDao extends MongoRepository<StudentBo,ObjectId> {
public Page<StudentBo> findBySnameLike(String sname,Pageable pageRequest);
}
2、通过mongoTemplate聚合查询(带参数)分页
2.1)mongo原生查询语句
2.2)Java
/** * *方式二:通过mongoTemplate聚合查询分页 *优势:方便简单,方法实现由工具类完成。 *缺点:适合多个可变查询条件。 */
Controller
@RequestMapping("selectStudentPagerByMongoTemplate")
@ResponseBody
public Object selectStudentPagerByMongoTemplate(
@RequestParam(value = "page") String page,@RequestParam(value = "rows") String rows,@RequestParam(value = "sname",defaultValue = "") String sname) {
Map<String,Object>();
try {
//方式一:通过mongoTemplate聚合查询分页
PagerBo pagerBo = new PagerBo(Integer.parseInt(page),Integer.parseInt(rows));
StudentBo student = new StudentBo();
student.setSname(sname);
pagerBo = studentService.getStudentPagerByMongoTemplate(student,pagerBo);
List<StudentBo> studentBoList = pagerBo.getResult();
for (StudentBo studentBo : studentBoList) {
String courseName = null;
List<CourseBo> courses = studentBo.getCourses();
for (CourseBo course : courses) {
if (StringUtils.isBlank(courseName)) {
courseName = course.getCname();
} else {
courseName += "," + course.getCname();
}
}
studentBo.setCoursesName(courseName);
}
result.put("total",pagerBo.getTotal());
result.put("rows",studentBoList);
} catch (Exception e) {
log.error(e);
}
return result;
}
Service
public PagerBo getStudentPagerByMongoTemplate(StudentBo studentBo,PagerBo pagerBo) {
return studentDao.getStudentPagerByMongoTemplate(studentBo,pagerBo);
}
Dao
public PagerBo getStudentPagerByMongoTemplate(StudentBo studentBo,PagerBo pagerBo) {
List<AggregationOperation> operations = new ArrayList<AggregationOperation>();
operations.add(Aggregation.skip(pagerBo.getStart()));
operations.add(Aggregation.limit(pagerBo.getRows()));
operations.add(Aggregation.lookup("course","_id","students._id","courses"));
operations.add(Aggregation.sort(Sort.Direction.DESC,"id"));
if (StringUtils.hasText(studentBo.getSname())) {
operations.add(Aggregation.match(Criteria.where("sname").
regex(".*?\\" + studentBo.getSname() + ".*")));
}
Aggregation aggregation = Aggregation.newAggregation(operations);
/*Aggregation aggregation = Aggregation.newAggregation( Aggregation.skip(pagerBo.getStart()),Aggregation.limit(pagerBo.getRows()),Aggregation.lookup("course","_id","students._id","courses"),Aggregation.sort(Sort.Direction.DESC,"id") //,Aggregation.match(Criteria.where("sname").is(studentBo.getSname())) );*/
long total = mongoTemplate.count(new Query(),StudentBo.class);
AggregationResults<StudentBo> results = mongoTemplate.aggregate(aggregation,"student",StudentBo.class);
pagerBo.setResult(results.getMappedResults());
pagerBo.setTotal(total);
return pagerBo;
}
3.Jsp(通过easyui实现)
3.1)列表显示
<table id="dg" title="学生列表" class="easyui-datagrid" style="width:700px;height:250px" url="selectStudentPagerByMongoTemplate.do" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr>
<th field="sname" width="50">学生姓名</th>
<th field="coursesName" width="50">课程名</th>
</tr>
</thead>
</table>
3.2)列表查询
Jsp
<input id="sname" class="easyui-searchBox" style="width:300px" data-options="searcher:findByStudentNameLike,prompt:'请输入学生姓名'"></input>
Js
//实现方式一()[参考地址]http://www.jeasyui.net/tutorial/22.html)
function findByStudentNameLike() {
$('#dg').datagrid('load',{
sname: $('#sname').val()
});
}
//实现方式二(不推荐使用:需要重新写一个带参数的分页查询)
var sname= $('#sname').val();
$("#dg").datagrid({
title: '用户信息',iconCls: 'icon-ok',pageSize: 10,pageList: [5,10,15,20],nowrap: true,striped: true,collapsible: true,toolbar: "#easyui_toolbar",url: 'findBySnameLike.do?sname=' + sname,//触发此action,带上参数searcValue
loadMsg: '数据加载中......',fitColumns: true,//允许表格自动缩放,以适应父容器
sortName: 'id',sortOrder: 'asc',remoteSort: false,columns: [[{
field: 'id',title: 'id',hidden: true
},{
field: 'sname',title: '学生姓名',width: '165px'
},{
field: 'coursesName',title: '课程名',width: '165px'
}
]],pagination: true,rownumbers: true
})
原文链接:https://www.f2er.com/javaschema/283376.html个人心得:选用的方式看:是否适合多个可变查询条件。