gpt4 book ai didi

Mybatis 中 Oracle 的拼接模糊查询及用法详解

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Mybatis 中 Oracle 的拼接模糊查询及用法详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1、结论 。

这里先给大家看一下结论 。

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 .

2、技巧:

刚开始写的时候一直报错,报错信息是这样的:

    "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 。

最后此篇关于Mybatis 中 Oracle 的拼接模糊查询及用法详解的文章就讲到这里了,如果你想了解更多关于Mybatis 中 Oracle 的拼接模糊查询及用法详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com