Mybatis框架,Mybatis_day3_Mybatis的動態SQL

 2023-10-17 阅读 29 评论 0

摘要:動態 SQL 之< if > and < where >標簽 我們根據實體類的不同取值,使用不同的 SQL 語句來進行查詢。比如在 id 如果不為空時可以根據 id 查詢,如果 username 不同空時還要加入用戶名作為條件。這種情況在我們的多條件組合查詢中經常會碰到。使用步驟

動態 SQL 之< if > and < where >標簽

  • 我們根據實體類的不同取值,使用不同的 SQL 語句來進行查詢。比如在 id 如果不為空時可以根據 id 查詢,如果 username 不同空時還要加入用戶名作為條件。這種情況在我們的多條件組合查詢中經常會碰到。
  • 使用步驟
  1. 持久層 Dao 接口
/**
* 根據用戶信息,查詢用戶列表
* @param user
* @return
*/
List<User> findByUser(User user);

  1. 持久層 Dao 映射配置
<select id="findByUser" resultType="user" parameterType="user">select * from user where 1=1<if test="username!=null and username != '' ">and username like #{username}</if><if test="address != null">and address like #{address}</if>
</select>
  • 注意:標簽的 test 屬性中寫的是對象的屬性名,如果是包裝類的對象要使用 OGNL表達式的寫法。
    另外要注意 where 1=1 的作用~!
  • 為了簡化上面 where 1=1 的條件拼裝,我們可以采用<where>標簽來簡化開發:
<!-- 根據用戶信息查詢 -->
<select id="findByUser" resultType="user" parameterType="user">select * from user<where><if test="username!=null and username != '' ">and username like #{username}</if><if test="address != null">and address like #{address}</if></where>
</select>

  1. 測試
@Test
public void testFindByUser() {User u = new User();u.setUsername("%王%");u.setAddress("%順義%");//6.執行操作List<User> users = userDao.findByUser(u);for(User user : users) {System.out.println(user);}
}


動態標簽之< foreach >標簽

需求:
傳入多個 id 查詢用戶信息,用下邊兩個 sql 實現:

  1. SELECT * FROM USERS WHERE username LIKE ‘%張%’ AND (id =10 OR id =89 OR id=16)
  2. SELECT * FROM USERS WHERE username LIKE ‘%張%’ AND id IN (10,89,16)
  • 這樣我們在進行范圍查詢時,就要將一個集合中的值,作為參數動態添加進來。
    這樣我們將如何進行參數的傳遞?

  • 方法如下:
    1.在 QueryVo 中加入一個 List 集合用于封裝參數

/**
* 
* <p>Title: QueryVo</p>
* <p>Description: 查詢的條件</p>
* 
*/
public class QueryVo implements Serializable {
private List<Integer> ids;public List<Integer> getIds() {return ids;}public void setIds(List<Integer> ids) {this.ids = ids;}
}

  1. 持久層 Dao 接口
/**
* 根據 id 集合查詢用戶
* @param vo
* @return
*/
List<User> findInIds(QueryVo vo);

  1. 持久層 Dao 映射配置
<!-- 查詢所有用戶在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">select * from user<where><if test="ids != null and ids.size() > 0"><foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">#{uid}</foreach></if></where>
</select>
  • Mybatis框架、SQL 語句:

    • select 字段 from user where id in (?)
  • <foreach>標簽用于遍歷集合,它的屬性:

    • collection:代表要遍歷的集合元素,注意編寫時不要寫#{}
    • open:代表語句的開始部分
    • close:代表結束部分
    • item:代表遍歷集合的每個元素,生成的變量名
    • sperator:代表分隔符

  1. 編寫測試方法
@Test
public void testFindInIds() {QueryVo vo = new QueryVo();List<Integer> ids = new ArrayList<Integer>();ids.add(41);ids.add(42);ids.add(43);ids.add(46);ids.add(57);vo.setIds(ids);
//6.執行操作List<User> users = userDao.findInIds(vo);for(User user : users) {System.out.println(user);}
}



Mybatis 中簡化編寫的 SQL 片段

  • Sql 中可將重復的 sql 提取出來,使用時用 include 引用即可,最終達到 sql 重用的目的。
  • 步驟如下
  1. 定義代碼片段
<!-- 抽取重復的語句代碼片段 -->
<sql id="defaultSql">select * from user
</sql>

  1. 引用代碼片段
<!-- 配置查詢所有操作 -->
<select id="findAll" resultType="user"><include refid="defaultSql"></include>
</select>
<!-- 根據 id 查詢 -->
<select id="findById" resultType="UsEr" parameterType="int"><include refid="defaultSql"></include>where id = #{uid}
</select>
  • include refid="defaultSql"></include>
    代表
    select * from user

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/143614.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息