http://www.iteye.com/topic/393042最近做了很多动态的查询,尤其是排序,以及一些状态字段,所以就做了一个总的动态查询,以不变应万变,呵呵

ibatis 里面的sql代码:

Xml代码
        <select id="getTopics" resultClass="topic" parameterClass="map">
                        <![CDATA[
                                select * from p_Topic
                        ]]>
                <dynamic prepend=" WHERE ">
                        <isPropertyAvailable property="authorId">
                                <isNotNull property="authorId" prepend=" and ">
                                        authorId=#authorId#
                </isNotNull>
                        </isPropertyAvailable>
                        <isPropertyAvailable property="marketId">
                                <isNotNull property="marketId" prepend=" and ">
                                        marketId=#marketId#
                </isNotNull>
                        </isPropertyAvailable>

<isPropertyAvailable property="isDelete">
                                <isNotNull property="isDelete" prepend=" and ">
                                        isDelete=#isDelete#
                </isNotNull>
                        </isPropertyAvailable>

<isPropertyAvailable property="isBest">
                                <isNotNull property="isBest" prepend=" and ">
                                        isBest=#isBest#
                                </isNotNull>
                        </isPropertyAvailable>

<isPropertyAvailable property="statusStr">
                                <isNotNull property="statusStr" prepend=" and ">
                                        $statusStr$
                                </isNotNull>
                        </isPropertyAvailable>
                        <isPropertyAvailable property="marketIdList">
                                <isNotNull property="marketIdList" prepend=" and marketId in ">
                                        <iterate property="marketIdList" conjunction="," close=")" open="(">
                                                #marketIdList[]#
                                        </iterate>
                                </isNotNull>
                        </isPropertyAvailable>
                </dynamic>

<dynamic prepend=" order by ">
                        <isPropertyAvailable property="orderStr">
                                <isNotNull property="orderStr">
                                        $orderStr$
                </isNotNull>
                        </isPropertyAvailable>
                </dynamic>

<dynamic>
                        <isPropertyAvailable property="begin">
                                <isNotNull property="begin">
                                        limit #begin#
                </isNotNull>
                        </isPropertyAvailable>
                        <isPropertyAvailable property="max" prepend=" , ">
                                <isNotNull property="max">
                                        #max#
                </isNotNull>
                        </isPropertyAvailable>
                </dynamic>
        </select>

<select id="getTopicCount" resultClass="java.lang.Long"
                parameterClass="map">
                        <![CDATA[
                                select count(id) from p_Topic
                        ]]>
                <dynamic prepend=" WHERE ">
                        <isPropertyAvailable property="authorId">
                                <isNotNull property="authorId" prepend=" and ">
                                        authorId=#authorId#
                </isNotNull>
                        </isPropertyAvailable>
                        <isPropertyAvailable property="marketId">
                                <isNotNull property="marketId" prepend=" and ">
                                        marketId=#marketId#
                </isNotNull>
                        </isPropertyAvailable>

<isPropertyAvailable property="isDelete">
                                <isNotNull property="isDelete" prepend=" and ">
                                        isDelete=#isDelete#
                </isNotNull>
                        </isPropertyAvailable>

<isPropertyAvailable property="isBest">
                                <isNotNull property="isBest" prepend=" and ">
                                        isBest=#isBest#
                                </isNotNull>
                        </isPropertyAvailable>

<isPropertyAvailable property="statusStr">
                                <isNotNull property="statusStr" prepend=" and ">
                                        $statusStr$
                                </isNotNull>
                        </isPropertyAvailable>
                        <isPropertyAvailable property="marketIdList">
                                <isNotNull property="marketIdList" prepend=" and ">
                                        <iterate property="marketIdList" conjunction="," close=")" open=" marketId in (">

 
                                                #marketIdList[]#
                                        </iterate>
                                </isNotNull>
                        </isPropertyAvailable>
                </dynamic>
        </select>

这里需要注意的是:

①#xxx#  代表xxx是属性值,map里面的key或者是你的pojo对象里面的属性,ibatis会自动在它的外面加上引号,表现在sql语句是这样的 where xxx = 'xxx' ;

