Jfinal分页问题

andying 发布于 2014/05/16 14:34
阅读 2K+
收藏 1

@JFinal 你好,想跟你请教个问题:

Jfinal目前好像只支持单表分页,如果针对多表关联查询应如何分页呢?thks.

加载中
1
andying
andying

经测试用Page<Student>还真可以,关联表的字段sexName,gradeName会包含中在Student中,

List<Student>  recs=pageRec.getList();

for (Student  student: recs) {
System.out.println("id:"+student.getId());
System.out.println("title:"+student.getStr("sexName"));
System.out.println("authName:"+student.getStr("gradeName"));
}

还真能取到关联表字段值,,终于ok了,但感觉当关联sql语名很复杂时,这样写好难看呦,都是用“+”号加起来的.

andying
andying
嗯,很好用,Jfinal后續能否再改進一下,sql能否放在配置文件里定義,不要用+字符拼接了。
JFinal
JFinal
感受到便捷之处了没? 这种解决表关联的方式 jfinal 称为无招胜有招,不需要学习新东西就会
0
JFinal
JFinal
提供 paginate 多表关联查询的 sql 就打完收工了
0
andying
andying

如:

select  t1.no,t1.name,t2.sexName,t1.age,t3.gradeName
 from student     t1
 left join  sex   t2   on t1.sexId=t2.id
 left join  grade t3   on t1.gradeId=t3.id



以上语句,应如何分页处理?  thks.
andying
andying
回复 @糊搞 : 能具体说说吗?谢谢.
糊搞
Db.pagenate(...)
0
andying
andying
以上在jfinal中能做到分页吗?
0
andying
andying

jfinal中单表分页如下:

Page<Gallery>  pageGallery=Gallery.dao.paginate(curPageNo,
pageNum,
" select * ",
" from gallery where 1=1");



对应以上,多表如何转换呢?我想了想作如下转换:

Page<Student>   pageRec=Student.dao.paginate(
				curPageNo,
				pageNum,
				" select * ",
				" from (" +
				"  select  t1.no,t1.name,t2.sexName,t1.age,t3.gradeName  from student     t1 " +
				"    left join  sex   t2   on t1.sexId=t2.id " +
				"    left join  grade t3   on t1.gradeId=t3.id )" +
				" tt where 1=1");



其中Page<Record>原本想用Page<Student>,想了想不对吧,我这是多表关联查询,又不是单表,如何返回Student呢?那就用Record类型,本想应该可以,但提示:

Type mismatch: cannot convert from Page<Student> to Page<Record> 

不知大家,这种场景是如何实现分页?这种情况在实际应用中应该来说很普遍呀,很少只针对一个表来分页的呀,除非你把所有相关字段都设计在一个表中,但企业应用中这种情况几乎没有呀,难道大家在用Jfinal时都没有这种应用场景?



0
andying
andying

看了一下jfinal源码,原来jfinal是把这个sql生成的ResultSet全部栏位值放入了List<M>中,所以我们就可以用get方法来获取你想要的栏位值了.


ResultSet rs = pst.executeQuery();
List<M> result = ModelBuilder.build(rs, modelClass);


ModelBuilder.build方法:



public static final <T> List<T> build(ResultSet rs, Class<? extends Model> modelClass) throws SQLException, InstantiationException, IllegalAccessException {
		List<T> result = new ArrayList<T>();
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		String[] labelNames = new String[columnCount + 1];
		int[] types = new int[columnCount + 1];
		buildLabelNamesAndTypes(rsmd, labelNames, types);
		while (rs.next()) {
			Model<?> ar = modelClass.newInstance();
			Map<String, Object> attrs = ar.getAttrs();
			for (int i=1; i<=columnCount; i++) {
				Object value;
				if (types[i] < Types.BLOB)
					value = rs.getObject(i);
				else if (types[i] == Types.CLOB)
					value = handleClob(rs.getClob(i));
				else if (types[i] == Types.NCLOB)
					value = handleClob(rs.getNClob(i));
				else if (types[i] == Types.BLOB)
					value = handleBlob(rs.getBlob(i));
				else
					value = rs.getObject(i);
				
				attrs.put(labelNames[i], value);
			}
			result.add((T)ar);
		}
		return result;
	}




返回顶部
顶部