成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

實(shí)例說(shuō)明ibatis動(dòng)態(tài)查詢

開發(fā) 后端
可能很多朋友對(duì)于ibatis中的排序以及一些狀態(tài)字段動(dòng)態(tài)查詢產(chǎn)生過(guò)疑問(wèn)。實(shí)際上,這些都是ibatis動(dòng)態(tài)查詢的問(wèn)題。掌握了ibatis動(dòng)態(tài)查詢,很多問(wèn)題便會(huì)迎刃而解。這篇文章就是一個(gè)總的ibatis動(dòng)態(tài)查詢,幫助您以不變應(yīng)萬(wàn)變。

最近做了很多動(dòng)態(tài)的查詢,尤其是排序,以及一些狀態(tài)字段,所以就做了一個(gè)總的動(dòng)態(tài)查詢,以不變應(yīng)萬(wàn)變,呵呵

ibatis動(dòng)態(tài)查詢里面的sql代碼:

ibatis動(dòng)態(tài)查詢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 marketId in ">  
                <iterate property="marketIdList" conjunction="," close=")" open="(">  
                    #marketIdList[]#   
                </iterate>  
            </isNotNull>  
        </isPropertyAvailable>  
    </dynamic>  
</select>

ibatis動(dòng)態(tài)查詢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 marketId in "> 
                <iterate property="marketIdList" conjunction="," close=")" open="("> 
                    #marketIdList[]#  
                </iterate> 
            </isNotNull> 
        </isPropertyAvailable> 
    </dynamic> 
</select>

這里需要注意的是:

#xxx#  代表xxx是屬性值,map里面的key或者是你的pojo對(duì)象里面的屬性,ibatis會(huì)自動(dòng)在它的外面加上引號(hào),表現(xiàn)在sql語(yǔ)句是這樣的 where xxx = 'xxx' ;而$xxxx$ 則是把xxxx作為字符串拼接到你的sql語(yǔ)句中,比如 order by  topicId , 如果你不用$來(lái)拼接而用#的話,外面就會(huì)被加上引號(hào)的哦    比如你的語(yǔ)句這樣寫  ... order by #xxx# (xxx就是你傳進(jìn)來(lái)的字符串topicId),ibatis 就會(huì)把他翻譯成  order by 'topicId' 這樣就報(bào)錯(cuò)了,用$的結(jié)果就是這樣  order by topicId。

另外在ibatis動(dòng)態(tài)查詢里要注意它的iterate。

Java代碼

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

注意 iterate 的property屬性 ,雖然你上面的isNotNull什么的都有這句,但這里一定要寫清楚,否則ibatis會(huì)找不到你的list的,ibatis動(dòng)態(tài)查詢自然無(wú)法實(shí)現(xiàn)。

ibatis動(dòng)態(tài)查詢中的數(shù)據(jù)訪問(wèn)層代碼:

Java代碼

public List getTopics(Map map) {   
  
        return getSqlMapClientTemplate().queryForList("getTopics", map);   
    }  

ibatis動(dòng)態(tài)查詢中的服務(wù)層代碼:

Java代碼

public List getTopicsByMarketIdList(Long authorId,List marketIdList,   
        Integer orderby, Integer status, Pagination pagination) {   
    Map map = new HashMap();   
    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());   
              //這個(gè)getTopicCount()方法和getTopics()大體是一致的,所以我的dao里面省略了它   
    Long total = topicDao.getTopicCount(map);   
    if (total == 0) {   
        return new ArrayList();   
    } else {   
        pagination.setTotal(total);   
        List res = topicDao.getTopics(map);   
        return res;   
    }   
}

Java代碼

public List getTopicsByMarketIdList(Long authorId,List marketIdList,  
        Integer orderby, Integer status, Pagination pagination) {  
    Map map = new HashMap();  
    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());  
              //這個(gè)getTopicCount()方法和getTopics()大體是一致的,所以我的dao里面省略了它  
    Long total = topicDao.getTopicCount(map);  
    if (total == 0) {  
        return new ArrayList();  
    } else {  
        pagination.setTotal(total);  
        List 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 省略...   
}

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 省略...  

