script

在注解中使用动态 SQL 其实十分简单,只需在动态 SQL 语句的外面包上一层script标签即可。如下:

@Select({"<script>",
   "SELECT * FROM imooc_user",
   "    WHERE",
   "    <choose>",
   "      <when test='id != null'>",
   "        id = #{id}",
   "      </when>",
   "      <when test='username != null'>",
   "        username = #{username}",
   "      </when>",
   "      <otherwise>",
   "        1 = 0",
   "      </otherwise>",
   "    </choose>",
   "</script>"})
User selectUserByIdOrName(@Param("id") Integer id, @Param("username") String username);
posted @ 2020-08-01 09:06  柒丶月  阅读(557)  评论(0编辑  收藏  举报