Mybatis中sql in的使用

1. 當查詢的參數只有一個時 
  findByIds(List<Long> ids)

 1.1 如果參數的類型是List, 則在使用時,collection屬性要必須指定爲 list

<select id="findByIdsMap" resultMap="BaseResultMap">  
 Select  
 <include refid="Base_Column_List" />  
 from jria where ID in  
 <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
  #{item}  
 </foreach>  
</select> 

1.2 如果參數的類型是Array,則在使用時,collection屬性要必須指定爲 array

<select id="findByIdsMap" resultMap="BaseResultMap">  
select  
<include refid="Base_Column_List" />  
from tabs where ID in  
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">  
 #{item}  
</foreach>  
</select> 

2. 當查詢的參數有多個時,例如 findByIds(String name, Long[] ids)
 這種情況需要特別注意,在傳參數時,一定要改用Map方式, 這樣在collection屬性可以指定名稱
         下面是一個示例
         Map<String, Object> params = new HashMap<String, Object>(2);
         params.put("name", name);
         params.put("ids", ids);
        mapper.findByIdsMap(params);

<select id="findByIdsMap" resultMap="BaseResultMap">  
 select  
 <include refid="Base_Column_List" />  
 from tabs where ID in  
 <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">  
  #{item}  
 </foreach>  
</select>  


鏈接:http://blog.csdn.net/kpchen_0508/article/details/48470107

實例:

<select id="findAlarmAll" resultType="MCellDTO" parameterType="QueryTerms">
		<![CDATA[
			select to_char(mc.ttime, 'yyyy-mm-dd hh24:mi:ss') ttime,
		       mc.ne_name neName,
		       mc.monitor_type monitorType,
		       mc.id id,
		       mc.lac lac,
		       mc.ci ci,
		       mc.state_type stateType,
		       sm.monitor_name monitorName,
		       sm.monitor_detail monitorDetail,
		       sm.professional professional,
		       sm.factory factory,
		       sm.equipment equipment,
		       sm.monitor_level monitorLevel
		  from m_cell mc
		  inner join s_monitor sm
		    on mc.monitor_type = sm.monitor_type
		 ]]>
		<![CDATA[
		    where mc.state_type=#{stateType} 
			and mc.ttime > to_date(#{startDateTime},'yyyy-mm-dd hh24:mi:ss')
	        and mc.ttime <= to_date(#{endDateTime},'yyyy-mm-dd hh24:mi:ss')
		]]>
		
		 <if test="neName!=null"> and upper(mc.ne_name) like '%'||#{neName}||'%' escape '\'</if>
		 
		 <if test="lacArr!=null"> 
		 	and mc.lac in 
		 	<foreach item="item" index="index" collection="lacArr" open="(" separator="," close=")">  
			  #{item}  
			</foreach>  
		 </if>
		 <if test="ciArr!=null"> 
		 	and mc.ci in 
		 	<foreach item="item" index="index" collection="ciArr" open="(" separator="," close=")">  
			  #{item}  
			</foreach>  
		 </if>
		
		<![CDATA[
			order by mc.ttime ,sm.monitor_level desc
		]]>
	</select>

package com.ibs.miq.utils;

public class QueryTerms {

	private String area;// 區域
	private String neName;// 網元
	private String startDateTime;// 開始時間
	private String endDateTime;// 結束時間
	private Long stateType;// 告警類型

	private Integer beforeHour;// 前推時間(小時)
	private String monitorName;// 告警標題

	private String[] lacArr;// lac集合
	private String[] ciArr;// ci集合

	public String[] getLacArr() {
		return lacArr;
	}

	public void setLacArr(String[] lacArr) {
		this.lacArr = lacArr;
	}

	public String[] getCiArr() {
		return ciArr;
	}

	public void setCiArr(String[] ciArr) {
		this.ciArr = ciArr;
	}

	public Integer getBeforeHour() {
		return beforeHour;
	}

	public void setBeforeHour(Integer beforeHour) {
		this.beforeHour = beforeHour;
	}

	public String getMonitorName() {
		return monitorName;
	}

	public void setMonitorName(String monitorName) {
		this.monitorName = monitorName;
	}

	public Long getStateType() {
		return stateType;
	}

	public void setStateType(Long stateType) {
		this.stateType = stateType;
	}

	public String getStartDateTime() {
		return startDateTime;
	}

	public void setStartDateTime(String startDateTime) {
		this.startDateTime = startDateTime;
	}

	public String getEndDateTime() {
		return endDateTime;
	}

	public void setEndDateTime(String endDateTime) {
		this.endDateTime = endDateTime;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getNeName() {
		return neName;
	}

	public void setNeName(String neName) {
		this.neName = neName;
	}

	public QueryTerms(String area, String neName) {
		this.area = area;
		this.neName = neName;
	}

	public QueryTerms() {
	}

}




發佈了132 篇原創文章 · 獲贊 55 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章