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属性中的判定条件必须用引号包裹,否则会产生错误。
相关推荐
2601_967987371 小时前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
xbzb6 小时前
自定义 yum 软件源:Linux 上 createrepo,Windows 上纯 Python 也能造
linux·运维·windows·python·yum·createrepo
武雄(小星Ai)7 小时前
Ollama报llama-server binary not found排坑实录:服务活着引擎没了
人工智能·windows·大模型·ollama·排坑
bksczm10 小时前
Redis 持久化:RDB 快照与 AOF 日志
数据库·redis·mybatis
叼烟扛炮21 小时前
[LangChain] 学习笔记(06):突破大模型限制——结构化输出(Structured Output)
windows·笔记·学习·langchain
Thneonl1 天前
桌宠穿透不是一行 SetWindowLong
windows·程序员
CoderYanger2 天前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第二弹)
java·数据库·spring boot·spring·面试·java-ee·mybatis
艺杯羹2 天前
Windows原生软件锁设计:从暴力杀进程丢数据到零轮询事件驱动架构
windows·系统架构·开源·开源软件·内存管理·开源协议
csdn_aspnet2 天前
Redis 入门:Windows、Mac安装指南
数据库·windows·redis·缓存·mac
没差c3 天前
winsw将jar、nginx等注册为Windows服务
windows·nginx·jar