【Mybatis】动态语句 第三期


*一、if和where标签

如果传入属性,就判断相等。不传入不加对应的条件。

if 判断传入的参数,最终是否添加语句
test 属性 : 内部做比较运算,最终TRUE将标签内的sql语句进行拼接,FALSE不拼接
判断语句:“key 比较运算符 值 and | or key 比较运算符 值”
大于和小于 不推荐直接写符号-> 使用 大于(>) &gt; , 小于(<) &lt;

where 标签 作用:
1. 自动添加where 关键字 , where内部有任何一个if 满足,就自动添加 where关键字,不满足就会去掉where。
2. 自动去掉多余的 and 和 or 关键字

  • 接口
    /**
     * 根据名字或薪资查询 员工表
     * @param name
     * @param salary
     * @return
     */
    List<Employee> query(@Param("name") String name, @Param("salary") Double salary);
  • xml
    • where标签会自动去掉“标签体内前面多余的and/or”
<mapper namespace="com.wake.mapper.EmployeeMapper">
    <select id="query" resultType="employee">
        select * from t_emp
        <where>
            <if test="name != null">
                emp_name = #{name}
            </if>
            <if test="salary != null and salary &gt; 100">
                and emp_salary = #{salary}
            </if>
        </where>
    </select>
</mapper>
  • 测试
    @Test
    public void testDynamicCondition(){
   
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> query = employeeMapper.query(null, 147.60000);
        System.out.println(query);
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值