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>
相关推荐
我科绝伦(Huanhuan Zhou)8 小时前
深入解析Oracle SQL调优健康检查工具(SQLHC):从原理到实战优化
数据库·sql·oracle
神奇侠202412 小时前
Hive SQL常见操作
hive·hadoop·sql
一只叫煤球的猫12 小时前
MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
后端·sql·mysql
多多*12 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap
deriva13 小时前
某水表量每15分钟一报,然后某天示数清0了,重新报示值了 ,如何写sql 计算每日水量
数据库·sql
云之兕15 小时前
MyBatis 的动态 SQL
数据库·sql·mybatis
圈圈编码18 小时前
悲观锁和乐观锁
java·开发语言·sql·mysql
qq_408413391 天前
spark 执行 hive sql数据丢失
hive·sql·spark
漫谈网络1 天前
sqlite3 命令行工具详细介绍
sql·sqlite·db
emo了小猫1 天前
Mybatis #{} 和 ${}区别,使用场景,LIKE模糊查询避免SQL注入
数据库·sql·mysql·mybatis