Mybatis基于注解的Mapper如何使用动态sql?

qq619771478 2016-03-03 08:19:45
网上说:
使用<script>标签直接包裹动态sql,例如:

@Select("<script>select * from user where <if test=\"username !=null \">username = #{username} </if> <if test=\"phone !=null \">phone = #{phone} </if> <if test=\"usermail !=null \">usermail = #{usermail } </if></script>")

但是并不行
...全文
3116 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
白虹李李 2016-05-07
  • 打赏
  • 举报
回复
比如可以在mapper文件里直接这样写xml(当然if也是完全类似的写法) <update id="setStatus"> update myTable <choose> <when test = "isNeedSendSMS == true"> set send_flag = 1 </when> <otherwise> set send_flag = 0 </otherwise> </choose> where id = #{Id} </update> 对应的Interface里是这样的: public int setStatus(@Param("Id") String Id, @Param("isNeedSendSMS") boolean isNeedSendSMS);
  • 打赏
  • 举报
回复
如果只是为了if else这些条件判断,直接可以写在dao.xml里面,最好说下你的最终需求。
Red2us 2016-05-05
  • 打赏
  • 举报
回复
不是很清楚你这种写法,但是你where的条件之间都不用加and 的吗
Leon_lkf 2016-05-04
  • 打赏
  • 举报
回复
这样的吗?动态SQL语句不是写在Mapper里面吗?
虾米吃小鱼 2016-05-04
  • 打赏
  • 举报
回复
ni那请问你找到解决方法了么

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