注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

享受编码

    的乐趣

 
 
 

日志

 
 

ibatis查询oracle分页  

2011-09-04 09:32:59|  分类: ibatis |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

开发采用spring+struts+ibatis+extjs,数据库用oracle

ibtais配置分页 模糊查询

Java代码
  1. <sqlMap namespace="RYDM" >
  2. <typeAlias alias="page" type="com.portal.util.Page" />
  3. <typeAlias alias="abatorgenerated_RydmResult" type="com.portal.model.Rydm" />
  4. <resultMap id="abatorgenerated_RydmResult" class="com.portal.model.Rydm" >
  5. <result column="RYDM" property="rydm" jdbcType="VARCHAR" />
  6. <result column="RYMC" property="rymc" jdbcType="VARCHAR" />
  7. <sql id="findByPageCondition">
  8. <isNotEmpty property="objCondition">
  9. <isNotEmpty property="objCondition.bmzdm">
  10. <![CDATA[
  11. bmzdm LIKE '%$objCondition.bmzdm$%'
  12. ]]>
  13. </isNotEmpty>
  14. <isNotEmpty property="objCondition.rymc">
  15. <![CDATA[
  16. AND rymc LIKE '%$objCondition.rymc$%'
  17. ]]>
  18. </isNotEmpty>
  19. </isNotEmpty>
  20. </sql>
  21. <select id="findByPage" parameterClass="page"
  22. resultClass="abatorgenerated_RydmResult">
  23. SELECT * FROM (SELECT row_.*, rownum rownum_ FROM
  24. (select ry.* from RYDM ry
  25. where 1=1
  26. <dynamic prepend="AND">
  27. <include refid="findByPageCondition" />
  28. </dynamic>
  29. )row_ WHERE rownum &lt;= $limit$)row_ WHERE rownum_ &gt;$start$
  30. ORDER BY rydm
  31. </select>
  32. <select id="findByCount" parameterClass="page" resultClass="int">
  33. <![CDATA[
  34. SELECT COUNT(*) FROM RYDM ry where 1=1
  35. ]]>
  36. <dynamic prepend="AND">
  37. <include refid="findByPageCondition" />
  38. </dynamic>
  39. </select>
<sqlMap namespace="RYDM" >  <typeAlias alias="page" type="com.portal.util.Page" />  <typeAlias alias="abatorgenerated_RydmResult" type="com.portal.model.Rydm" />    <resultMap id="abatorgenerated_RydmResult" class="com.portal.model.Rydm" >   <result column="RYDM" property="rydm" jdbcType="VARCHAR" />      <result column="RYMC" property="rymc" jdbcType="VARCHAR" />       <sql id="findByPageCondition">    <isNotEmpty property="objCondition">         <isNotEmpty property="objCondition.bmzdm">      <![CDATA[        bmzdm LIKE '%$objCondition.bmzdm$%'         ]]>         </isNotEmpty>         <isNotEmpty property="objCondition.rymc">      <![CDATA[          AND rymc LIKE '%$objCondition.rymc$%'             ]]>         </isNotEmpty>    </isNotEmpty>   </sql>   <select id="findByPage" parameterClass="page"                                resultClass="abatorgenerated_RydmResult">              SELECT * FROM (SELECT row_.*, rownum rownum_ FROM                      (select ry.* from RYDM ry                        where 1=1            <dynamic prepend="AND">     <include refid="findByPageCondition" />    </dynamic>                     )row_ WHERE rownum &lt;= $limit$)row_ WHERE rownum_ &gt;$start$      ORDER BY rydm   </select>   <select id="findByCount" parameterClass="page" resultClass="int">    <![CDATA[     SELECT COUNT(*) FROM RYDM ry   where 1=1    ]]>    <dynamic prepend="AND">     <include refid="findByPageCondition" />    </dynamic>   </select>

分页模型通用类

