mybatis中的like使用方式


在做查詢時,我們一般會有使用like需求

例如:

1、使用$符號:它可以進行拼接,但會有sql注入的問題

    select id,name,gender,email from emp
        <where>
            <if test="id != null and id != ''">
                 and id =  #{id}
            </if>
           <if test="name != null and name != ''">
                and name like '%${name}%'
           </if>
        <where>

2、在傳入name屬性,就設置為‘%李白%’,然后使用#符號

 select id,name,gender,email from emp
        <where>
            <if test="id != null and id != ''">
                 and id =  #{id}
            </if>
           <if test="name != null and name != ''">
                and name like #{name}
           </if>
        <where>

3、使用mybatis的bind標簽

select id,name,gender,email from emp
    <bind name="_name" value="'%'+name+'%'"></bind>
    <where>
        <if test="id != null and id != ''">
            and id =  #{id}
        </if>
        <if test="name != null and name != ''">
            and name like #{_name}
        </if>
    </where>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM