天天看点

MyBatis:Choose 动态查询

动态查询:choose 标签(相当于java中的if)

choose有两个标签:when and otherwise

when:     标签表示当 when 中的条件满足的时候就输出其中的内容

otherwise: 标签当所有的条件都不满足的时候就输出其中的内容

例如:

获取到bean层的ID            设置包名            设置返回值得数据类型
<select id="findUserInfoByOneParam" parameterType="Map" resultMap="UserInfoResult">
	//查询语句
	select * from userinfo 
	//设置动态查询
	<choose>
		如果这个字段不为空,则department=#{department}
		<when test="department!=null">  
			where department=#{department}
		</when>
		如果这个字段不为空,则position=#{position}
		<when test="position!=null"> 
			where position=#{position}
		</when>
		//如果以上条件都不满足则执行此语句
		<otherwise>
			where gender=#{gender}
		</otherwise>
	</choose>
</select> 
           

继续阅读