1. 程式人生 > >mybatis中模糊查詢的方式

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>