mybatis中if标签判断 属性!=‘‘和属性.trim!=‘‘区别

mybatis中if标签判断 属性!=’‘和属性.trim!=’'区别

1.准备测试环境

建表脚本

CREATE TABLE `tbl_employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `last_name` varchar(255) DEFAULT NULL,
  `gender` char(1) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

-- 库中数据
 select * from tbl_employee;
+----+-----------+--------+------------+
| id | last_name | gender | email      |
+----+-----------+--------+------------+
|  1 | tom       | 0      | tom@qq.com |
|  2 | tom       | 0      | tom@qq.com |
|  3 | tom       | 0      | tom@qq.com |
+----+-----------+--------+------------+
3 rows in set (0.000 sec)

说明:

除了主键,其余列默认可以为null,但是不建议这样,实际工作中一般字段都是非空

实体类

package com.shaoming.model.entity;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "employee实体类",description = "对应数据库的tbl_employee表")
public class Employee {
    @ApiModelProperty("主键id")
    private Integer id;
    @ApiModelProperty("员工姓名")
    private String lastName;
    @ApiModelProperty("性别")
    private Character gender;
    @ApiModelProperty("邮箱")
    private String email;
}

说明:

方便接口测试,引入swagger

mapper接口

@Mapper
public interface EmployeeMapper {
    /**
     * 测试where和if标签
     *
     * @param employee
     * @return
     */
    List<Employee> getEmpsByConditionIf(Employee employee);
}

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shaoming.mapper.EmployeeMapper">
    <select id="getEmpsByConditionIf" resultType="com.shaoming.model.entity.Employee"
            parameterType="com.shaoming.model.entity.Employee">
        select *
        from tbl_employee
        <where>
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="lastName != null and lastName != ''">
                and last_name like #{lastName}
            </if>
            <if test="gender == 0 or gender == 1">
                and gender = #{gender}
            </if>
            <if test="email != null and email != ''">
                and email = #{email}
            </if>
        </where>
    </select>
</mapper>

controller

package com.shaoming.controller;

@Api(tags = "测试controller")
@RestController
public class HelloController {
    @Autowired
    private EmployeeMapper employeeMapper;

    @ApiOperation("get请求,方式为属性名=属性值")
    @GetMapping("/gettrim")//get请求
    public R gettrim(Employee employee) {
        System.out.println("测试gettrim");
        List<Employee> empList = employeeMapper.getEmpsByConditionIf(employee);
        return R.ok("成功").put("data", empList);
    }

    @ApiOperation("post请求,参数为json格式")
    @PostMapping("/posttrim")//get请求
    public R posttrim(@RequestBody  Employee employee) {
        System.out.println("测试gettrim");
        List<Employee> empList = employeeMapper.getEmpsByConditionIf(employee);
        return R.ok("成功").put("data", empList);
    }

}

测试的swagger界面

image-20210301162612883

2.测试

参数中email为null或者为""

get请求

image-20210301162743093

可以查出数据

post请求

image-20210301162918273

可以查出数据

参数中email为" "(email为长度不为0的空串)

get请求

image-20210301163122306

查询不到数据

post请求

image-20210301163300244

查询不到数据

3.修改mapper.xml测试

修改后的mapper.xml

 <select id="getEmpsByConditionIf" resultType="com.shaoming.model.entity.Employee"
            parameterType="com.shaoming.model.entity.Employee">
        select *
        from tbl_employee
        <where>
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="lastName != null and lastName != ''">
                and last_name like #{lastName}
            </if>
            <if test="gender == 0 or gender == 1">
                and gender = #{gender}
            </if>
            <if test="email != null and email.trim() != ''">
                and email = #{email}
            </if>
        </where>
    </select>

修改的地方

把 email != '' 改为 email.trim() != ''

继续测试

参数中email为" "(email为长度不为0的空串)

按照以上请求可以查出数据

4.总结

mybatis判断属性字符窜不为空且不为空字符窜可以写成如下

 <if test="email != null and email.trim() != ''">
                and email = #{email}
            </if>

il为" "(email为长度不为0的空串)

按照以上请求可以查出数据

4.总结

mybatis判断属性字符窜不为空且不为空字符窜可以写成如下

 <if test="email != null and email.trim() != ''">
                and email = #{email}
            </if>

属性.trim()可以避免传来长度不为0的空串

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值