国产精品电影_久久视频免费_欧美日韩国产激情_成年人视频免费在线播放_日本久久亚洲电影_久久都是精品_66av99_九色精品美女在线_蜜臀a∨国产成人精品_冲田杏梨av在线_欧美精品在线一区二区三区_麻豆mv在线看

實例說明ibatis動態(tài)查詢

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

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

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

ibatis動態(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動態(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對象里面的屬性,ibatis會自動在它的外面加上引號,表現(xiàn)在sql語句是這樣的 where xxx = 'xxx' ;而$xxxx$ 則是把xxxx作為字符串拼接到你的sql語句中,比如 order by  topicId , 如果你不用$來拼接而用#的話,外面就會被加上引號的哦    比如你的語句這樣寫  ... order by #xxx# (xxx就是你傳進來的字符串topicId),ibatis 就會把他翻譯成  order by 'topicId' 這樣就報錯了,用$的結(jié)果就是這樣  order by topicId。

另外在ibatis動態(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會找不到你的list的,ibatis動態(tài)查詢自然無法實現(xiàn)。

ibatis動態(tài)查詢中的數(shù)據(jù)訪問層代碼:

Java代碼

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

ibatis動態(tài)查詢中的服務層代碼:

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());   
              //這個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());  
              //這個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動態(tài)查詢中的Pagination代碼:

Java代碼:

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

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

【編輯推薦】

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

2009-07-22 13:04:41

iBATIS動態(tài)查詢

2009-07-22 11:27:36

iBATIS模糊查詢

2009-07-24 17:20:59

iBatis配置

2009-07-15 17:58:07

iBATIS 動態(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動態(tài)條件

2009-07-21 10:39:19

動態(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分頁實例ObjectDataS

2009-07-20 14:56:18

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

2009-07-16 17:40:48

iBATIS高級查詢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實例

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
點贊
收藏

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

国产日韩第一页| 日韩专区第三页| 欧美日韩无遮挡| 国产在线视频在线| 99视频在线视频| 国产美女性感在线观看懂色av | 日本乱码高清不卡字幕| 欧美日韩国产在线看| 亚洲午夜久久久久久久| 国产盗摄xxxx视频xxx69| 久久一区免费| 国产字幕中文| 国内在线免费视频| 欧美国产亚洲精品| 国产精品一区毛片| 99精品国产一区二区三区不卡| 亚洲永久免费av| 亚洲精品成人久久| 欧美国产日韩视频| 深夜福利成人| 超碰在线94| 午夜不卡影院| 亚洲精品护士| 亚洲一二三级电影| 久久久精品在线| 欧美12av| 国产三级日本三级在线播放| 欧美理论电影| 日韩av网站在线免费观看| 精品一区二区三区蜜桃| 国产精品久久久久天堂| 亚洲视频视频在线| 国内一区二区三区在线视频| 精品少妇一区二区三区在线| 免费污视频在线| 97精品97| 亚洲欧美日韩中文字幕一区二区三区 | 日本a级片久久久| 一起操在线观看| 在线免费成人| 92精品国产成人观看免费| 日韩欧美亚洲一区二区| 成人中文字幕+乱码+中文字幕| 欧美一级黄色片视频| 看亚洲a级一级毛片| hitomi一区二区三区精品| 日韩精品专区在线影院观看 | 天堂av免费看| 午夜毛片在线| 激情欧美丁香| 色偷偷一区二区三区| 97视频免费观看| 黄色av免费| 日韩久久精品| 亚洲精品第一国产综合野| 性欧美xxxx交| 亚洲福利精品视频| 欧美交a欧美精品喷水| 亚洲精品日韩专区silk| 国产精品高潮呻吟久久av野狼 | 亚洲综合在线中文字幕| 国产视频在线看| 美女视频黄 久久| 一区二区三区美女xx视频| 欧美国产亚洲一区| 日韩电影在线观看完整免费观看| 一区二区三区在线视频播放 | 在线播放av网站| 亚洲欧美综合| 亚洲激情免费观看| 九九爱精品视频| 亚洲精品a区| 五月天欧美精品| 九九九九久久久久| 国产蜜臀一区二区打屁股调教| 国产在线精品国自产拍免费| 久久亚洲电影天堂| 激情丁香在线| 免费精品视频| 一区二区在线视频播放| 亚洲一区二区三区四区五区xx| 色综合咪咪久久网| 91精品综合久久久久久| 玖玖精品在线视频| 国内成人精品| 精品不卡在线视频| 久久国产成人精品国产成人亚洲| 成人激情电影在线| 亚洲激情电影中文字幕| 青青青在线播放| 欧美日韩蜜桃| 成年无码av片在线| 黄色高清在线观看| 高清不卡一二三区| 国产精品女人久久久久久| 2024短剧网剧在线观看| 国产欧美日韩另类视频免费观看| 999在线免费观看视频| 欧美日韩不卡| 在线观看成人免费视频| 69堂免费视频| 亚洲女优在线| 午夜精品久久久久久久白皮肤| 日本暖暖在线视频| 亚洲欧美日韩中文字幕一区二区三区| 你懂的网址一区二区三区| 美女视频免费精品| 日韩有码在线视频| a视频在线观看免费| 成人av网站免费观看| 精品九九九九| 涩爱av色老久久精品偷偷鲁| 91精品国产综合久久香蕉麻豆| 婷婷久久伊人| 国产精品99免费看| 综合国产在线观看| 午夜小视频福利在线观看| 在线看日韩精品电影| 青娱在线视频| 国产偷v国产偷v亚洲高清| 国产精品999视频| 国产高清无密码一区二区三区| 老司机精品福利在线观看| 成人羞羞动漫| 欧美成年人网站| 成人影院在线免费观看| 亚洲毛茸茸少妇高潮呻吟| 婷婷色在线播放| 欧美另类久久久品| 成人黄动漫网站| 成人97人人超碰人人99| 日本美女爱爱视频| 男女激情视频一区| 欧美精品v日韩精品v国产精品| 欧美欧美全黄| 亚洲一区二区日本| 亚洲视频免费| 97超级碰碰| 欧美a级片网站| 91精品在线播放| 欧美伦理在线视频| 96国产粉嫩美女| 国产一区亚洲| 欧美日韩在线一二三| 毛片一区二区三区| 日韩精品一线二线三线| 国产一区不卡精品| 国产精品亚洲a| 国产精品乱码妇女bbbb| 成人福利视频在| 五月婷婷激情综合| 伊人75在线| 欧美一区二区三区人| free性欧美| 色999日韩欧美国产| aaa国产精品视频| 国产精品91久久| 国产一区激情| 在线观看成人免费| 国产精品不卡在线观看| 国产在线黄色| 中文字幕亚洲一区| 成人综合久久| 久久99国产精品99久久| 国产精品1区2区| 欧美福利网站| 欧美精品高清视频| 超碰在线视屏| 日本国产一区二区三区| 久久蜜桃精品| 免费观看精品视频| 欧美日韩国产在线播放| 成人一区福利| 在线小视频网址| 久草精品在线| 91福利视频网| 牛牛国产精品| 蜜桃精品久久久久久久免费影院 | 中文字幕一区在线| 波多野结衣中文字幕久久| 欧美亚洲视频在线看网址| 亚洲理论电影网| 免费视频爱爱太爽了| 亚洲天堂2016| 免费在线毛片网站| 久久午夜a级毛片| 精品精品99| 99精品一级欧美片免费播放| 中文字幕精品一区二区精品绿巨人| 亚洲一区二区三区精品中文字幕| 亚洲欧美日韩中文在线| 欧美激情性爽国产精品17p| 国产男女免费视频| 日韩一区二区三区免费观看| 香蕉视频一区二区三区| av在线com| 日韩欧美成人午夜| 希岛爱理av一区二区三区| 亚洲AV无码成人精品一区| 欧美亚洲丝袜传媒另类|