欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis中bind的使用示例詳解

 更新時間:2025年06月19日 16:33:03   作者:五月天的尾巴  
在 MyBatis 中,bind?標(biāo)簽允許在 OGNL 表達式上下文中創(chuàng)建一個變量并將其綁定到當(dāng)前上下文,常用于動態(tài) SQL 中簡化復(fù)雜表達式或重復(fù)計算,這篇文章主要介紹了mybatis中bind的使用示例詳解,需要的朋友可以參考下

mybatis中bind的使用

一、介紹

在 MyBatis 中,bind 標(biāo)簽允許在 OGNL 表達式上下文中創(chuàng)建一個變量并將其綁定到當(dāng)前上下文,常用于動態(tài) SQL 中簡化復(fù)雜表達式或重復(fù)計算。

二、語法

<bind name="變量名" value="OGNL表達式"/>
  • name:變量名稱,后續(xù)可直接引用。
  • value:OGNL 表達式,可以是字符串拼接、計算等操作。

三、常見示例

3.1、字符串拼接

在模糊查詢中,避免直接在 SQL 中拼接 %,提升可讀性:

<select id="searchUsers" resultType="User">
    <bind name="pattern" value="'%' + username + '%'"/>
    SELECT * FROM users 
    WHERE username LIKE #{pattern}
</select>

3.2、動態(tài)條件處理(三元表達式)

結(jié)合條件判斷,簡化復(fù)雜邏輯:

<select id="getUsers" resultType="User">
    <bind name="filterName" value="name != null ? name : '%'"/>
    SELECT * FROM users
    WHERE name LIKE #{filterName}
</select>

3.3、避免重復(fù)計算

在多次使用同一表達式時,通過 bind 復(fù)用:

<select id="calculate" resultType="double">
    <bind name="total" value="price * quantity"/>
    <bind name="discounted" value="total * (1 - discount)"/>
    SELECT #{discounted} AS final_price
</select>

3.4、bind與分頁查詢

bind經(jīng)常與分頁查詢limit一起使用,用來計算limit中的起始條數(shù)與分頁數(shù)。

<select id="pageListUser" resultType="com.demo.entity.User"
            parameterType="java.util.List">
    select * from user 
    where name = #{name}
    <if test="pageNo != null and pageSize != null">
        <bind name="pageNo" value="(pageNo-1)*pageSize"/>
        limit #{pageNo},#{pageSize}
    </if>
</select>
  • pageNo:頁碼、第幾頁
  • pageSize: 每頁顯示條數(shù)

3.5、foreach中使用bind

在 MyBatis 的 foreach 循環(huán)中,應(yīng)避免使用 bind 來創(chuàng)建每個迭代的臨時變量,因為 bind 的作用域是當(dāng)前上下文,在循環(huán)中會被覆蓋。替代方案是在 Java 代碼中預(yù)處理數(shù)據(jù),或者直接在表達式中使用循環(huán)項和索引。

若實在需要在foreach中使用bind,可以參考文末補充介紹《mybatis foreach里用bind、foreach中使用bind的坑、foreach中動態(tài)生成表名》

四、錯誤示例

4.1、where后面跟著bind

網(wǎng)上看到有些博文寫過這樣的示例,bind跟在where條件后面,但這是一種錯誤的寫法。bind 變量僅在當(dāng)前語句塊(如 <select>、<update>)內(nèi)有效。

&lt;select id="getUsersByNameAndAge" resultType="User"&gt;
    SELECT * FROM users
    WHERE name = #{name} AND age &gt;= &lt;bind name="minAge" value="${minAge}" /&gt;
&lt;/select&gt;

<bind> 必須放在 SQL 語句的最前面,不能跟在 WHERE 條件后面。

五、總結(jié)

5.1、注意事項

  • bind 變量僅在當(dāng)前語句塊(如 <select>、<update>)內(nèi)有效。
  • OGNL 表達式支持 Java 語法,如三元運算符、方法調(diào)用等。
  • 優(yōu)先使用 bind 而非直接拼接 SQL,以提高安全性和可維護性。

