Mybatis的動態SQL陳述句
- 專案目錄
- 動態 SQL 之if標簽
- 持久層 Dao 介面
- 持久層 Dao 映射配置
- 測驗代碼
- 動態 SQL 之where標簽
- 持久層 Dao 映射配置
- 動態 SQL 之foreach標簽
- 需求
- 在 QueryVo 中加入一個 List 集合用于封裝引數
- 持久層 Dao 介面
- 持久層 Dao 映射配置
- 測驗代碼
- Mybatis中簡化撰寫的 SQL 片段
- 定義代碼片段
- 參考代碼片段
專案目錄

動態 SQL 之if標簽
持久層 Dao 介面
/**
* 根據傳入的引數條件
* @param user 查詢的條件,有可能有用戶名 ,性別,或都沒有
* @return
*/
List<User> finduserCondition(User user);
持久層 Dao 映射配置
<!--根據條件查詢-->
<select id="finduserCondition" resultMap="userMap" parameterType="user">
select * from user where 1=1
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</select>
測驗代碼
/**
* 根據條件查詢 if標簽
* @throws Exception
*/
@Test
public void testfinduserCondition() throws Exception {
User u = new User();
u.setUserName("老王");
u.setUserSex("男");
//5.使用代理物件執行方法
List<User> users = userDao.finduserCondition(u);
for (User user : users) {
System.out.println(user);
}
}
測驗結果:

動態 SQL 之where標簽
為了簡化上面 where 1=1 的條件拼裝,我們可以采用
<where>標簽來簡化開發,
持久層 Dao 映射配置
<select id="finduserCondition" resultMap="userMap" parameterType="user">
select * from user
<where>
<if test="userName != null">
and username = #{userName}
</if>
<if test="userSex != null">
and sex = #{userSex}
</if>
</where>
</select>
效果和上面的if標簽一樣
動態 SQL 之foreach標簽
需求
傳入多個 id 查詢用戶資訊,用下邊兩個 sql 實作:
SELECT * FROM USERS WHERE username LIKE ‘%王%’ AND (id =10 OR id =19 OR id=36)
SELECT * FROM USERS WHERE username LIKE ‘%王%’ AND id IN (10,19,36)
這樣我們在進行范圍查詢時,就要將一個集合中的值,作為引數動態添加進來,
在 QueryVo 中加入一個 List 集合用于封裝引數
QueryVo:
package com.keafmd.domain;
import java.util.List;
/**
* Keafmd
*
* @ClassName: QueryVo
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-08 21:08
*/
public class QueryVo {
private User user;
private List<Integer> ids;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
持久層 Dao 介面
/**
* 根據QueryVo中提供的id集合查詢
* @param vo
* @return
*/
List<User> findUserInIds(QueryVo vo);
持久層 Dao 映射配置
<!--根據QueryVo中的id集合實作查詢查詢用戶串列-->
<select id="findUserInIds" resultMap="userMap" parameterType="queryvo">
<include refid="defaultUser"></include>
<where>
<if test="ids!=null and ids.size()>0">
<foreach collection="ids" open = "and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
測驗代碼
@Test
public void testfindUserInIds() throws Exception {
QueryVo vo = new QueryVo();
List<Integer> list = new ArrayList<Integer>();
list.add(41);
list.add(42);
list.add(50);
vo.setIds(list);
//5.使用代理物件執行方法
List<User> users = userDao.findUserInIds(vo);
for (User user : users) {
System.out.println(user);
}
}
測驗結果:

Mybatis中簡化撰寫的 SQL 片段
Sql 中可將重復的 sql 提取出來,使用時用 include 參考即可,最終達到 sql 重用的目的,
這樣可以簡化我們每次都需要在sql陳述句中寫的select * from user,
定義代碼片段
<!--了解的內容,抽取重復的sql陳述句-->
<sql id="defaultUser">
select * from user
</sql>
注意細節:在抽取重復的sql陳述句盡量不要寫分號
;,因為可能還會和后面的sql陳述句進行拼接,有分號就會導致報錯,
參考代碼片段
<!--配置查詢所有-->
<select id="findAll" resultMap="userMap">
<include refid="defaultUser"></include>
</select>
<!--根據QueryVo中的id集合實作查詢查詢用戶串列-->
<select id="findUserInIds" resultMap="userMap" parameterType="queryvo">
<include refid="defaultUser"></include>
<where>
<if test="ids!=null and ids.size()>0">
<foreach collection="ids" open = "and id in (" close=")" item="uid" separator=",">
#{uid}
</foreach>
</if>
</where>
</select>
以上就是Mybatis的動態SQL陳述句的全部內容,
看完如果對你有幫助,感謝點贊支持!
如果你是電腦端的話,看到右下角的 “一鍵三連” 了嗎,沒錯點它[哈哈]

加油!
共同努力!
Keafmd
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/259225.html
標籤:java
上一篇:LRU演算法及實作
下一篇:Python_變數和資料型別
