ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Mybatis查询 动态sql

2022-04-01 16:03:09  阅读:179  来源: 互联网

标签:username users 查询 birthday concat sql Mybatis select like


Mybatis查询 动态sql

  • < sql > and < include >

    <sql id="allColumns"> id ,username,birthday,sex,address</sql>
    
    <select id="方法名" resultType="users">
    	select <include refid="allColumns"></include>
        from users
    </select>
    
  • < if > and < where >

    <select id="方法名" resultType="users">
    	select <include refid="allColumns"></include>
        from users
        <where>
        	<if test="userName != null and username ="">
    			and username like concat('%'#{username}'%')
            </if>
            <if  test="birthday != null and birthday ="">
                and birthday like concat('%'#{birthday}'%') 
            </if>                                           
        </where>
    </select>           	
    
  • < set > 为空的不更新

    <select id="方法名" resultType="users">
    	select <include refid="allColumns"></include>
        from users
        <where>
            <set>
                <if test="userName != null and username ="">
    				and username like concat('%'#{username}'%')
            	</if>
                <if  test="birthday != null and birthday ="">
                	and birthday like concat('%'#{birthday}'%') 
            	</if> 
            </set>
        </where>
    </select>       
    
  • < foreach > 循环遍历 循环条件查询 删除 增加 更改

    <select id="方法名" resultType="users">
    	select <include refid="allColumns"></include>
        from users
        <where>  id in
            <foreach collection="array" item="id" separator="," open="(",close=")">
        		#{id}
        	</foreach>
        </where>
    </select>  
    
    • 详解

      • collection:用来指定入参的类型,如果是List集合,则为list,如果为Map集合,则为map,如果是数组,则为array,
      • item:每次循环遍历出来的值或对象
      • separator:多个值或对象或者语句之间的分隔符
      • open:整个循环外的前括号 close:整个循环外面的后括号

标签:username,users,查询,birthday,concat,sql,Mybatis,select,like
来源: https://www.cnblogs.com/jotian/p/16087345.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有