mybatis if choose ifelse 使用

以下内容为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>











猜你喜欢

转载自blog.csdn.net/u010343544/article/details/78273297