使用MyBatis-plus自定义SQL结尾“;“引发的错误

本文记录了在使用MyBatis-Plus(MP)进行自定义SQL查询时,由于结尾分号导致的错误。在进行分页查询的动态SQL配置中,作者发现一个普通的查询语句在添加了不必要的分号后,引发了`SqlSession`未注册的错误。通过日志分析,确定问题出在SQL语句的结尾分号上。解决方案是删除多余的分号。作者提醒,在XML中定义SQL语句时,应避免在结尾习惯性添加分号。

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

记录使用MyBatis-plus自定义SQL结尾";"引发的错误

当MP中提供的BaseMapper和Iservice中的方法不能满足我们的也无需求时,我们常常会在XXXmapper.xml文件中定制化SQL
问题展示:
这是一个普通的分页查询的动态SQL语句

<resultMap id="sysUserMap" type="com.wcj.model.system.SysUser" autoMapping="true"></resultMap>

    <select id="selectPage" resultMap="sysUserMap">
        select
        id,
        username,
        password,
        name,
        phone,
        head_url,
        dept_id,
        post_id,
        description,
        status,
        create_time,
        update_time,
        is_deleted
        from sys_user
        <where>
            <if test="vo.keyword != null and vo.keyword != ''">
                and (username like CONCAT('%',#{vo.keyword},'%')
                or name like CONCAT('%',#{vo.keyword},'%')
                or phone like CONCAT('%',#{vo.keyword},'%'))
            </if>
            <if test="vo.createTimeBegin != null and vo.createTimeBegin != ''">
                and create_time >= #{vo.createTimeBegin}
            </if>
            <if test="vo.createTimeEnd != null and vo.createTimeEnd != ''">
                and create_time &lt;=  #{vo.createTimeBegin}
            </if>
            and is_deleted = 0;
        </where>
        order by id desc;
    </select>

swagger测试时报错:

SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7deaf778] was not registered 
for synchronization because synchronization is not active

通过查看日志发现是***;***的问题
在这里插入图片描述
在这里插入图片描述
问题解决:把两个分号去掉就行了

总结

在xml中自定义SQL语句时,结尾尽量不要习惯性的加";"!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值