mybatis中模糊查詢的方式


 
         
<!--Mapper.xml中如何進行模糊查詢-->
    <sql id="brand_columns">
             id, name, firstChar,brandName
    </sql>
    <select id="selectBrand" parameterType="com.lf.Brand" resultType="com.lf.Brand">
        select <include refid="brand_columns"/> from tb_brand
        <where>
            <!-- 直接使用 % 拼接字符串 -->
            <!-- 錯誤寫法 "%#{name}%",正確寫法: "%"#{name}"%",即#{name}能夠正確解析出來,前后再拼上%即可-->
            <if test="name != null">
                name like "%"#{name}"%"
            </if>
            <!concat(str1,str2)函數將兩個參數連接 -->
            <if test="firstChar != null">
                and first_char like concat(concat("%",#{firstChar}),"%")
            </if>
            <! bind 標簽,對字符串進行綁定,對綁定后的字符串使用 like 關鍵字進行模糊查詢 -->
            <if test="brandName != null">
                <bind name="likeBrandName" value="'%'+brandName+'%'"/>
                and brand_name like #{brandName}
            </if>
        </where>
    </select>
 
         

 

 

 


免責聲明!

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



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