Druid数据源模糊查询报错

在使用Druid数据源后,进行模糊查询时遇到异常。本文介绍如何通过修改MyBatis映射文件中的SQL语句或关闭Druid的SQL监控来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Druid数据源后,模糊查询抛出异常如下

UTOOLS1590638606472.png

Druid版本如下:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.1.21</version>
</dependency>

Spring Boot中关于Druid的配置如下

#数据源基本配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
#将Spring Boot默认数据源改为Druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#数据源其他配置
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true

#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.useGlobalDataSourceStat=true
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

映射文件如下

<select id="findOrders" resultMap="order">
        SELECT * FROM orders
        <where>
            <if test="id != '' and id != null">
                id LIKE '%' #{id} '%'
            </if>
            <if test="receiverName != '' and receiverName != null">
                AND receiverName = #{receiverName}
            </if>
        </where>
</select>

myatis这边连接mysql进行模糊查询,并未更改xml文件中的sql语句。也就是说,同样的sql,在spring boot集成了druid之前是完全支持可以查询的,集成之后查询出错,报错如最上面。

Druid数据源的问题,解决方式如下

方法一:

<select id="findOrders" resultMap="order">
        SELECT * FROM orders
        <where>
            <if test="id != '' and id != null">
                id LIKE '%${id}%'
            </if>
            <if test="receiverName != '' and receiverName != null">
                AND receiverName = #{receiverName}
            </if>
        </where>
    </select>

方法二:

<select id="findOrders" resultMap="order">
        SELECT * FROM orders
        <where>
            <if test="id != '' and id != null">
                id LIKE '%' #{id,jdbcType=VARCHAR} '%'
            </if>
            <if test="receiverName != '' and receiverName != null">
                AND receiverName = #{receiverName}
            </if>
        </where>
    </select>

方法三:关闭Druid SQL监控

在配置文件中注释以下配置

#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#spring.datasource.filters=stat,wall,log4j
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
#spring.datasource.useGlobalDataSourceStat=true
#spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值