ibatis動(dòng)態(tài)查詢中的Pagination代碼:

Java代碼:

public class Pagination {   
  
    /**  
     * 要查看的頁(yè)碼  
     */  
    private int page;   
  
    /**  
     * 每頁(yè)顯示數(shù)  
     */  
    private int pageSize;   
  
    /**  
     * 一共有多少頁(yè)  
     */  
    private int totalPage;   
  
    /**  
     * 一共有多少條記錄  
     */  
    private long total;   
  
    /**  
     * 當(dāng)前頁(yè)的記錄數(shù)  
     */  
    private int size;   
  
    /**  
     * 只需要topxx,不需要頁(yè)數(shù)信息了  
     */  
    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

雖然代碼量有些大,但是這是一個(gè)總的ibatis動(dòng)態(tài)查詢實(shí)例,您在實(shí)際工作中遇到相應(yīng)的ibatis動(dòng)態(tài)查詢問(wèn)題可以參考一下。

【編輯推薦】

  1. ibatis標(biāo)簽詳解
  2. ibatis插件的安裝方式
  3. ibatis下加入c3p0連接池的方法
  4. ibatis也能用proxool連接池
  5. 避免ibatisN+1查詢的方法
責(zé)任編輯:桑丘 來(lái)源: tntxie的blog
相關(guān)推薦

2009-07-22 13:04:41

iBATIS動(dòng)態(tài)查詢

2009-07-22 11:27:36

iBATIS模糊查詢

2009-07-24 17:20:59

iBatis配置

2009-07-15 17:58:07

iBATIS 動(dòng)態(tài)映射

2009-07-22 09:44:05

iBATIS Para

2009-07-17 17:16:48

Spring iBAT

2009-09-15 09:45:23

Linq動(dòng)態(tài)條件

2009-07-21 10:39:19

動(dòng)態(tài)Mapped St

2009-07-24 16:59:57

iBatis模糊查詢

2009-07-21 17:06:35

iBATIS.NET執(zhí)

2009-07-17 17:45:56

iBATIS Spri

2009-07-22 11:11:39

iBATIS分頁(yè)實(shí)例ObjectDataS

2009-07-20 14:56:18

iBATIS.NET動(dòng)態(tài)選擇DAO

2009-07-16 17:40:48

iBATIS高級(jí)查詢iBATIS使用

2009-07-20 10:06:07

iBATIS.net查詢方式

2009-07-20 16:07:19

Struts2.0 S

2009-07-17 10:20:24

iBATIS實(shí)例

2009-07-20 15:37:09

iBatis like注入漏洞

2009-07-20 16:41:27

Struts2.0+i

2009-07-20 17:01:11

Struts2.0+i
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产精品久久久久久二区 | 狠狠操狠狠搞 | 国产视频二区 | 国产免费一区二区 | 天天综合网天天综合色 | 亚洲欧洲精品成人久久奇米网 | 久草日韩| 伊人在线 | 狠狠色网| 高清一区二区三区 | 黄色毛片免费看 | 日韩欧美亚洲综合 | 成人国产一区二区三区精品麻豆 | 鸡毛片| 国产精品美女久久久久久免费 | 国产在线视频在线观看 | www.久久精品视频 | 成人国产精品久久久 | 久久久久久久一区 | 久久精品视频在线观看 | 日韩欧美亚洲 | 久久99视频 | 国内精品一区二区三区 | 一区二区视频在线 | 亚洲精品久久久久国产 | 精品福利一区二区三区 | 蜜桃臀av一区二区三区 | 久久精品亚洲精品 | 国产欧美精品在线观看 | 婷婷久久网 | 欧美日韩亚洲国产综合 | 羞羞的视频在线观看 | 国产成人福利 | 欧美一级在线观看 | 国产伦精品一区二区三区照片91 | 九九九久久国产免费 | 黄色在线免费观看视频 | 久久久不卡网国产精品一区 | 久久精品国产a三级三级三级 | 一区二区国产在线 | 亚洲精品一区二区三区 |