一、結(jié)論
這里先給大家看一下結(jié)論
oracle 中,拼接模糊查詢的正確寫(xiě)法
1
2
3
4
5
6
|
select a.user_id, a.user_name from user a and a.user_name like concat(concat( '%' , 'w' ), '%' ) 或者 and a.user_name like '%' || 'w' || '%' |
mybatis 中,拼接模糊查詢的正確寫(xiě)法
1
2
3
4
5
6
7
8
9
10
11
12
|
<select id= "selectbyname" resultmap= "baseresultmap" > select a.user_id, a.user_name from t_base_user_info a < if test= "username != null" > and a.user_name like '%' || #{username} || '%' </ if > 或者 < if test= "username != null" > and a.user_name like concat(concat( '%' , '${username}' ), '%' ) </ if > </select> |
注意 mybatis 中,拼接模糊查詢的用法
,是將傳入的值當(dāng)做字符串的形式。所以拼接的時(shí)候 #{username}
默認(rèn)自帶引號(hào)。例如: ${username}
直接轉(zhuǎn)為 ‘zhen'。
,是將傳入的數(shù)據(jù)直接顯示生成sql語(yǔ)句。所以拼接的時(shí)候
,是將傳入的數(shù)據(jù)直接顯示生成sql語(yǔ)句。所以拼接的時(shí)候
{username}
沒(méi)有默認(rèn)引號(hào)。例如:${username}
直接轉(zhuǎn)為 zhen 。
二、技巧:
剛開(kāi)始寫(xiě)的時(shí)候一直報(bào)錯(cuò),報(bào)錯(cuò)信息是這樣的:
"message": "request processing failed; nested exception is org.mybatis.spring.mybatissystemexception: nested exception is org.apache.ibatis.type.typeexception: could not set parameters for mapping: parametermapping{property='username', mode=in, javatype=class java.lang.string, jdbctype=null, numericscale=null, resultmapid='null', jdbctypename='null', expression='null'}. cause: org.apache.ibatis.type.typeexception: error setting non null for parameter #1 with jdbctype null . try setting a different jdbctype for this parameter or a different configuration property. cause: java.sql.sqlexception: 無(wú)效的列索引",
我的寫(xiě)法是這樣的:
1
2
3
4
5
6
7
8
9
10
11
12
|
< if test= "_parameter != null" > -- and a.user_name like concat( '%' , '#{username}' , '%' ) and a.user_name = #{username} </ if > <!-- < if test= "usertype != null" > and a.user_type = #{usertype} </ if > < if test= "mobilephoneno != null" > and a.mobile_phone_no like concat( '%' , '#{mobilephoneno}' , '%' ) </ if > < if test= "roleid != null" > and b.role_id = #{roleid}<br> </ if >--> |
后來(lái)我徹底凌亂了,于是就從頭開(kāi)始寫(xiě),結(jié)果就好了。
小結(jié):
出現(xiàn)的報(bào)錯(cuò)可能跟我之前寫(xiě)了太多的if 判斷語(yǔ)句有關(guān),于是先寫(xiě)一個(gè)簡(jiǎn)單的
1
2
3
|
< if test= "username != null" > and a.user_name like '%' || #{username} || '%' </ if > |
這個(gè)可以執(zhí)行,其他再有什么條件加進(jìn)來(lái),稍微修改之后,都可以正常運(yùn)行。
1
2
3
4
5
6
7
8
9
10
11
12
|
< if test= "username != null" > and a.user_name like concat(concat( '%' , '${username}' ), '%' ) </ if > < if test= "usertype != null" > and a.user_type = #{usertype} </ if > < if test= "mobilephoneno != null" > and a.mobile_phone_no like '%' || #{mobilephoneno} || '%' </ if > < if test= "baseroleinfo.roleid != null" > and b.role_id = #{baseroleinfo.roleid} </ if > |
總結(jié)
以上所述是小編給大家介紹的mybatis 中 oracle 的拼接模糊查詢,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/ancdc/article/details/81479622