Ibatis动态查询例子(#和$以及iterate等的用法)

最近做了很多动态的查询,尤其是排序,以及一些状态字段,所以就做了一个总的动态查询,以不变应万变,呵呵

 

ibatis 里面的sql代码:

Xml代码 复制代码
  1. <select id="getTopics" resultClass="topic" parameterClass="map">  
  2.         <![CDATA[  
  3.                 select * from p_Topic   
  4.         ]]>  
  5.     <dynamic prepend=" WHERE ">  
  6.         <isPropertyAvailable property="authorId">  
  7.             <isNotNull property="authorId" prepend=" and ">  
  8.                 authorId=#authorId#    
  9.                </isNotNull>  
  10.         </isPropertyAvailable>  
  11.         <isPropertyAvailable property="marketId">  
  12.             <isNotNull property="marketId" prepend=" and ">  
  13.                 marketId=#marketId#    
  14.                </isNotNull>  
  15.         </isPropertyAvailable>  
  16.   
  17.         <isPropertyAvailable property="isDelete">  
  18.             <isNotNull property="isDelete" prepend=" and ">  
  19.                 isDelete=#isDelete#    
  20.                </isNotNull>  
  21.         </isPropertyAvailable>  
  22.   
  23.         <isPropertyAvailable property="isBest">  
  24.             <isNotNull property="isBest" prepend=" and ">  
  25.                 isBest=#isBest#   
  26.             </isNotNull>  
  27.         </isPropertyAvailable>  
  28.   
  29.         <isPropertyAvailable property="statusStr">  
  30.             <isNotNull property="statusStr" prepend=" and ">  
  31.                 $statusStr$   
  32.             </isNotNull>  
  33.         </isPropertyAvailable>  
  34.         <isPropertyAvailable property="marketIdList">  
  35.             <isNotNull property="marketIdList" prepend=" and marketId in ">  
  36.                 <iterate property="marketIdList" conjunction="," close=")" open="(">  
  37.                     #marketIdList[]#   
  38.                 </iterate>  
  39.             </isNotNull>  
  40.         </isPropertyAvailable>  
  41.     </dynamic>  
  42.   
  43.     <dynamic prepend=" order by ">  
  44.         <isPropertyAvailable property="orderStr">  
  45.             <isNotNull property="orderStr">  
  46.                 $orderStr$   
  47.                </isNotNull>  
  48.         </isPropertyAvailable>  
  49.     </dynamic>  
  50.   
  51.     <dynamic>  
  52.         <isPropertyAvailable property="begin">  
  53.             <isNotNull property="begin">  
  54.                 limit #begin#    
  55.                </isNotNull>  
  56.         </isPropertyAvailable>  
  57.         <isPropertyAvailable property="max" prepend=" , ">  
  58.             <isNotNull property="max">  
  59.                 #max#   
  60.                </isNotNull>  
  61.         </isPropertyAvailable>  
  62.     </dynamic>  
  63. </select>  
  64.   
  65.   
  66.   
  67. <select id="getTopicCount" resultClass="java.lang.Long"  
  68.     parameterClass="map">  
  69.         <![CDATA[  
  70.                 select count(id) from p_Topic   
  71.         ]]>  
  72.     <dynamic prepend=" WHERE ">  
  73.         <isPropertyAvailable property="authorId">  
  74.             <isNotNull property="authorId" prepend=" and ">  
  75.                 authorId=#authorId#    
  76.                </isNotNull>  
  77.         </isPropertyAvailable>  
  78.         <isPropertyAvailable property="marketId">  
  79.             <isNotNull property="marketId" prepend=" and ">  
  80.                 marketId=#marketId#    
  81.                </isNotNull>  
  82.         </isPropertyAvailable>  
  83.   
  84.         <isPropertyAvailable property="isDelete">  
  85.             <isNotNull property="isDelete" prepend=" and ">  
  86.                 isDelete=#isDelete#    
  87.                </isNotNull>  
  88.         </isPropertyAvailable>  
  89.   
  90.         <isPropertyAvailable property="isBest">  
  91.             <isNotNull property="isBest" prepend=" and ">  
  92.                 isBest=#isBest#   
  93.             </isNotNull>  
  94.         </isPropertyAvailable>  
  95.   
  96.         <isPropertyAvailable property="statusStr">  
  97.             <isNotNull property="statusStr" prepend=" and ">  
  98.                 $statusStr$   
  99.             </isNotNull>  
  100.         </isPropertyAvailable>  
  101.         <isPropertyAvailable property="marketIdList">  
  102.             <isNotNull property="marketIdList" prepend=" and marketId in ">  
  103.                 <iterate property="marketIdList" conjunction="," close=")" open="(">  
  104.                     #marketIdList[]#   
  105.                 </iterate>  
  106.             </isNotNull>  
  107.         </isPropertyAvailable>  
  108.     </dynamic>  
  109. </select>  
	<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会自动在它的外面加上引号,表现在sql语句是这样的 where xxx = 'xxx' ;

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

②这里的iterate

Java代码 复制代码
  1. <isPropertyAvailable property="marketIdList">   
  2.     <isNotNull property="marketIdList" prepend=" and marketId in ">   
  3.         <iterate property="marketIdList" conjunction="," close=")" open="(">   
  4.             #marketIdList[]#   
  5.         </iterate>   
  6.     </isNotNull>   
  7. </isPropertyAvailable>  
<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的

 

 

数据访问层代码:

Java代码 复制代码
  1. public List<Topic> getTopics(Map<String, Object> map) {   
  2.   
  3.         return getSqlMapClientTemplate().queryForList("getTopics", map);   
  4.     }  
public List<Topic> getTopics(Map<String, Object> map) {

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

 服务层代码:

Java代码 复制代码
  1. public List<Topic> getTopicsByMarketIdList(Long authorId,List<Long> marketIdList,   
  2.         Integer orderby, Integer status, Pagination pagination) {   
  3.     Map<String, Object> map = new HashMap<String, Object>();   
  4.     map.put("authorId", authorId);   
  5.     map.put("isDelete"false);   
  6.     map.put("marketIdList", marketIdList);   
  7.     map.put("orderStr""这里你组装你的order字符串");   
  8.     map.put("statusStr","这里你组装你的status字符串");   
  9.     map.put("begin", pagination.getOffset());   
  10.     map.put("max", pagination.getPageSize());   
  11.               //这个getTopicCount()方法和getTopics()大体是一致的,所以我的dao里面省略了它   
  12.     Long total = topicDao.getTopicCount(map);   
  13.     if (total == 0) {   
  14.         return new ArrayList<Topic>();   
  15.     } else {   
  16.         pagination.setTotal(total);   
  17.         List<Topic> res = topicDao.getTopics(map);   
  18.         return res;   
  19.     }   
  20. }  
	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代码 复制代码
  1. public class Topic extends BaseObject implements Serializable {   
  2.     /**  
  3.      *   
  4.      */  
  5.     private static final long serialVersionUID = -851973667810710701L;   
  6.   
  7.     private Long id;   
  8.     private Long authorId;   
  9.     private String authorName;   
  10.     private Long marketId;   
  11.     private String title;   
  12.     private String tags;   
  13.     private String content;   
  14.     private Date pubdate;   
  15.     private Integer isBest;   
  16.     private Integer status;   
  17.     private Integer isDelete;   
  18.     private Integer clickCount;   
  19.     private Integer replyCount;   
  20.     private Date lastReplyTime;   
  21.        //getter and setter 省略...   
  22. }  
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代码 复制代码
  1. public class Pagination {   
  2.   
  3.     /**  
  4.      * 要查看的页码  
  5.      */  
  6.     private int page;   
  7.   
  8.     /**  
  9.      * 每页显示数  
  10.      */  
  11.     private int pageSize;   
  12.   
  13.     /**  
  14.      * 一共有多少页  
  15.      */  
  16.     private int totalPage;   
  17.   
  18.     /**  
  19.      * 一共有多少条记录  
  20.      */  
  21.     private long total;   
  22.   
  23.     /**  
  24.      * 当前页的记录数  
  25.      */  
  26.     private int size;   
  27.   
  28.     /**  
  29.      * 只需要topxx,不需要页数信息了  
  30.      */  
  31.     private boolean topOnly;   
  32.   
  33.       /**  
  34.        *从第几条记录开始      
  35.        */  
  36.     private int offset;   
  37.        
  38.     public void setOffset(int offset) {   
  39.         this.offset = offset;   
  40.     }   
  41.   
  42.     public Pagination(int page, int pageSize) {   
  43.         this.page = page;   
  44.         this.pageSize = pageSize;   
  45.     }   
  46.   
  47.     public Pagination() {   
  48.     }   
  49.   
  50.     public boolean require() {   
  51.         return pageSize > 0 ? true : false;   
  52.     }   
  53.   
  54.     public int from() {   
  55.         return page * pageSize;   
  56.     }   
  57.   
  58.     public int to() {   
  59.         return from() + size;   
  60.     }   
  61.   
  62.     public int getPage() {   
  63.         return page;   
  64.     }   
  65.   
  66.     public void setPage(int page) {   
  67.         this.page = page;   
  68.     }   
  69.   
  70.     public int getPageSize() {   
  71.         return pageSize;   
  72.     }   
  73.   
  74.     public void setPageSize(int pageSize) {   
  75.         this.pageSize = pageSize;   
  76.     }   
  77.   
  78.     public int getTotalPage() {   
  79.         return totalPage;   
  80.     }   
  81.   
  82.     public void setTotalPage(int totalPage) {   
  83.         this.totalPage = totalPage;   
  84.     }   
  85.   
  86.     public long getTotal() {   
  87.         return total;   
  88.     }   
  89.   
  90.     public void setTotal(long total) {   
  91.         this.total = total;   
  92.         if (pageSize > 0) {   
  93.             this.totalPage = (int) Math.ceil(total / (double) pageSize);   
  94.         } else {   
  95.             this.totalPage = 1;   
  96.         }   
  97.         if (page >= totalPage) {   
  98.             page = totalPage - 1;   
  99.         }   
  100.         if (page < 0)   
  101.             page = 0;   
  102.         if (pageSize > 0) {   
  103.             if (page < totalPage - 1)   
  104.                 this.size = pageSize;   
  105.             else  
  106.                 this.size = (int) (total % pageSize);   
  107.         } else
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值