而$xxxx$ 则是把xxxx作为字符串拼接到你的sql语句中,比如 order by  topicId , 如果你不用$来拼接而用#的话,外面就会被加上引号的哦    比如你的语句这样写  ... order by #xxx# (xxx就是你传进来的字符串topicId),ibatis 就会把他翻译成  order by 'topicId' 这样就报错了 ,用$的结果就是这样  order by topicId

②这里的iterate

Java代码
 
         <isPropertyAvailable property="marketIdList">  
            <isNotNull property="marketIdList" prepend="and">  
                <iterate property="marketIdList" conjunction="," open=" marketId in (" close=")">  
                    #marketIdList[]#  
                </iterate>  
            </isNotNull>  
        </isPropertyAvailable>
 
 
注意 iterate 的property属性 ,虽然你上面的isNotNull什么的都有这句,但这里一定要写清楚,否则ibatis会找不到你的list的
 
数据访问层代码:
 
Java代码
               public List<Topic> getTopics(Map<String, Object> map) {

return getSqlMapClientTemplate().queryForList("getTopics", map);
        }

 
 
 服务层代码:
 
Java代码
        public List<Topic> getTopicsByMarketIdList(Long authorId,List<Long> marketIdList,
                        Integer orderby, Integer status, Pagination pagination) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("authorId", authorId);
                map.put("isDelete", false);
                map.put("marketIdList", marketIdList);
                map.put("orderStr", "这里你组装你的order字符串");
                map.put("statusStr","这里你组装你的status字符串");
                map.put("begin", pagination.getOffset());
                map.put("max", pagination.getPageSize());
               //这个getTopicCount()方法和getTopics()大体是一致的,所以我的dao里面省略了它
                Long total = topicDao.getTopicCount(map);
                if (total == 0) {
                        return new ArrayList<Topic>();
                } else {
                        pagination.setTotal(total);
                        List<Topic> res = topicDao.getTopics(map);
                        return res;
                }
        }
 
Java代码
 
public class Topic extends BaseObject implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = -851973667810710701L;

private Long id;
        private Long authorId;
        private String authorName;
        private Long marketId;
        private String title;
        private String tags;
        private String content;
        private Date pubdate;
        private Integer isBest;
        private Integer status;
        private Integer isDelete;
        private Integer clickCount;
        private Integer replyCount;
        private Date lastReplyTime;
       //getter and setter 省略...
}

 
Pagination代码:
 
Java代码
public class Pagination {

/**
         * 要查看的页码
         */
        private int page;

/**
         * 每页显示数
         */
        private int pageSize;

/**
         * 一共有多少页
         */
        private int totalPage;

/**
         * 一共有多少条记录
         */
        private long total;

/**
         * 当前页的记录数
         */
        private int size;

/**
         * 只需要topxx,不需要页数信息了
         */
        private boolean topOnly;

/**
       *从第几条记录开始        
       */
        private int offset;
        
        public void setOffset(int offset) {
                this.offset = offset;
        }

public Pagination(int page, int pageSize) {
                this.page = page;
                this.pageSize = pageSize;
        }

public Pagination() {
        }

public boolean require() {
                return pageSize > 0 ? true : false;
        }

public int from() {
                return page * pageSize;
        }

public int to() {
                return from() + size;
        }

public int getPage() {
                return page;
        }

public void setPage(int page) {
                this.page = page;
        }

public int getPageSize() {
                return pageSize;
        }

public void setPageSize(int pageSize) {
                this.pageSize = pageSize;
        }

public int getTotalPage() {
                return totalPage;
        }

public void setTotalPage(int totalPage) {
                this.totalPage = totalPage;
        }

public long getTotal() {
                return total;
        }

public void setTotal(long total) {
                this.total = total;
                if (pageSize > 0) {
                        this.totalPage = (int) Math.ceil(total / (double) pageSize);
                } else {
                        this.totalPage = 1;
                }
                if (page >= totalPage) {
                        page = totalPage - 1;
                }
                if (page < 0)
                        page = 0;
                if (pageSize > 0) {
                        if (page < totalPage - 1)
                                this.size = pageSize;
                        else
                                this.size = (int) (total % pageSize);
                } else {
                        this.size = (int) total;
                }
                offset=page * pageSize;
        }

public int getOffset() {
                return offset;
        }

public int getSize() {
                return size;
        }

public void setSize(int size) {
                this.size = size;
        }

public boolean isTopOnly() {
                return topOnly;
        }

public void setTopOnly(boolean topOnly) {
                this.topOnly = topOnly;
        }

}

