好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

查看更多关于Mybatis 中 Oracle 的拼接模糊查询及用法详解的详细内容...

  阅读:81次