Java代码
  1. public class Page implements java.io.Serializable{
  2. public void setPageProperty(Page page) {
  3. if(page.getStart()>0){
  4. // 设置取数据的区间
  5. int endPage=page.getStart()+page.getLimit();
  6. int stratPage=page.getStart();
  7. this.setStart(stratPage);
  8. this.setLimit(endPage);
  9. }else{
  10. this.setLimit(page.getLimit());
  11. this.setStart(0);
  12. }
  13. }
  14. /** 总记录数 */
  15. private int totalProperty;
  16. /** 分页结果 */
  17. private List root;
  18. /** 开始页码 */
  19. private int start;
  20. /** 每页多少 */
  21. private int limit;
  22. /** 成功与否 */
  23. private boolean success;
  24. /** 查询条件 */
  25. private Object objCondition;
public class Page implements java.io.Serializable{     public void setPageProperty(Page page) {    if(page.getStart()>0){    // 设置取数据的区间    int endPage=page.getStart()+page.getLimit();     int stratPage=page.getStart();    this.setStart(stratPage);    this.setLimit(endPage);    }else{     this.setLimit(page.getLimit());     this.setStart(0);    }   }     /** 总记录数 */   private int totalProperty;     /** 分页结果 */   private List root;     /** 开始页码 */   private int start;     /** 每页多少 */   private int limit;     /** 成功与否 */   private boolean success;     /** 查询条件 */   private Object objCondition;

服务层调用

Java代码
  1. public class RydmServiceImpl extends SqlMapClientDaoSupport implements
  2. RydmService {
  3. public Page findByPageRydm(Page page) throws BusinessException {
  4. page.setTotalProperty((Integer) getSqlMapClientTemplate()
  5. .queryForObject("RYDM.findByCount", page));
  6. page.setPageProperty(page);
  7. page.setRoot(getSqlMapClientTemplate().queryForList(
  8. "RYDM.findByPage", page));
  9. return page;
  10. }
public class RydmServiceImpl extends SqlMapClientDaoSupport implements    RydmService {     public Page findByPageRydm(Page page) throws BusinessException {    page.setTotalProperty((Integer) getSqlMapClientTemplate()      .queryForObject("RYDM.findByCount", page));    page.setPageProperty(page);    page.setRoot(getSqlMapClientTemplate().queryForList(      "RYDM.findByPage", page));    return page;   }

action设置分页属性

Java代码
  1. /*
  2. * 查询用户信息
  3. */
  4. public String listUser() {
  5. Rydm user=new Rydm();
  6. user.setBmzdm(request.getParameter("bmzdm").toString());
  7. user.setRymc(request.getParameter("rymc").toString());
  8. int start =0;
  9. int Limit=10;
  10. Page page = new Page();
  11. try {
  12. start = Integer.valueOf(getRequest().getParameter("start"));
  13. Limit = Integer.valueOf(getRequest().getParameter("limit"));
  14. }catch (NumberFormatException e) {
  15. }
  16. page.setStart(start);
  17. page.setLimit(Limit);
  18. //使用对象作为查询参数 传入ibtais
  19. page.setObjCondition(user);
  20. page = userService.findByPage(page);
  21. } catch (BusinessException e) {
  22. log.warn("UsersAction.class查询用户信息出现异常"+e.getMessage(), e);
  23. }
  24. return SUCCESS;
  25. }
/*    * 查询用户信息    */   public String listUser() {                Rydm user=new   Rydm();                                 user.setBmzdm(request.getParameter("bmzdm").toString());    user.setRymc(request.getParameter("rymc").toString());    int start =0;    int Limit=10;    Page page = new Page();                    try {    start = Integer.valueOf(getRequest().getParameter("start"));    Limit = Integer.valueOf(getRequest().getParameter("limit"));    }catch (NumberFormatException e) {    }    page.setStart(start);    page.setLimit(Limit);    //使用对象作为查询参数 传入ibtais    page.setObjCondition(user);    page = userService.findByPage(page);    } catch (BusinessException e) {     log.warn("UsersAction.class查询用户信息出现异常"+e.getMessage(), e);    }    return SUCCESS;   }

extjs 参数查询

Js代码
  1. var rydm_store = new Ext.data.Store({
  2. proxy : new Ext.data.HttpProxy({
  3. url : "/extDemo/admin/userList.action",
  4. method:"post"
  5. }),
  6. reader : new Ext.data.JsonReader({
  7. totalProperty : 'totalProperty',
  8. root : 'root',
  9. fields : [ {
  10. name : 'bmzdm',
  11. type : 'string'
  12. } , {
  13. name : 'bmmc',
  14. type : 'string'
  15. }]
  16. })
  17. });
  18. var btn_search_rydm = new Ext.Button({
  19. text : '查询',
  20. iconCls : 'icon-search',
  21. handler : queryRydm
  22. });
  23. var queryRydm = function() {
  24. rydm_store.baseParams.bmzdm= bmzdm_search_rydn.getValue();
  25. rydm_store.baseParams.rymc =text_search_rydm.getValue();
  26. rydm_store.load({params : {start : 0,limit : 15}});
  27. }
var rydm_store = new Ext.data.Store({   proxy : new Ext.data.HttpProxy({    url : "/extDemo/admin/userList.action",    method:"post"   }),   reader : new Ext.data.JsonReader({    totalProperty : 'totalProperty',    root : 'root',    fields : [  {     name : 'bmzdm',     type : 'string'    } , {     name : 'bmmc',     type : 'string'    }]   })  });    var btn_search_rydm = new Ext.Button({   text : '查询',   iconCls : 'icon-search',   handler : queryRydm  });      var queryRydm = function() {   rydm_store.baseParams.bmzdm= bmzdm_search_rydn.getValue();   rydm_store.baseParams.rymc =text_search_rydm.getValue();   rydm_store.load({params : {start : 0,limit : 15}});     }
  评论这张
 
阅读(1770)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017