Mybatis动态SQL拼接

  1. <!—需求1:
  2. 根据作者名字和博客名字来查询博客!
  3. 如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询
  4. select * from blog where title = #{title} and author = #{author}
  5. —>
  6. select * from blog where
  7. title = #{title}
  8. and author = #{author}

  9. select * from blog title = #{title} and author = #{author}

  10. update blog
  11. title = #{title},
  12. author = #{author}
  13. where id = #{id};
  1. select * from blog
  2. title = #{title}
  3. and author = #{author}
  4. and views = #{views}

  5. select * from blog
  6. <!—
  7. collection:指定输入对象中的集合属性
  8. item:每次遍历生成的对象
  9. open:开始遍历时的拼接字符串
  10. close:结束时拼接的字符串
  11. separator:遍历对象之间需要拼接的字符串
  12. select * from blog where 1=1 and (id=1 or id=2 or id=3)
  13. —>
  14. id=#{id}

title = #{title} and author = #{author}

select * from blog