農(nóng)產(chǎn)品網(wǎng)站開發(fā) 文獻綜述seo外包公司興田德潤官方地址
where標(biāo)簽
?在上一節(jié)SQL 語句中加入了一個條件“1=1”,如果沒有加入這個條件,那么可能就會變成下面這樣一條錯誤的語句。
SELECT id,name,url,age,country FROM website AND name LIKE CONCAT('%',#{name},'%')
顯然以上語句會出現(xiàn) SQL 語法異常,但加入“1=1”這樣的條件又非常奇怪,所以 MyBatis 提供了 where 標(biāo)簽。
where 標(biāo)簽主要用來簡化 SQL 語句中的條件判斷,可以自動處理 AND/OR 條件,語法如下
<where><if test="判斷條件">AND/OR ...</if>
</where>
if 語句中判斷條件為 true 時,where 關(guān)鍵字才會加入到組裝的 SQL 里面,否則就不加入。where 會檢索語句,它會將 where 后的第一個 SQL 條件語句的 AND 或者 OR 關(guān)鍵詞去掉。
示例:
<select id="selectWebsite" resultType="net.cc.po.Website">select id,name,url from website<where><if test="name != null">AND name like #{name}</if><if test="url!= null">AND url like #{url}</if></where>
</select>
測試
public class Test {public static void main(String[] args) throws IOException {// 讀取配置文件mybatis-config.xmlInputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // 根據(jù)配置文件構(gòu)建SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);// 通過SqlSessionFactory創(chuàng)建SqlSessionSqlSession ss = ssf.openSession();Website site = new Website();site.setname("編程");List<Website> siteList = ss.selectList("net.cc.mapper.WebsiteMapper.selectWebsite", site);for (Website ws : siteList) {System.out.println(ws);}}
}
set標(biāo)簽
在 Mybatis 中,update 語句可以使用 set 標(biāo)簽動態(tài)更新列。set 標(biāo)簽可以為 SQL 語句動態(tài)的添加 set 關(guān)鍵字,剔除追加到條件末尾多余的逗號。
示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="net.cc.mapper.WebsiteMapper"><select id="selectWebsite" resultType="net.cc.po.Website">SELECT * FROM website<where><if test="id!=null and id!=''">id=#{id}</if></where></select><!--使用set元素動態(tài)修改一個網(wǎng)站記錄 --><update id="updateWebsite"parameterType="net.cc.po.Website">UPDATE website<set><if test="name!=null">name=#{name}</if><if test="url!=null">url=#{url}</if></set>WHERE id=#{id}</update>
</mapper>
測試
public class Test {public static void main(String[] args) throws IOException {InputStream config = Resources.getResourceAsStream("mybatis-config.xml");SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);SqlSession ss = ssf.openSession();Website site = new Website();site.setId(1);site.setUrl("www.cc.net");// 執(zhí)行update語句前List<Website> siteList = ss.getMapper(WebsiteMapper.class).selectWebsite(site);for (Website st : siteList) {System.out.println(st);}int num = ss.getMapper(WebsiteMapper.class).updateWebsite(site);System.out.println("影響數(shù)據(jù)庫行數(shù)" + num);// 執(zhí)行update語句后List<Website> siteList2 = ss.getMapper(WebsiteMapper.class).selectWebsite(site);for (Website st : siteList2) {System.out.println(st);}ss.commit();ss.close();}
}
foreach標(biāo)簽
對于一些 SQL 語句中含有 in 條件,需要迭代條件集合來生成的情況,可以使用 foreach 來實現(xiàn) SQL 條件的迭代。?
Mybatis foreach 標(biāo)簽用于循環(huán)語句,它很好的支持了數(shù)據(jù)和 List、set 接口的集合,并對此提供遍歷的功能。語法格式如下。
<foreach item="item" index="index" collection="list|array|map key" open="(" separator="," close=")">參數(shù)值
</foreach>
foreach 標(biāo)簽主要有以下屬性,說明如下。
- item:表示集合中每一個元素進行迭代時的別名。
- index:指定一個名字,表示在迭代過程中每次迭代到的位置。
- open:表示該語句以什么開始(既然是 in 條件語句,所以必然以
(
開始)。 - separator:表示在每次進行迭代之間以什么符號作為分隔符(既然是 in 條件語句,所以必然以
,
作為分隔符)。 - close:表示該語句以什么結(jié)束(既然是 in 條件語句,所以必然以
)
開始)。
使用 foreach 標(biāo)簽時,最關(guān)鍵、最容易出錯的是 collection 屬性,該屬性是必選的,但在不同情況下該屬性的值是不一樣的,主要有以下 3 種情況:
- 如果傳入的是單參數(shù)且參數(shù)類型是一個 List,collection 屬性值為 list。
- 如果傳入的是單參數(shù)且參數(shù)類型是一個 array 數(shù)組,collection 的屬性值為 array。
- 如果傳入的參數(shù)是多個,需要把它們封裝成一個 Map,當(dāng)然單參數(shù)也可以封裝成 Map。Map 的 key 是參數(shù)名,collection 屬性值是傳入的 List 或 array 對象在自己封裝的 Map 中的 key。
示例
<select id="selectWebsite"parameterType="net.cc.po.Website"resultType="net.biancheng.po.Website">SELECT id,name,url,age,countryFROM website WHERE age in<foreach item="age" index="index" collection="list" open="("separator="," close=")">#{age}</foreach>
</select>
?測試
public class Test {public static void main(String[] args) throws IOException {// 讀取配置文件mybatis-config.xmlInputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // 根據(jù)配置文件構(gòu)建SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);// 通過SqlSessionFactory創(chuàng)建SqlSessionSqlSession ss = ssf.openSession();List<Integer> ageList = new ArrayList<Integer>();ageList.add(10);ageList.add(12);List<Website> siteList = ss.selectList("net.cc.mapper.WebsiteMapper.selectWebsite", ageList);for (Website ws : siteList) {System.out.println(ws);}}
}
在使用 foreach 標(biāo)簽時,應(yīng)提前預(yù)估一下 collection 對象的長度。因為大量數(shù)據(jù)的 in 語句會影響性能,且還有一些數(shù)據(jù)庫會限制執(zhí)行的 SQL 語句長度。?