Mybatis动态sql----if--where

当使用Map做为参数和pojo作参数时,两者配置文件写法相同


     <select id="SelectifwhereTestMap" parameterType="map" resultType="user">
    select * from user
    <where>
    <if test="username!=null and username!=''">
    and username like '%${username}%' 
    </if>
    
    <if test="sex!=null and sex!=''">
    and sex=#{sex} 
    </if>
    
    <if test="address!=null and address!=''">
    and address like '%${address}%' 
    </if>
    </where><!--where可以去除第一个空格或者OR-->
    </select> 
    ```


<select id="SelectifwhereTestUser" parameterType="user" resultType="user">
select * from user
<where><!--where可以去除第一个空格或者OR-->
<if test="username!=null and username!=''">
and username like '%${username}%' 
</if>

<if test="sex!=null and sex!=''">
and sex=#{sex} 
</if>

<if test="address!=null and address!=''">
and address like '%${address}%' 
</if>
</where>
</select>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Mybatis框架会根据 传入的参数判断有没有值,如果有值则 会加上where 并且去掉第一个and
    资深菜鸡程序员阅读 119评论 0 0
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,596评论 0 4
  • 阿里巴巴 JAVA 开发手册 1 / 32 Java 开发手册 版本号 制定团队 更新日期 备 注 1.0.0 阿...
    糖宝_阅读 7,652评论 0 5
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,908评论 0 5
  • 第1章 课程准备 本章首先从课程重点、特点、适合人群及学习收获几个方面对课程进行整体的介绍,然后会从一个实际的计数...
    cuzz_阅读 726评论 0 13