MyBatis在注解中使用动态查询

以前为了使用注解并在注解中融入动态查询,会使用Provider。后来发现只要加入"<script>包含动态查询的SQL语句</script>"就可以了。

例如:

sql 复制代码
    @Select("<script>" +
            "select  v.*,u.avatar,u.nickname from videos v join users u on v.uid = u.id " +
            "<where>" +
            "<if test= \'keyword!=null and keyword!=\"\" \' >" +
            " v.video_desc like '%${keyword}%'" +
            "</if> " +
            "and v.status = 1 " +
            "</where>" +
            "order by v.create_time desc"+
            "</script>")
    public List<UserVideosResp> getAllVideosByKeyword(String keyword);

需要提示两点的是:

  1. SQL语句是使用字符串的拼接形成的,要特别注意组成SQL语句的字符串后面是不是有足够的空格。字符串拼接出来的SQL是不是符合SQL应有的语法格式。
  2. <if test='...'> if标签写在test属性中的判定条件必须用引号包裹,否则会产生错误。
相关推荐
零号全栈寒江独钓1 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
霸道流氓气质5 小时前
SpringBoot中调用mybatis方法提示映射文件未找到Invalid bound statement(not found)的奇葩解决
spring boot·后端·mybatis
一只大袋鼠6 小时前
MyBatis 从入门到实战(二):代理 Dao 开发与多表关联查询
java·开发语言·数据库·mysql·mybatis
dyxal8 小时前
内网 Windows 离线安装 uv:极速 Python 包管理器的部署实战
windows·python·uv
stone08238 小时前
如何解决Windows硬盘变成了空白
windows
零号全栈寒江独钓9 小时前
基于c/c++实现linux/windows跨平台ntp时间戳服务器
linux·c语言·c++·windows
舒一笑9 小时前
Windows 下执行 pnpm install 报 EBUSY: resource busy or locked,我最后用这一招解决了
前端·windows·程序员
tycooncool10 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
hERS EOUS10 小时前
Redis 下载与安装 教程 windows版
数据库·windows·redis
爱吃香蕉的阿豪11 小时前
Mac 远程操作 Windows 开发:ZeroTier + JetBrains 实战指南
windows·macos·zerotoer