论坛首页 Java企业应用论坛

bboss持久层分页接口使用示例

浏览 2461 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (15)
作者 正文
   发表时间:2012-10-22   最后修改:2012-10-23
bboss持久层分页接口比较有特色,提供了三种Style的分页接口:
第一种Style 根据sql语句直接分页,这种风格是bboss 3.6.0及之前版本一直沿用的接口
第二种Style 根据sql语句和外部传入的总记录数进行分页,这是bboss 3.6.1及之后版本提供的接口
第三种Style 根据sql语句和外部传入的总记录数sql语句进行分页,这是bboss 3.6.1及之后版本提供的接口

我们根据查询参数的传入方式,分别下面举例介绍三种Style。
1.准备工作-编写一个sql语句配置文件,用来演示三种Style
queryMaterialList为分页sql
queryCountMaterialList为查总记录数sql
<?xml version="1.0" encoding="UTF-8"?>
<properties>
	<property name="queryMaterialList">
	<![CDATA[
		select * from td_app_bom where id=#[id]
		]]>
	</property>
	
	<property name="queryCountMaterialList">
	<![CDATA[
		select count(1) from td_app_bom where id=#[id]
		]]>
	</property>
	
	
	<property name="queryMaterialListbindParam">
	<![CDATA[
		select * from td_app_bom where id=?
		]]>
	</property>
	
	<property name="queryCountMaterialListbindParam">
	<![CDATA[
		select count(1) from td_app_bom where id=?
		]]>
	</property>
</properties>


2.分页查询方法示例代码
public class ApplyService {

	private com.frameworkset.common.poolman.ConfigSQLExecutor executor = new ConfigSQLExecutor("com/frameworkset/sqlexecutor/purchaseApply.xml");
	
	/*******************************以bean方式传递查询条件开始*******************************/
	public ListInfo queryMaterailListInfoFirstStyleBean(int offset, int pagesize ,PurchaseApplyCondition condition) throws Exception {
		
		//执行分页查询,queryMaterialList对应分页查询语句,
		//根据sql语句在分页方法内部执行总记录数查询操作,这种风格使用简单,效率相对较低
		//condition参数保存了查询条件
		return executor.queryListInfoBean(HashMap.class, "queryMaterialList", offset, pagesize,condition);
	}
	
	public ListInfo queryMaterailListInfoSecondStyleBean(int offset, int pagesize ,PurchaseApplyCondition condition) throws Exception {
		//执行总记录查询并存入totalSize变量中,queryCountMaterialList对应一个优化后的总记录查询语句
		//condition参数保存了查询条件
		long totalSize = executor.queryObjectBean(long.class, "queryCountMaterialList", condition);
		//执行总记分页查询,queryMaterialList对应分页查询语句,通过totalsize参数从外部传入总记录数,
		//这样在分页方法内部无需执行总记录数查询操作,以便提升系统性能,这种风格使用简单,效率相对第一种风格较高,但是要额外配置总记录数查询sql
		//condition参数保存了查询条件
		return executor.queryListInfoBean(HashMap.class, "queryMaterialList", offset, pagesize,totalSize ,condition);
	}
	
	
	public ListInfo queryMaterailListInfoThirdStyleBean(int offset, int pagesize ,PurchaseApplyCondition condition) throws Exception {
		//根据sql语句和外部传入的总记录数sql语句进行分页,这种风格使用简单,效率最高,但是要额外配置总记录数查询sql
		ListInfo list = executor.queryListInfoBeanWithDBName(HashMap.class, "bspf","queryMaterialList", 0, 10,"queryCountMaterialList" ,condition);
		return list;
	}
	/*******************************以bean方式传递查询条件结束*******************************/
	
	/*******************************以传统绑定变量方式传递查询条件开始*******************************/
	public ListInfo queryMaterailListInfoFirstStyle(int offset, int pagesize ,String id) throws Exception {
		
		//执行分页查询,queryMaterialList对应分页查询语句,
		//根据sql语句在分页方法内部执行总记录数查询操作,这种风格使用简单,效率相对较低
		//id参数保存了查询条件
		return executor.queryListInfo(HashMap.class, "queryMaterialListbindParam", offset, pagesize,id);
	}
	
