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属性中的判定条件必须用引号包裹,否则会产生错误。
相关推荐
小涛不学习10 小时前
手写线程池(从0实现 ThreadPoolExecutor 核心思想)
windows
twc82910 小时前
大模型生成 QA Pairs 提升 RAG 应用测试效率的实践
服务器·数据库·人工智能·windows·rag·大模型测试
wenlonglanying11 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
polaris063012 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
mldlds13 小时前
windows手动配置IP地址与DNS服务器以及netsh端口转发
服务器·windows·tcp/ip
取个名字太难了a13 小时前
DebugActiveProcess 调试流程分析(一)
windows
Java.熵减码农14 小时前
火绒安全软件误杀explorer.exe导致黑屏解决方法
windows
love530love14 小时前
不用聊天软件 OpenClaw 手机浏览器远程访问控制:Tailscale 配置、设备配对与常见问题全解
人工智能·windows·python·智能手机·tailscale·openclaw·远程访问控制
夏末蝉未鸣0114 小时前
Windows环境下载并安装milvus
windows·milvus
葡萄星球15 小时前
win11右键菜单一步改成win10样式
windows