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>
相关推荐
Rick199316 小时前
SQL 执行流程
数据库·sql
Y0011123617 小时前
MySQL-进阶
开发语言·数据库·sql·mysql
升职佳兴20 小时前
告别套娃式子查询:SQL WITH 语句(CTE)深度实战指南
数据库·sql
流觞 无依20 小时前
DedeCMS plus/vote.php SQL注入漏洞修复教程
sql·php
小小程序员.¥21 小时前
oracle--plsql块、存储过程、存储函数
数据库·sql·oracle
玛卡巴卡ldf1 天前
【Springboot7】ApachePOI文件导入导出
java·spring boot·sql
不会写DN1 天前
SQL 单表操作全解
java·服务器·开发语言·数据库·sql
Leon-Ning Liu1 天前
Oracle 26ai新特性:SQL Firewall(SQL 防火墙)的使用方法
数据库·sql·oracle
小小程序员.¥1 天前
oracle--函数
数据库·sql·mysql
Leon-Ning Liu1 天前
Oracle 26ai 的 SQL 语言增强特性
数据库·sql·oracle