Mybatis 中 Oracle 的拼接模糊查询

简介: 注意 Mybatis 中,拼接模糊查询的用法 #,是将传入的值当做字符串的形式。所以拼接的时候 #{userName} 默认自带引号。 例如: ${userName} 直接转为 'zhen'。 $,是将传入的数据直接显示生成sql语句。

一、结论

这里先给大家看一下结论

Oracle 中,拼接模糊查询的正确写法

      SELECT A.USER_ID,
            A.USER_NAME
        FROM T_USER A
            AND A.USER_NAME like concat(concat('%','w'),'%')
            或者
            AND A.USER_NAME like '%' || 'w' || '%'

Mybatis 中,拼接模糊查询的正确写法

   <select id="selectByName" resultMap="BaseResultMap">
        SELECT A.USER_ID,
            A.USER_NAME
        FROM T_USER 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语句。所以拼接的时候 ${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: 无效的列索引",

我的写法是这样的:



                <if test="userName != 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="bookId != null">
                    AND B.BOOK_ID = #{bookId}
                </if>-->

后来我彻底凌乱了,于是就从头开始写,结果就好了。

小结:

出现的报错可能跟我之前写了太多的if 判断语句有关,于是先写一个简单的

            <if test="userName != null">
                AND A.USER_NAME like '%' || #{userName} || '%'
            </if>

这个可以执行,其他再有什么条件加进来,稍微修改之后,都可以正常运行。

            <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="bookInfo.bookId != null">
                AND B.BOOK_ID = #{bookInfo.bookId}
            </if>
相关文章
|
4月前
|
Oracle Java 数据库连接
使用Mybatis generator自动生成代码,仅限Oracle数据库
使用Mybatis generator自动生成代码,仅限Oracle数据库
|
4月前
|
SQL Java 数据库连接
Mybatis【Map传参与模糊查询】
Mybatis【Map传参与模糊查询】
|
3月前
|
SQL Oracle 关系型数据库
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
87 0
|
1月前
|
Oracle Java 关系型数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
|
5月前
|
Java 关系型数据库 MySQL
MyBatis模糊查询like的三种方式
MyBatis模糊查询like的三种方式
|
4月前
|
存储 SQL Oracle
|
4月前
|
Java 数据库连接 mybatis
Mybatis中模糊查询like语句的使用方法
Mybatis中模糊查询like语句的使用方法
35 0
|
5月前
|
SQL 安全 Java
MyBatis动态语句且如何实现模糊查询及resultType与resultMap的区别---详细介绍
MyBatis动态语句且如何实现模糊查询及resultType与resultMap的区别---详细介绍
100 0
|
5月前
|
Oracle 关系型数据库 Java
Mybatis JdbcType与Oracle、MySql数据类型对应列表
Mybatis JdbcType与Oracle、MySql数据类型对应列表