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. IE添加可信任站点,启用ActiveX插件批处理

    添加可信任站点IP地址为:192.168.1.108,启用ActiveX插件执行以下批处理命令: reg add "HKCU\Software\Microsoft\Windows\Curre ...

  2. Git命令使用指南

    继续git相关的东西,网上很多讲解的,但是还是喜欢这个图:(爱屋及乌,当然内容也很好,文章链接:http://me.iblogc.com/2015/01/16/Git命令使用指南/) Git是软件开发 ...

  3. sqlmap的小试牛刀

    这次算是一次用sqlmap的例子,写的很水. 目的:通过工具扫描到了后台的数据库的地址(如下图),想通过sqlmap找到数据库的用户和密码进入到数据库(首先可以尝试一下root:root一般都是这个情 ...

  4. HDU 多校1.12

  5. Lock wait timeout exceeded数据库死锁问题

    环境 MySQL5.5 现象 A.数据更新或新增后数据经常自动回滚. B.表操作总报 Lock wait timeout exceeded 并长时间无反应 解决方法 A.应急方法:show proce ...

  6. 线段树+Dfs序【CF620E】New Year Tree

    Description 你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]. 有两种操作: 1 v c 将以v为根的子树中所有点颜色更改为c 2 v 查询以v为根的子树中的节点有多少种 ...

  7. centos7下配置samba,win10访问

    yum install -y samba samba-client 更改配置 [root@abcd mnt]# cat /etc/samba/smb.conf [global] workgroup = ...

  8. oracle 替换其中部分内容

    update TABLE_NAME set field =REPLACE(field ,substr(field ,0,1) ,'P') where field is not null ;

  9. [NOI2018]屠龙勇士(exCRT)

    首先很明显剑的选择是唯一的,直接用multiset即可. 接下来可以发现每条龙都是一个模线性方程.设攻击第i条龙的剑的攻击力为$s_i$,则$s_ix\equiv a_i\ (mod\ p_i)$. ...

  10. 【动态规划】【记忆化搜索】CODEVS 1010 过河卒 2002年NOIP全国联赛普及组

    f(i,j)=f(i-1,j)+f(i,j-1),显然可以暴力递归求解,但是很多重复的状态,所以可以记忆下来. 注意障碍点和边界的特判. #include<cstdio> #include ...