mybatis if choose ifelse 使用

本文介绍了一种在MyBatis中实现灵活SQL查询的方法,通过使用if和choose标签进行条件判断,使得SQL语句可以根据参数动态变化。适用于需要根据不同参数组合查询数据库的场景。

以下内容为mybatis查询数据库的时候,使用 if else 判断

使用if

		    <if test="seat_no != null and seat_no != '' ">  
		        AND seat_no = #{seat_no}  
		    </if>   
个人没有找到 mybatis 中使用 if else 的情况,所以使用了 choose when 来替代

参考:

		<choose>
		    <when test="……">
		    	……
		    </when>
		    <otherwise>
		    	……
		    </otherwise>
	    </choose>

以下是我自己真实使用的例子,并且经过了测试,仅供参考:

	<select id="qryMultiRecords"
		parameterType="com.hundsun.cloudtrade.match.dto.req.QryHoldReq"
		resultType="com.hundsun.cloudtrade.match.domain.DayHoldDomain">
		SELECT 
		    		firm_account    ,
					seat_no         ,
					stock_account   ,
					exchange_type   ,
					stock_name      ,
					stock_code      ,
					amount          ,
					market_value    ,
					position_str    
		<choose>
			<!-- 查询历史持仓表 -->
		    <when test="hold_date != null and hold_date != '' ">
		    		,hold_date FROM tb_history_hold
		    </when>
		    <!-- 查询当日持仓 -->
		    <otherwise>
		    		,'' hold_date FROM tb_day_hold
		    </otherwise>
	    </choose>
	    WHERE firm_account = #{firm_account}
		    <if test="seat_no != null and seat_no != '' ">  
		        AND seat_no = #{seat_no}  
		    </if>   
			<if test="exchange_type != null and exchange_type != '' ">  
		        AND exchange_type = #{exchange_type}  
		    </if>   
			<if test="stock_account != null and stock_account != '' ">  
		        AND stock_account = #{stock_account}  
		    </if>   
			<if test="position_str != null and position_str != '' ">  
		        AND position_str = #{position_str}  
		    </if>   
			    ORDER BY stock_code DESC
			<if test="req_number != null and req_number != '' ">  
		        LIMIT 0,#{req_number}
		    </if>
		    ;
	</select>











评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值