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>
相关推荐
SAP小崔说事儿40 分钟前
在数据库中将字符串拆分成表单(SQL和HANA版本)
java·数据库·sql·sap·hana·字符串拆分·无锡sap
MatrixOrigin2 小时前
在数据库里玩“平行宇宙”:MatrixOne Data Branch 让数据也拥有Git 的分支/合并/对比/回滚(含跨集群同步)
git·sql·数据分析
surtr12 小时前
关系代数与关系型数据库
数据库·sql·数据库系统
画***林4 小时前
雷家林诗歌集录一英文版Collected Poems of Lei Jialin, Volume I (English Edition)
数据库·sql
vortex55 小时前
ORM是什么?如何理解ORM?ORM的优缺点?
java·数据库·sql·mysql·oracle·orm
l1t5 小时前
在duckdb 递归CTE中实现深度优先搜索DFS
sql·算法·深度优先·duckdb·cte
晨曦5432106 小时前
MySQL8.0窗口函数实战指南
sql
·云扬·10 小时前
InnoDB事务隔离级别与加锁机制深度解析
数据库·sql·mysql
大佬,救命!!!12 小时前
python对应sql操作
开发语言·python·sql·学习笔记·学习方法
IT枫斗者15 小时前
Java 开发实战:从分层架构到性能优化(Spring Boot + MyBatis-Plus + Redis + JWT)
java·spring boot·sql·mysql·性能优化·架构