一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Mybatis 中 Oracle 的拼接模糊查詢及用法詳解

Mybatis 中 Oracle 的拼接模糊查詢及用法詳解

2021-05-24 13:38飯娛咖啡 Java教程

這篇文章主要介紹了Mybatis 中 Oracle 的拼接模糊查詢及用法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

一、結論

這里先給大家看一下結論

oracle 中,拼接模糊查詢的正確寫法

?
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 中,拼接模糊查詢的正確寫法

?
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 中,拼接模糊查詢的用法

,是將傳入的值當做字符串的形式。所以拼接的時候 #{username} 默認自帶引號。例如: ${username} 直接轉為 ‘zhen'。

,是將傳入的數據直接顯示生成sql語句。所以拼接的時候
,是將傳入的數據直接顯示生成sql語句。所以拼接的時候
{username} 沒有默認引號。例如:${username} 直接轉為 zhen 。

二、技巧:

剛開始寫的時候一直報錯,報錯信息是這樣的:

    "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: 無效的列索引",

我的寫法是這樣的:           

?
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>-->

后來我徹底凌亂了,于是就從頭開始寫,結果就好了。

小結:

出現的報錯可能跟我之前寫了太多的if 判斷語句有關,于是先寫一個簡單的          

?
1
2
3
<if test="username != null">
       and a.user_name like '%' || #{username} || '%'
     </if>

這個可以執行,其他再有什么條件加進來,稍微修改之后,都可以正常運行。          

?
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>

總結

以上所述是小編給大家介紹的mybatis 中 oracle 的拼接模糊查詢,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/ancdc/article/details/81479622

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 嫩草影院永久入口在线观看 | 99久久香蕉| 久久99re2热在线播放7 | 日韩综合网 | 久久国产视频网站 | 国产永久一区二区三区 | 成人伊人亚洲人综合网站222 | 国内自拍第1页 | 日本xxxxx18护士xxx | 性欧美sexvideo另类 | 日韩在线中文字幕 | 精品小视频在线 | 日韩人成免费网站大片 | 吃瓜视频在线观看 | 国产最新精品视频 | 国产精品久久久久久久久ktv | 亚州精品永久观看视频 | 女人和男人搞基 | 国产手机在线αⅴ片无码观看 | 小辣椒精品福利视频导航 | 九九大香尹人视频免费 | 免费观看欧美性一级 | 性色AV乱码一区二区三区视频 | 性一交一无一伦一精一品 | 欧美va免费精品高清在线 | 国内老司机精品视频在线播出 | 国产精品美女久久久久 | 午夜福利08550 | 果冻传媒在线观看的 | 海角社区在线登录 | 日本一级不卡一二三区免费 | acg火影忍者熟密姬纲手h | 糖心视频在线观看 | 午夜想想爱 | 亚洲天堂岛国片 | a级亚洲片精品久久久久久久 | 久久电影午夜 | 国产精品视频一区二区三区不卡 | 香蕉91xj.cc | 99九九精品视频 | 色狼屋 |