SpringBoot+mybatis实现:查询一定范围内的值(可任意选择大于或小于、与或,自定义逻辑查询)

项目开发中需求的实现,作为记录和分享

1、先看需求:根据两个输入框的值,自定义逻辑进行查询。
(大于、小于;与或都可以选择,相当于用户可以自定义逻逻辑查询);
ps:虽然觉得然并软,但是毕竟是需求。
在这里插入图片描述
2、实现:
思路:将要比较得值,和比较运算符 也就是“大于”这些都作为参数,共5个参数。都传到后台;在Mapper.xml 中使用标签判断然后比较;

3、代码
controller和service中赘述;主要是接收这5个参数,
在这里插入图片描述

"compare_l":"dayv",//String类型。大于:dayv;  小于:xiaoyv
"compare_r":"xiaoyv",//String类型。大于:dayv;  小于:xiaoyv
"logic":"and",//String类型。与:and; 或:or
"mj_l":"30",//Float类型面积
"mj_r":"100"//Float类型面积

4、以下是mapper.xml文件中得代码
注释:
1、mj为数据库面积字段
2、&lt; 是 < 的转义字符;同理&gt;是 > 符号的转义字符。

<!-- 左侧输入框不为空,右侧输入框为空的情况-->
<if test="mj_l!=null and mj_l!= ''">
        <if test="mj_r==null or mj_r== ''">
        	<!-- 直接对左侧输入框的值进行判断 -->
            <if test="compare_l=='dayv' ">
                and mj &gt; #{mj_l,jdbcType=FLOAT}
            </if>
            <if test="compare_l=='xiaoyv'">
                and mj  &lt; #{mj_l,jdbcType=FLOAT}
            </if>
        </if>
        <!-- 左侧输入框不为空,右侧输入框也不为空-->
        <if test="mj_r!=null and mj_r!= ''">
        	<!-- 如果是与-->
            <if test="logic=='and'">
                <if test="compare_l=='dayv' and compare_r=='dayv'">
                    and ((mj  &gt; #{mj_l,jdbcType=FLOAT} ) and  (mj  &gt; #{mj_r,jdbcType=FLOAT}))
                </if>
                <if test="compare_l=='dayv' and compare_r=='xiaoyv'">
                    and (( mj  &gt; #{mj_l,jdbcType=FLOAT} ) and ( mj  &lt; #{mj_r,jdbcType=FLOAT}))
                </if>
                <if test="compare_l=='xiaoyv' and compare_r=='dayv'">
                    and (( mj &lt; #{mj_l,jdbcType=FLOAT} ) and ( mj  &gt; #{mj_r,jdbcType=FLOAT}))
                </if>
                <if test="compare_l=='xiaoyv' and compare_r=='xiaoyv'">
                    and (( mj &lt; #{mj_l,jdbcType=FLOAT} ) and ( mj &lt; #{mj_r,jdbcType=FLOAT}))
                </if>
            </if>
			<!-- 如果是或-->
            <if test="logic=='or'">

                <if test="compare_l=='dayv' and compare_r=='dayv'">
                    and (( mj  &gt; #{mj_l,jdbcType=FLOAT} ) or ( mj  &gt; #{mj_r,jdbcType=FLOAT}))
                </if>
                <if test="compare_l=='dayv' and compare_r=='xiaoyv'">
                    and (( mj  &gt; #{mj_l,jdbcType=FLOAT} ) or ( mj  &lt; #{mj_r,jdbcType=FLOAT}))
                </if>
                <if test="compare_l=='xiaoyv' and compare_r=='dayv'">
                    and (( mj  &lt; #{mj_l,jdbcType=FLOAT} ) or  ( mj &gt; #{mj_r,jdbcType=FLOAT}))
                </if>
                <if test="compare_l=='xiaoyv' and compare_r=='xiaoyv'">
                    and ((mj  &lt; #{mj_l,jdbcType=FLOAT} ) or  ( mj &lt; #{mj_r,jdbcType=FLOAT}))
                </if>
            </if>
        </if>
    </if>
	<!-- 左侧输入框为空,右侧输入框不为空-->
    <if test="mj_l==null or mj_l== ''">
        <if test="mj_r!=null and mj_r!= ''">
            <if test="compare_r=='dayv' ">
                and mj  &gt; #{mj_r,jdbcType=FLOAT}
            </if>
            <if test="compare_r=='xiaoyv'">
                and mj  &lt; #{mj_r,jdbcType=FLOAT}
            </if>
        </if>
    </if>

总结:
1、Mapper.xml 文件中’<'和‘>’为特殊字符需要转义
2、其实我还有一个思路,是参数直接传 < 和 > 号 然后在mapper中用#{ }注入拼接语句,但是介 < 和 > 是特殊符号,就没尝试,但是这种方式非常的冗长,如果有大佬也希望给些建议。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值