braint8  
日历
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
统计
  • 随笔 - 0
  • 文章 - 15
  • 评论 - 16
  • 引用 - 0

导航

留言簿(2)

文章分类(15)

文章档案(15)

搜索

  •  

积分与排名

  • 积分 - 17482
  • 排名 - 1882

最新评论

 
三、有条件的方法并执行配置文件.xml中的SQL语句
1.条件为Bean对象
roleList = sqlMapClient.queryForList("searchRole",role);

Role.xml文件中
    <select id="searchRole" parameterClass="Role" resultClass="Role">
        select * from t_role
        <dynamic prepend="where">//dynamic标签,当下属标签成立时在sql语句后加入where,即isNotNull property="rolename">成立时
            <isNotNull property="rolename">
                rolename like '%$rolename$%'
            </isNotNull>
            <isNotNull property="status">//可以在bean里设置多个属性值作为多个条件
                and status=#status#
            </isNotNull>
        </dynamic>
    </select>

    模糊查询的时候需要把##换成$$,一开始使用##,一直提示"赋值变量不存在",网络上查了下才知道要换成$$,原因也不清楚。。。
    
2.条件为Map
与Bean对象一模一样,也可以用<isNotNull property="rolename">

3.只需要有用的字段
表的字段有10个,而这个时候你只需要用到5个,则可以使用resultMap
以最原始的查询为例子:
    <select id="searchRole" resultClass="Role">
        select * from t_role
    </select>
    
    可以修改为:
  <resultMap id="get-role-result" class="Role">
      <result property="roleid" column="roleid"/>//property为role对象的属性,column为对应表的字段名
      <result property="status" column="status"/>
  </resultMap>
 
  <select id="searchRole" resultMap="get-role-result">
        select * from t_role
    </select>
    这样返回的List里的role对象只就只有roleid,status有值,rolename是没有值为null;
    
4.分页
queryForList(String id, int skip, int max)
id表示配置文件中的方法
skip表示结果集记录的开始索引,从0开始
max表示结果集记录总数,max可以超过结果集的实际数无影响
比如要查结果集的前三条记录,skip=0,max=3;

5.queryForPaginatedList方法提供分页的数据子集
PaginatedList 接口包含了向前和向后翻页的方法(nextPage(),previousPage(),gotoPage()) ,
并提供了检查翻页状态的方法(isFirstPage(),isMiddlePage(),isLastPage(),
isPreviousPageAvailable(),getPageIndex(),getPageSize()) 。
虽然不能从 PaginatedList 接口得到查询结果集的总数,但这个总数可以再执行一个简单的语句
count()来得到。否则,PaginatedList接口会大大的降低性能。
posted on 2009-08-29 20:39 情晚风 阅读(264) 评论(0)  编辑  收藏 所属分类: ibatis学习过程
 
Copyright © 情晚风 Powered by: 博客园 模板提供:沪江博客