基於mybatis like %%的問題
mybatis like %%問題
<select id="getMaxNotOnlineInfoByConditon" resultType="com.shishike.susie.entity.CustomConfirmInfo"> SELECT * FROM t_customconfirminfo WHERE (if(#{pduName,jdbcType=VARCHAR} IS NULL, 0 = 0, pduname = #{pduName,jdbcType=VARCHAR}) AND if(#{moduleName,jdbcType=VARCHAR} IS NULL, 0 = 0, modulename = #{moduleName,jdbcType=VARCHAR}) AND if(#{env,jdbcType=VARCHAR} IS NULL, 0 = 0, confirminfo = #{env,jdbcType=VARCHAR}) AND if(#{ossAddress,jdbcType=VARCHAR} IS NULL, 0 = 0, ossaddress = #{ossAddress,jdbcType=VARCHAR}) AND if(#{branch,jdbcType=VARCHAR} IS NULL, 0 = 0, branch like concat(concat('%',#{branch,jdbcType=VARCHAR}),'%')) AND if(#{confirmId,jdbcType=BIGINT} IS NULL, 0 = 0, id > #{confirmId,jdbcType=BIGINT}) AND onlineinfo is null) ORDER BY CREATED DESC LIMIT 1 </select>
mybatis 關於like的處理
1、利用”%”對入參進行拼接
2、SQL語句字符串拼接
SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');
3、bind標簽
<select id="findUserByFuzzyEmail" resultMap="BaseResultMap" parameterType="java.lang.String"> select id,username,email,status,createtime,updatetime from tb_user <bind name="pattern" value="'%'+_parameter.getEmail() +'%'" /> <where> <if test="email != null and email != ''"> email like #{pattern} </if> </where> </select>
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。
推薦閱讀:
- mybatis的selectKey作用詳解
- mybatis中insert主鍵ID獲取和多參數傳遞的示例代碼
- MyBatis在DAO層定義接口返回類型泛型無效的解決
- 解讀Mapper與Mapper.xml文件之間匹配的問題
- mybatis模糊查詢之bind標簽和concat函數用法詳解