通過靈活使用 bind,可以顯著提升 MyBatis 動態(tài) SQL 的可讀性和簡潔性。

補充:mybatis foreach里用bind、foreach中使用bind的坑、foreach中動態(tài)生成表名

mybatis foreach里用bind、foreach中使用bind的坑、foreach中動態(tài)生成表名

一、前言

在mybatis實際開發(fā)中遇到這樣一個場景,foreach標(biāo)簽中需要動態(tài)生成一個表名,如t0,t1,t2…等等, 可以在dao層傳入,也可以在foreach中用bind標(biāo)簽生成,這里我們介紹使用bind生成該變量。

示例如下:

dao層傳入[張三、李四]。 mapper.xml中根據(jù)傳入的列表個數(shù)使用foreach進行union all拼接,并且需要動態(tài)生成表名 t0、t1等等。最終拼接sql如下:

select * from(
	select * from user where name='張三' order by id
) t0
union all
select * from(
	select * from user where name='李四' order by id
) t1

二、解決方法/foreach中使用bind

下面為公共的UserService.java、UserMapper.java中的代碼

UserService.java代碼:

@Autowired
private UserMapper userMapper;
public List<User> selectByNameList(){
    userMapper.selectByNameList(Arrays.asList("張三","李四"));
    return null;
}

UserMapper.java代碼:

public interface UserMapper  {
    List<User> selectByNameList(List<String> nameList);
}

2.1、方法一:#傳參

UserMapper.xml代碼

<select id="selectByNameList" resultType="com.demo.entity.User"
            parameterType="java.util.List">
    <foreach collection="list" item="item" separator="union all"  index="index">
        select * from (
            select * from user where name = #{item} order by id
        ) t#{index}
    </foreach>
</select>

日志如下:

==>  Preparing: select * from ( select * from user where name = ? order by id ) t? union all select * from ( select * from user where name = ? order by id ) t?
==> Parameters: 張三(String), 0(Integer), 李四(String), 1(Integer)

2.2、方法二:$傳參 bind生成變量

UserMapper.xml代碼

<select id="selectByNameList" resultType="com.demo.entity.User"
            parameterType="java.util.List">
    <foreach collection="list" item="item" separator="union all"  index="index">
        <bind name="tableName" value="'t' + index"></bind>
        select * from (
            select * from user where name = #{item} order by id
        ) ${tableName}
    </foreach>
</select>

日志如下:

==>  Preparing: select * from ( select * from user where name = ? order by id ) t0 union all select * from ( select * from user where name = ? order by id ) t1
==> Parameters: 張三(String), 李四(String)

注意:

  • ${}方式傳參有sql注入問題,所以確保${}中的變量沒有注入風(fēng)險。

三、bind變量覆蓋問題/錯誤示例

上面的示例中,我們在foreach標(biāo)簽內(nèi)使用了bind綁定變量給tableName并且用${tableName}的方式獲取值。如果我們是使用#{tableName}的方式獲取值,會發(fā)現(xiàn)每次tableName的值都會是最后一個元素。

錯誤示例一:

<select id="selectByNameList" resultType="com.demo.entity.User"
            parameterType="java.util.List">
    <foreach collection="list" item="item" separator="union all"  index="index">
        <bind name="tableName" value="'t' + index"></bind>
        select * from (
            select * from user where name = #{item} order by id
        ) #{tableName}
    </foreach>
</select>

日志文件:

==>  Preparing: select * from ( select * from user where name = ? order by id ) ? union all select * from ( select * from user where name = ? order by id ) ?
==> Parameters: 張三(String), t1(String), 李四(String), t1(String)

錯誤示例二:循環(huán)內(nèi)重復(fù)綁定同名變量

<foreach item="item" collection="list" separator=",">
  <!-- 錯誤:每次循環(huán)覆蓋 previousItem,最終所有值相同 -->
  <bind name="previousItem" value="item" />
  #{previousItem}
</foreach>

四、總結(jié)

