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. QTP自动化测试框架的基础知识

    1. 什么是自动化测试框架? 假定你有一个活,需要构建许多自动化测试用例来测试这个应用程序.当你对这个应用程序完成自动化测试后,你对自己创建脚本应该有什么期望吗?你难道不想要- 脚本应该按照预期的来执 ...

  2. 统计mysql库中每张表的行数据

    修改数据库配置文件:vim /etc/my.cnf [client] user=username password=password 使用shell脚本统计表中的行数据:count.sh #!/bin ...

  3. Jekins持续集成,gitlab代码仓库

    http://blog.csdn.net/john_cdy/article/details/7738393

  4. POJ 2079 Triangle (凸包+旋转卡壳)

    [题目链接] http://poj.org/problem?id=2079 [题目大意] 给出一些点,求出能组成的最大面积的三角形 [题解] 最大三角形一定位于凸包上,因此我们先求凸包,再在凸包上计算 ...

  5. 【点分治】【乘法逆元】hdu4812 D Tree

    思路比较裸,但是要把答案存到哈希表里面,这里需要一定技巧,否则会被K=1且点权全是1的数据卡飞.预处理乘法逆元.TLE了一天.换了种点分治的姿势…… #pragma comment(linker,&q ...

  6. Linux下提示命令找不到:bash:command not found

    Linux下输入某些命令时会提示:bash:command not found. 首先,查看$PATH中是否包含了这些命令. $PATH:决定了shell到哪些目录中去寻找命令或程序,PATH值是一系 ...

  7. Java如何实现系统监控、系统信息收集(转

    Java如何实现系统监控.系统信息收集.sigar开源API的学习 系统监控(1) Jar资源下载:http://download.csdn.net/detail/yixiaoping/4903853 ...

  8. React Native使用Navigator组件进行页面导航报this.props....is not a function错误

    在push的时候定义回调函数: this.props.navigator.push({ component: nextVC, title: titleName, passProps: { //回调 g ...

  9. Error (10663): Verilog HDL Port Connection error at led_demo.v(6): output or inout port "led" must be connected to a structural net expression

    错误现象:

  10. Firefox中好用的几个快捷键

    对于一些经常用FF(firefox)上网的朋友来说, 怎样加快上网的操作速度呢, 使用Firefox快捷键是很好的方法. 本人也经常遇到一些Firefox的很好的快捷键,现在我来告诉大家Firefox ...