sharding sphere 加解密功能 like语句 SQL 解析报错

问题描述

应用在使用 sharding sphere 来实现加密后,对于 like sql 语句解析抛异常,异常信息如下:

sharding sphere 版本 5.3.2

xml 文件SQL 语句:

java 复制代码
<select id="countSchoolByStatus" parameterType="java.lang.Integer" resultType="int">
        select
        count(id)
        from school
        <where>
            <if test="status != null">
                and `status` = #{status, jdbcType=INTEGER}
            </if>
            <if test="name != null">
                and `name` like '%' #{status, jdbcType=INTEGER} '%'
            </if>
        </where>
    </select>
java 复制代码
Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error preparing statement.  Cause: org.apache.shardingsphere.sql.parser.exception.SQLParsingException: You have an error in your SQL syntax: select
        count(id)
        from school
         WHERE  `status` = ?
            
            
                and `name` like '%' ? '%', no viable alternative at input '?' at line 7, position 36, near [@15,136:136='?',<42>,7:36]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) ~[mybatis-spring-1.3.2.jar:1.3.2]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-1.3.2.jar:1.3.2]
	at com.sun.proxy.$Proxy59.selectOne(Unknown Source) ~[na:na]
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166) ~[mybatis-spring-1.3.2.jar:1.3.2]

解决方案

修改 like % % 的写法,改为 like concat()

java 复制代码
<select id="countSchoolByStatus" parameterType="java.lang.Integer" resultType="int">
        select
        count(id)
        from school
        <where>
            <if test="status != null">
                and `status` = #{status, jdbcType=INTEGER}
            </if>
            <if test="name != null">
                and `name` like concat('%', #{status, jdbcType=INTEGER}, '%')
            </if>
        </where>
    </select>
相关推荐
p***92482 小时前
深入理解与实战SQL IFNULL()函数
数据库·sql·oracle
Y***98516 小时前
DVWA靶场通关——SQL Injection篇
数据库·sql
蒋士峰DBA修行之路6 小时前
实验二十八 SQL PATCH调优
数据库·sql·gaussdb
I***t7166 小时前
一条sql 在MySQL中是如何执行的
数据库·sql·mysql
n***F87510 小时前
修改表字段属性,SQL总结
java·数据库·sql
a***592615 小时前
【SQL技术】不同数据库引擎 SQL 优化方案剖析
数据库·sql
喂自己代言15 小时前
常见的关系型数据库有哪些?如何安装和使用Postgres?(中英双语版)
sql·postgresql·database
愚戏师16 小时前
MySQL SQL 注入
数据库·sql·mysql
q***333719 小时前
UNION 和 UNION ALL 的区别:深入解析 SQL 中的合并操作
数据库·sql·oracle
啊吧怪不啊吧20 小时前
SQL之表的字符串内置函数详解
大数据·数据库·sql