總結(jié):在 MyBatis 的 foreach 循環(huán)中,應(yīng)避免使用 bind 來創(chuàng)建每個迭代的臨時變量,因為 bind 的作用域是當(dāng)前上下文,在循環(huán)中會被覆蓋。替代方案是在 Java 代碼中預(yù)處理數(shù)據(jù),或者直接在表達式中使用循環(huán)項和索引。

在 MyBatis 的 foreach 循環(huán)中使用 bind 元素時,需特別注意 bind 的作用域是當(dāng)前整個 SQL 語句,而非單次循環(huán)迭代。這意味著在循環(huán)內(nèi)多次使用 bind 綁定同名變量時,后一次會覆蓋前一次的值,可能導(dǎo)致邏輯錯誤。

到此這篇關(guān)于mybatis中bind的使用的文章就介紹到這了,更多相關(guān)mybatis bind使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

  • AlmaLinux 9 安裝 MySQL 8.0.32的詳細(xì)過程

    AlmaLinux 9 安裝 MySQL 8.0.32的詳細(xì)過程

    這篇文章主要介紹了AlmaLinux 9 安裝 MySQL 8.0.32的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • mysql配置SSL證書登錄的實現(xiàn)

    mysql配置SSL證書登錄的實現(xiàn)

    應(yīng)國家等級保護三級安全要求,mysql 的 ssl 需要安全證書加密,本文主要介紹了mysql配置SSL證書登錄,感興趣的可以了解一下
    2021-09-09
  • mysql語句查詢用戶權(quán)限過程詳解

    mysql語句查詢用戶權(quán)限過程詳解

    這篇文章主要介紹了mysql語句查詢用戶權(quán)限過程詳解,授予用戶的權(quán)限可能分全局層級權(quán)限、數(shù)據(jù)庫層級權(quán)限、表層級別權(quán)限、列層級別權(quán)限、子程序?qū)蛹墮?quán)限。,需要的朋友可以參考下
    2019-06-06
  • windows10系統(tǒng)安裝mysql-8.0.13(zip安裝) 的教程詳解

    windows10系統(tǒng)安裝mysql-8.0.13(zip安裝) 的教程詳解

    這篇文章主要介紹了windows10安裝mysql-8.0.13(zip安裝) 的教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Mysql數(shù)據(jù)庫之索引優(yōu)化

    Mysql數(shù)據(jù)庫之索引優(yōu)化

    MySQL憑借著出色的性能、低廉的成本、豐富的資源,已經(jīng)成為絕大多數(shù)互聯(lián)網(wǎng)公司的首選關(guān)系型數(shù)據(jù)庫。本文給大家介紹mysql數(shù)據(jù)庫之索引優(yōu)化,感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • 詳解MySQL子查詢(嵌套查詢)、聯(lián)結(jié)表、組合查詢

    詳解MySQL子查詢(嵌套查詢)、聯(lián)結(jié)表、組合查詢

    這篇文章主要介紹了MySQL子查詢(嵌套查詢)、聯(lián)結(jié)表、組合查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • MySql索引詳細(xì)介紹及正確使用方法

    MySql索引詳細(xì)介紹及正確使用方法

    這篇文章主要介紹了MySql索引詳細(xì)介紹及正確使用方法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • MySQL中TO_DAYS()函數(shù)詳解與實際應(yīng)用舉例

    MySQL中TO_DAYS()函數(shù)詳解與實際應(yīng)用舉例

    TO_DAYS函數(shù)是指從零開始到函數(shù)內(nèi)時間的天數(shù),下面這篇文章主要給大家介紹了關(guān)于MySQL中TO_DAYS()函數(shù)詳解與實際應(yīng)用的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • MySQL 使用開源審計插件示例詳解

    MySQL 使用開源審計插件示例詳解

    審計插件是包含在 MariaDB 中的,所以需要先下載 MariaDB 然后將 server_audit.so 審計插件 copy 出來,這篇文章主要介紹了MySQL 使用開源審計插件,需要的朋友可以參考下
    2023-08-08
  • 最新評論