mybatis框架中動態sql的應用

MyBatis 的一個強大的特性之一通常是它的動態 SQL 能力;

MyBatis 使用一種強大的動態 SQL 語言來改進這種情形,這種語言可以被用在任意映射的 SQL 語句中。動態 SQL 元素和使用 JSTL 或其它相似的基於 XML 的文本處理器相似。MyBatis 採用功能強大的基於 OGNL 的表達式來消除其他元素。

假設數據庫中有表person和card;其中person表的字段有pid,pname,page,psex,cid;

1.條件查詢

if
使用基本的if判斷條件,解決動態where條件。具體實例:

根據姓名和年齡進行查詢:

<select id="selectPerson" parameterType="person" resultType="person">
	select * from person where 1=1
	<if test="pname!=null and pname!=''"> and
		pname like '%${pname}%' </if>
	<if test="page!=0"> <![CDATA[and page<=#{page} 
		]]>
	</if>
	<if test="psex!=null and psex!=''"> and psex=#{psex} </if>
</select>
此處在where條件後加1=1的恆等條件很好的解決了在接下來的if判斷中各個條件前面的and添加問題,實現了將後面所有的if判斷中的and條件一致化,統一都加and。select * from person where 1=1;

在動態 SQL 中所做的最通用的事情是包含部分 where 字句的條件。

<select id="selectPerson" parameterType="person" resultType="person">
	select * from person
	<where>
		<if test="pname!=null and pname!=''"> and pname
			like '%${pname}%' </if>
		<if test="page!=0"> <![CDATA[and page<=#{page} ]]>
		</if>
		<if test="psex!=null and psex!=''"> and psex=#{psex} </if>
	</where>
</select>

此處where標籤:會自動去除第一個and關鍵字,如果所有的條件不存在了,則where關鍵字也消失

sql片段:解決一段sql代碼的共享問題

<sql id="selectperson">
		<if test="pname!=null and pname!=''">
			and pname like '%${pname}%'
		</if>
		<if test="page!=0">
		<![CDATA[and page<=#{page} ]]>
		</if>
		<if test="psex!=null and psex!=''">
			and psex=#{psex}
		</if>
	</sql>
	<select id="selectPerson" parameterType="person" resultType="person">
		select * from person
		<where>
			pid=6
			<include refid="selectperson" />
		</where>
	</select>
導入片段:<include refid="selectperson"/>

2.集合查詢

foreach:解決集合查詢問題

第一種方法:用封裝類屬性的方式:具體實例

根據年齡在某個集合中進行查詢:

(1)mapper.xml映射文件:

<select id="selectPersonByAges" parameterType="personPo"
		resultType="person">
		select * from person where page in
		<foreach collection="ages" item="item" open="(" close=")"
			separator=",">
			#{item}
		</foreach>
	</select>
1-collection:指定要遍歷的集合
 2-item:定義個臨時變量接受集合中的值
 3-open:以什麼符號開始
 4-close:以什麼結尾
 5-separator:每一個值之間的分隔符
(2)mapper接口定義的方法:

public List<Person> selectPersonByAges(PersonPo p);

PersonPo類:

import java.util.List;

public class PersonPo extends Person {
	private List<Integer> ages;

	public List<Integer> getAges() {
		return ages;
	}

	public void setAges(List<Integer> ages) {
		this.ages = ages;
	}
	
}
(3)使用junit測試結果:

@Test
	public void testSelectPersonByAges(){
		PersonPo p=new PersonPo();
		List<Integer> ages=new ArrayList<Integer>();
		//ages.add(1);
		ages.add(50);
		ages.add(45);
		p.setAges(ages);
		List<Person> l=pm.selectPersonByAges(p);
		for (Person person : l) {
			System.out.println(person);
		}
		
	}

第二種方法:以list集合作爲參數方式:

(1)mapper.xml映射文件:

<select id="selectPersonByAgeList" parameterType="java.util.List"
		resultType="person">
		select * from person where page in
		<foreach collection="list" item="item" open="(" close=")"
			separator=",">
			#{item}
		</foreach>
 1-parameterType:java.util.List
  2-collection:固定值:list
(2)mapper接口定義方法:

public List<Person> selectPersonByAgeList(List<Integer> ages);

(3)使用junit測試結果:

@Test
	public void testSelectPersonByAgeList(){
		List<Integer> ages=new ArrayList<Integer>();
		//ages.add(1);
		ages.add(50);
		ages.add(45);
		
		List<Person> l=pm.selectPersonByAgeList(ages);
		System.out.println(l.size());
		for (Person person : l) {
			System.out.println(person);
		}
		
	}


第三種方法:以數組作爲參數方式:

(1)mapper.xml映射文件

</select>
	<select id="selectPersonByAgeArray" parameterType="Object[]"
		resultType="person">
		select * from person where page in
		<foreach collection="array" item="item" open="(" close=")"
			separator=",">
			#{item}
		</foreach>
	</select>

1-parameterType:Object[]
 2-collection:array

(2)接口定義方法:

public List<Person> selectPersonByAgeArray(int[] ages);//集合查詢的數組方式

(3)使用junit測試結果:

@Test
	public void testselectPersonByAgeArray(){
		int[] ages=new int[2];
		//ages.add(1);
		ages[0]=50;
		ages[1]=45;
		
		List<Person> l=pm.selectPersonByAgeArray(ages);
		System.out.println(l.size());
		for (Person person : l) {
			System.out.println(person);
		}
		
	}




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