频道栏目
首页 > 资讯 > Java > 正文

在Java下,MyBatis中的动态SQL是什么?

17-11-08        来源:[db:作者]  
收藏   我要投稿

对于一些复杂的查询,我们可能会指定多个查询条件,但是这些条件可能存在也可能不存在,例如在58同城上面找房子,我们可能会指定面积、楼层和所在位置来查找房源,也可能会指定面积、价格、户型和所在位置来查找房源,此时就需要根据用户指定的条件动态生成SQL语句。如果不使用持久层框架我们可能需要自己拼装SQL语句,还好MyBatis提供了动态SQL的功能来解决这个问题。MyBatis中用于实现动态SQL的元素主要有:

- if

- choose / when / otherwise

- trim

- where

- set

- foreach

下面是映射文件的片段。

<select id="foo" parametertype="Blog" resulttype="Blog">
     select * from t_blog where 1 = 1
      
         and title = #{title}
      
      
         and content = #{content}
      
      
         and owner = #{owner}
      
</select>

当然也可以像下面这些书写。

<select id="foo" parametertype="Blog" resulttype="Blog">
    select * from t_blog where 1 = 1
     
         
            and title = #{title}
         
         
            and content = #{content}
         
         
            and owner = "owner1"
         
     
</select>

再看看下面这个例子。

<select id="bar" resulttype="Blog">
    select * from t_blog where id in
     
        #{item}
     
</select>
相关TAG标签
上一篇:在Java中,Hibernate中的DetachedCriteria类是做什么的?
下一篇:Java中Hibernate的一级缓存、二级缓存和查询缓存介绍
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站