zoukankan      html  css  js  c++  java
  • mybatis_动态sql 查询、更新

    1)sql where 条件
    select id="find" parameterType="User" resultType="User">
    	select id,name, age,address from user_c where 1=1
    	<if test="id!=null">
    		and id=#{id}
    	</if>
    	<if test="name!=null">
    		and name like "%"#{name}"%"
    	</if>
    	<if test="age!=null">
    		and age=#{age}
    	</if>
    	<if test="address!=null">
    		and address=#{address}
    	</if>
    </select>
    

    2)<where>…</where> 会自动将sql中的 and 去掉,就无需写 where 1=1

    select <include refid="cols"/> from user_c
    
    <where>
    	<if test="name!=null">
    	 and name like "%"#{name}"%"
    	</if>
    	
    	<if test="age!=null">
    	 and age=#{age}
    	</if>
    	
    	<if test="address!=null">
    	 and address like #{address}
    	</if>
    </where>
    

     3)<set>…</set> 标签
    会将update sql中的 set 最后一个条件多余的逗号去掉

    <update id="update" parameterType="User">
    	update user_c
    	<set>
    	<if test="name!=null">
    	and name like "%"#{name}"%“,
    	</if>
    	
    	<if test="age!=null">
    	age=#{age},
    	</if>
    	
    	<if test="address!=null">
    	address=#{address},
    	</if>
    	</set>
    	
    	where id=#{id}
    </update>
    
  • 相关阅读:
    java泛型
    java集合
    java面向对象
    java常用类
    二分查找
    递归
    选择排序+冒泡排序
    threejs入门简单例子
    css表格合并边框以及单元格宽度计算方式
    Spring Boot 有哪些优点?
  • 原文地址:https://www.cnblogs.com/zhangshiwen/p/4340464.html
Copyright © 2011-2022 走看看