ibatis 动态查询的更多相关文章

  1. ibatis动态查询条件

    ibatis的调试相对困难,出错的时候主要依据是log4生成的log文件和出错提示,这方面要能比较熟练的看懂. 下面这个配置基本上包含了最复杂的功能:分页\搜索\排序\缓存\传值Hash表\返回has ...

  2. ibatis动态查询

    在复杂查询过程中,我们常常需要根据用户的选择决定查询条件,这里发生变化的并不只是SQL 中的参数,包括Select 语句中所包括的字段和限定条件,都可能发生变化.典型情况,如在一个复杂的组合查询页面, ...

  3. ibatis动态多条件查询及模糊查询(oracle,mysql,sql)

    首先是模糊查询的问题,开始时我使用如下条件:select * from user where name like '%#value#%'. 可是怎么也不行,好像还报错了.后来在网上找到了解决方法,就是 ...

  4. Ibatis动态(dynamic)查询

     Ibatis的动态查询使得数据操作变得非常的灵活,下次举出了常用的动态查询的属性信息: Ibatis配置信息 <!-- Dynamic Sql --> <typeAlias a ...

  5. ibatis Dynamic总结(ibatis使用安全的拼接语句,动态查询)

    ibatis中使用安全的拼接语句,动态查询,ibatis比JDBC的优势之一,安全高效 说明文字在注释中 一.引入 一个小例子  <select id="selectAllProduc ...

  6. ibatis 取消查询动态列的缓存

    ibatis在查询结果列不确定(或是动态变化)的情况下,会因为列缓存的原因导致变化后的列数据查不出来 解决方法是: select标签有个属性remapResults,该属性默认值为false,设置成r ...

  7. 转:ibatis动态sql

    转:ibatis动态sql 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的 IF-ELSE条件语句和一连串 ...

  8. Ibatis动态拼装sql,常用标签总结及举栗子。

    今天得到项目经理一项任务,就是拼装sql,第一次见到,不是太懂,赶紧回来睡一觉再说,由于这次的项目orm使用的是ibatis框架,所以需要使用动态拼装sql,或者是ognl语言,这门语言不是专属于ib ...

  9. IBATIS动态SQL(1)

    转:IBATIS动态SQL 直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的 ...

随机推荐

  1. (3)三剑客之sed

    (1)基本介绍 1) 工作流程:sed每次处理一行内容,处理时,把当前处理的行存储在临时缓存区,称为模式空间,接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕,直到内容处理完毕2 ...

  2. PHP开发经常遇到的几个错误

    错误1:foreach循环后留下悬挂指针 在foreach循环中,如果我们需要更改迭代的元素或是为了提高效率,运用引用是一个好办法: $arr = array(1,2,3,4); foreach($a ...

  3. UVA 10341.Solve It-二分查找

    二分查找 二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表.首先,假设表中元素是按升序 ...

  4. ACM-ICPC北京赛区(2017)网络赛1【模拟+枚举+数组操作】

    题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n ...

  5. HDU 2829 Lawrence

    $dp$,斜率优化. 设$dp[i][j]$表示前$i$个数字切了$j$次的最小代价.$dp[i][j]=dp[k][j-1]+p[k+1][i]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...

  6. Eclipse 教程 | 菜鸟教程

    http://www.runoob.com/eclipse/eclipse-charset.html

  7. POJ 3537 Crosses and Crosses (NEERC)

                      Crosses and Crosses Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4 ...

  8. SciPy中两个模块:io 和misc

    读写.mat文件 如果你有一些数据,或者在网上下载到一些有趣的数据集,这些数据以Matlab的.mat 文件格式存储,那么可以使用scipy.io 模块进行读取. data = scipy.io.lo ...

  9. Problem K: 数字菱形

    #include<stdio.h> int main() { int n,i,j,k,t,x,q,p; while(scanf("%d",&n)!=EOF) ; ...

  10. noip2017集训测试赛(三) Problem B: mex [补档]

    Description 给你一个无限长的数组,初始的时候都为0,有3种操作: 操作1是把给定区间[l,r][l,r] 设为1, 操作2是把给定区间[l,r][l,r] 设为0, 操作3把给定区间[l, ...