	public ListInfo queryMaterailListInfoSecondStyle(int offset, int pagesize ,String id) throws Exception {
		//执行总记录查询并存入totalSize变量中,queryCountMaterialList对应一个优化后的总记录查询语句
		//id参数保存了查询条件
		long totalSize = executor.queryObject(long.class, "queryCountMaterialListbindParam",id);
		//执行总记分页查询,queryMaterialList对应分页查询语句,通过totalsize参数从外部传入总记录数,
		//这样在分页方法内部无需执行总记录数查询操作,以便提升系统性能,这种风格使用简单,效率相对第一种风格较高,但是要额外配置总记录数查询sql
		//id参数保存了查询条件
		return executor.queryListInfoWithTotalsize(HashMap.class, "queryMaterialListbindParam", offset, pagesize,totalSize,id );
	}
	
	
	public ListInfo queryMaterailListInfoThirdStyle(int offset, int pagesize ,String id) throws Exception {
		//根据sql语句和外部传入的总记录数sql语句进行分页,这种风格使用简单,效率最高,但是要额外配置总记录数查询sql,id参数保存了查询条件
		ListInfo list = executor.queryListInfoWithDBName2ndTotalsizesql(HashMap.class, "bspf","queryMaterialListbindParam", 0, 10,"queryCountMaterialListbindParam",id );
		return list;
	}
	/*******************************以传统绑定变量方式传递查询条件结束*******************************/
}


举例完毕,如有疑问,请留言进一步探讨。
补充说明一下:ListInfo对象包含当页记录集和总记录数
   发表时间:2012-10-22  
api名称貌似有些太长了
0 请登录后投票
   发表时间:2012-10-23  
java_user 写道
api名称貌似有些太长了

用户的眼光总是很挑剔
0 请登录后投票
   发表时间:2012-10-23  
yin_bp 写道
java_user 写道
api名称貌似有些太长了

用户的眼光总是很挑剔

如此幽默的回复,总是让人神清气爽!不知道该框架是否有一整套完整的开发教程,或者UserGuide?
0 请登录后投票
   发表时间:2012-10-23   最后修改:2012-10-23
freezingsky 写道
yin_bp 写道
java_user 写道
api名称貌似有些太长了

用户的眼光总是很挑剔

如此幽默的回复,总是让人神清气爽!不知道该框架是否有一整套完整的开发教程,或者UserGuide?


bboss持久层的使用文档请关注博客中的相关介绍持久层的文章:
http://yin-bp.iteye.com/category/55607

在发布的最新稳定版本的/bestpractices下有个persistent工程就是持久层的示例工程,可以参考这个工程使用bboss的持久层
0 请登录后投票
   发表时间:2012-10-24   最后修改:2012-10-24
看看我的分页查询怎么样?

 

   /**
     * 分页查询上下文组装方法,处理页面上传递过来的查询参数,也在这个方法中进行。
     * @return 分页查询上下文对象
     */
    @Ignored
    protected PaginationQueryContext<UserInfo> getPQC() {
        PaginationQueryContext<UserInfo> pageQueryBean = this.createPQC();
        pageQueryBean.setBasePath("list");
        pageQueryBean.setPageSelectSqlId("UserInfo.getList");
        pageQueryBean.setCountSqlId("UserInfo.getCount");
        return pageQueryBean;
    }

    /**
     * 控制器方法:分页查询列表页
     */
    @Get
    @Post
    public Result list() {
        //从数据库中返回查询列表
         List<UserInfo> currentDataList = pagingService.doPagingQuery(getPQC());
        ac.addModel("currentDataList", currentDataList);

        return rf.view().setViewPath("user-list");
    }


pagingService 是控制器父类中自动注入的类实例,控制器中直接使用就可以。所有分页实现的细节,都在父类中封装好了。
0 请登录后投票
   发表时间:2012-10-24   最后修改:2012-10-24
你这个分页代码也挺清爽的,不晓得你查询返回的结构中包含了哪些信息?
另外你的分页查询结构脱离了你用的mvc框架还能不能独立使用呢?
0 请登录后投票
   发表时间:2012-10-24  
对我来说,方法名字长没事,最好一看方法名就知道这个方法想干啥,这是最爽的,因为到了这个程度,写上注释都感觉多此一举,不过注释最好还是要有.
支持国产框架,你们的付出多一份新鲜血液,膜拜和支持.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics