MyBatis在注解上使用动态SQL(@select使用if)

@Select({
            "<script>",
            "select * from user_info where 1=1",
            "<when test='name!=null and name!='' '>",
            "and name = #{name}",
            "</when>",
            "</script>"})

判断条件是name不为空并且不等于''(空字符串)
这该怎么写呢?上面这个写法是错的

阅读 14.8k
2 个回答

图片描述

这样试试

@Select("<script>" +
    "select * from user_info where 1=1" +
        "<where>" +
            "<if test=test='name!=null and name!=''>" +
                "and name = #{name}"+
            "</if>" +
        "</script>")
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进