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

记录使用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语句时,结尾尽量不要习惯性的加";"!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Mybatis-plus提供了多种自定义SQL的方式,以下是其中几种常用的方式: 1. 使用@Select注解或者Mapper.xml文件中的<select>标签编写自定义SQL语句。 2. 使用@Update、@Insert、@Delete注解或者Mapper.xml文件中的<update>、<insert>、<delete>标签编写自定义SQL语句。 3. 使用Wrapper对象构建查询条件,然后调用BaseMapper的selectList()、selectOne()等方法进行查询。 4. 使用SqlHelper类提供的方法构建SQL语句,然后调用BaseMapper的selectList()、selectOne()等方法进行查询。 5. 使用自定义SQL解析器,将自定义SQL语句解析成Mybatis-plus可识别的SQL语句,然后调用BaseMapper的selectList()、selectOne()等方法进行查询。 以上是常用的几种自定义SQL的方式,具体使用哪种方式取决于具体的业务需求和个人习惯。 ### 回答2: Mybatis-plus 是一个基于 Mybatis 的增强工具,它提供了一系列解决方案,使得 Mybatis使用变得更加简单和高效。其中,自定义 SQLMybatis-plus 的一个重要特性,它提供了一种自由扩展 SQL 的方式,使得开发者可以通过自己的实现来满足各种灵活的需求。 要使用自定义 SQL,首先需要在 Mapper 接口中定义方法,并用 @Select 注解标注该方法。在注解中,我们可以书写 SQL 语句,如: ``` @Select("select * from user where name = #{name}") User findUserByName(@Param("name") String name); ``` 这里的 @Select 注解告诉了 Mybatis-plus,这是一个查询方法,所使用SQL 是 select * from user where name = #{name}。其中,#{name} 是一个占位符,Mybatis-plus 会自动将方法参数中的 name 值替换到该占位符中。 除了 @Select,Mybatis-plus 还提供了一些其他的注解,如 @Insert、@Update、@Delete 等,它们分别表示插入、更新、删除操作。类似于 @Select,这些注解也可以用自定义 SQL,声明方式都类似。 在定义完自定义 SQL 方法之后,我们就可以在业务方法中调用这些方法来实现对数据库的操作了,如: ``` User user = userMapper.findUserByName("Tom"); ``` 最后需要注意的是,虽然自定义 SQL 可以满足我们的各种需求,但是它们本身也存在一些缺点。比如它们可能会使代码可读性变低,也会存在 SQL 注入等安全问题。因此,在使用自定义 SQL 的时候,需要谨慎考虑,遵循安全和可读性原则来编写。 ### 回答3: MyBatis-Plus 是一个基于 MyBatis 的增强工具,在其基础上,提供了更加强大和便捷的操作。在使用 MyBatis-Plus 进行数据库操作时,很多情况下需要编写自定义SQL 语句来满足特定的需求,因此,学习如何自定义 SQL 语句是非常必要的。 MyBatis-Plus 支持多种方式自定义 SQL,以下分别介绍: 1. @Select 注解 @Select 注解可以在方法上加入 SQL 语句,执行时会使用SQL 语句进行查询。例如: @Select("select * from user where id = #{id}") User selectById(Long id); 2. XML 映射文件 在 MyBatis-Plus 配置文件中,可以配置 XML 映射文件的位置和名称,然后在 XML 文件中书写 SQL 语句,例如: <select id="selectById" resultType="com.example.model.User"> select * from user where id = #{id} </select> 3. 自定义接口 自定义接口是将 SQL 语句封装在方法中最为常用的方式,只需定义一个接口并继承 MyBatis-Plus 提供的 BaseMapper 接口,然后在接口中定义方法即可。例如: public interface UserMapper extends BaseMapper<User> { @Select("select * from user where username = #{username}") User selectByUsername(String username); } 4. 自定义 SQL 语句构建器 MyBatis-Plus 提供了 LambdaQueryWrapper 和 QueryWrapper 等构建器来方便编写 SQL 语句,若这些构建器无法满足我们的需求,可以自定义 SQL 语句构建器。例如: public class CustomWrapper<T> extends AbstractWrapper<T, CustomWrapper<T>> { public CustomWrapper() { super(); } public CustomWrapper(T entity) { super.setEntity(entity); } public CustomWrapper(T entity, String... columns) { super.setEntity(entity); super.select(columns); } public CustomWrapper<T> myMethod(String field, Object value) { super.eq(field, value); return this; } } 以上就是 MyBatis-Plus 自定义 SQL 的几种方式,选择哪种方式取决于个人需求和习惯。无论是哪种方式,都需要注意 SQL 注入的风险,保证 SQL 语句的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值