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属性中的判定条件必须用引号包裹,否则会产生错误。
相关推荐
ictI CABL2 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
22信通小白3 小时前
USRP初学者使用手册(基础配置及bug记录)——Windows+MATLAB
windows·matlab·bug
胡斌附体3 小时前
Windows 打包方式与 exe图标说明
windows·electron·exe·package·build·nsis
小江的记录本4 小时前
【Linux】《Linux常用命令汇总表》
linux·运维·服务器·前端·windows·后端·macos
lclcooky4 小时前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
椰汁菠萝5 小时前
Mybatis-plus + PostgreSQL json格式类型转换异常
postgresql·json·mybatis
追烽少年x6 小时前
Windows API中线程异步API简介
windows
私人珍藏库6 小时前
[Windows] 绘画工具 Krita v5.3.1
人工智能·windows·媒体·工具·软件·多功能
TlYf NTLE8 小时前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
tumeng071110 小时前
Node.JS 版本管理工具 Fnm 安装及配置(Windows)
windows·node.js