1. 程式人生 > 資訊 >《GTA5》達成 1.5 億份銷量里程碑,成美國近十年來最暢銷之作

《GTA5》達成 1.5 億份銷量里程碑,成美國近十年來最暢銷之作

動態 SQL

MyBatis 的核心就是能夠對 SQL 語句進行靈活的操作,甚至還可以通過表示式進行判斷,對 SQL 語句進行靈活 的拼裝,組成最終的想要執行的 SQL,這就是動態 SQL 的含義。 例如,在進行復雜查詢時,查詢條件可能是多個,也可能是一個,也就是說,查詢條件不確定。需要根據查詢條 件,來確定最終執行的 SQL 語句。

使用 mybatis 的動態 SQL 時,需要標籤元素的支援,最常用的為 where 元素和 if 元素

  • mapper.xml
<select id="queryUserByItemName" parameterType="userVo" resultType="user">
select * from user join item on user.user_id=item.user_id
<where>
<if test="item!=null and item.name!=null and item.name!=''">
and item.name=#{item.name}
</if>
<if test="cname!=null and cname!=''">
and user.cname like #{cname}
</if>
</where>
</select>

程式解析:配置動態 SQL 時,要用 where 標籤轉換成 where 關鍵字,不要把 where 關鍵字寫死在 SQL 中,因 為如果 SQL 中的條件判斷都不成立,就沒有後面的過濾條件,如果 where 關鍵字寫死在 SQL 語句中反而出錯。而使 用 where 標籤,由 MyBatis 框架在解析時判斷是否需要在 SQL 中加入 where 關鍵字。 if 標籤中的判斷,首先判斷是否不為 null,然後再判斷是否不為空,判斷空時使用兩個單引號,並且單引號中間 不能有空格。過濾條件的拼接關鍵字 and、or 等一定要寫死在 SQL 中,MyBatis 框架不會為 SQL 拼接 and、or 等 關鍵字,但是如果 where 關鍵字後出現了 and 或者 or 關鍵字,MyBatis 反而會去掉這個關鍵字。例如上面程式中, 第一個 if 判斷成立,SQL 語句會變成 where and item.name=? ,此時 MyBatis 就會去掉這個關鍵字

引用 SQL 片段

在 mapper.xml 配置檔案中,如果存在大量的複雜查詢,而且查詢條件相同,那麼則可以把查詢條件抽取成一個 SQL 片段,在其他 SQL中引用該片段即可。

  • mapper.xml 配置檔案
<!-- 定義 SQL 片段 -->
<sql id="userQuery">
<where>
<if test="item!=null and item.name!=null and item.name!=''">
and item.name=#{item.name}
</if>
<if test="cname!=null and cname!=''">
user.cname like #{cname}
</if>
</where>
</sql>
<!-- 根據商品名稱查詢,引用 SQL 片段 -->
<select id="queryUserByItemName" parameterType="userVo" resultType="user">
select * from user join item on user.user_id=item.user_id
<include refid="userQuery" />
</select>

程式解析:通過 SQL 片段的定義,不僅可以減少 mapper.xml 配置檔案的書寫,而且方便程式人員的開發。

使用 foreach 遍歷

在進行復雜 SQL 查詢時,往往會遇到這樣一種情況,對於某個列的值,好幾個值都符合條件,使用 SQL 書寫格 式如下:

where column='value1' or column='value2' or column='value3' 

執行此 SQL 時傳入的引數就需要是個集合,在 MyBatis 中,對於這種情況的處理,使用 foreach 元素解決。 foreach 標籤用於遍歷傳入的集合,屬性如下:

  1. collection:指定要遍歷的集合物件
  2. item:定義標識指向每次遍歷時得到的物件
  3. open:開始遍歷時要拼接的字串
  4. close:結束遍歷時要拼接的字串
  5. separator:遍歷兩個物件中間要拼接的字串
  • Mapper 介面定義
public List<User> queryUserByIds(List<Integer> idList);
  • mapper.xml 配置檔案
<select id="queryUserByIds" parameterType="list" resultMap="userMap">
select * from user
<where>
<foreach collection="list" item="value" separator=" or ">
user_id=#{value}
</foreach>
</where>
</select>
  • 測試程式:
@Test
public void testQueryUserByIds() throws Exception{
List<Integer> idList = new ArrayList<>();
idList.add(13);
idList.add(14);
idList.add(15);
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> userList = mapper.queryUserByIds(idList);
for (User userObj : userList) {
System.out.println(userObj.getCname());
}
}

程式解析:如果 List 集合中不存在元素,上面 mapper.xml 配置檔案中定義的 foreach 則不會執行,最終執行 的 SQL 為:select * from user。對上面的 SQL 進行更改為另一種格式,使用 in的方式 如下圖所示:

  • mapper.xml 配置檔案
<select id="queryUserByIds" parameterType="list" resultMap="userMap">
select * from user
<where>
<foreach collection="list" item="value" open="user_id in (" separator="," close=")">
#{value}
</foreach>
</where>
</select>

測試程式同上。